import { defineConfig } from 'vite';
import { crx } from '@crxjs/vite-plugin';
import { nodePolyfills } from 'vite-plugin-node-polyfills';
import manifest from './manifest.json';

export default defineConfig({
  plugins: [
    crx({
      manifest,
      contentScripts: {
        injectCss: false,
      },
    }),
    nodePolyfills({
      include: ['buffer'],
      globals: {
        Buffer: true,
        global: false,
        process: false,
      },
      protocolImports: false,
    }),
  ],
  resolve: {
    alias: {
      ebml: 'ebml/lib/ebml.js',
    },
  },
  // `define` text-replaces token symbols at bundle time. We declare
  // __MOKOSH_UAT__ as `false` in the production config so the gated
  // hook-import branches in src/background/index.ts + src/offscreen/
  // recorder.ts are static dead branches that Rollup tree-shakes. The
  // test-only build (vite.test.config.ts) overrides this to `true`. We
  // chose a dedicated token rather than gating on import.meta.env.MODE
  // because vitest also uses MODE='test' by default — gating on MODE
  // would activate the hooks during unit tests and overwrite the
  // vi.fn() chrome.* mocks the existing 83-test baseline relies on.
  // Reference: https://vite.dev/config/shared-options.html#define
  define: {
    __MOKOSH_UAT__: 'false',
    // Plan 01-12 Wave 5 (RESEARCH §12 + D-09 spirit-satisfaction):
    // Defensive token reserved for any future inline smoke-mode check.
    // Currently the smoke harness lives entirely in `smoke.sh` outside
    // Vite's input set — verified by `grep -rn 'smoke\|SMOKE\|data:text/html' src/`
    // returning empty. Activated by setting the env var `VITE_DEV=1`
    // before invoking `vite` / `vite build`; defaults to `false` so the
    // gated branch (if any future plan adds one) is statically tree-
    // shaken out of production. See scripts/README.md for the wider
    // dev-script isolation invariant.
    __VITE_DEV__: JSON.stringify(process.env.VITE_DEV === '1'),
  },
  build: {
    // Plan 01-11: bump from default ES2020 to ES2022 so gated top-level
    // await (`if (__MOKOSH_UAT__) { await import(...); }` in
    // src/background/index.ts + src/offscreen/recorder.ts) compiles.
    // The extension targets MV3 (Chrome ≥88); top-level await landed in
    // Chrome 89 / Edge 89 / Firefox 89 / Safari 15 per MDN — comfortably
    // inside the MV3 compatibility envelope.
    // Reference: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/await#browser_compatibility
    // Reference: https://vite.dev/config/build-options.html#build-target
    target: 'es2022',
    rollupOptions: {
      input: {
        offscreen: 'src/offscreen/index.html',
        // Plan 01-10 D-17-onboarding: welcome page bundled into both
        // production and test builds. The page is reached via
        // chrome.runtime.getURL('src/welcome/welcome.html') from the
        // SW's openWelcomeIfFirstInstall helper; it requires the
        // manifest's web_accessible_resources block to be navigable.
        welcome: 'src/welcome/welcome.html',
      },
    },
  },
});
