import type { Plugin } from 'vite';

/**
 * Add a footer with the returned value so it can return values to
 * `scripting.executeScript` Footer is added a part of esbuild to make sure it's
 * not minified. It get's removed if added to
 * `build.rollupOptions.output.footer` See
 * https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/scripting/executeScript#return_value
 */
export function iifeFooter(iifeReturnValueName: string): Plugin {
  return {
    name: 'wxt:iife-footer',
    generateBundle(_, bundle) {
      for (const chunk of Object.values(bundle)) {
        if (chunk.type === 'chunk' && chunk.isEntry) {
          const code = chunk.code;
          const marker = '\n//# sourceMappingURL=';
          const returnValue = `${iifeReturnValueName};`;

          const index = code.indexOf(marker);

          if (index >= 0) {
            chunk.code =
              code.slice(0, index + 1) +
              `${returnValue}\n` +
              code.slice(index + 1);
          } else {
            chunk.code += `\n${returnValue}`;
          }
        }
      }
    },
  };
}
