import { defineConfig } from 'wxt';
import vue2 from '@vitejs/plugin-vue2';
import path from 'path';
import pkg from './package.json';

const firefoxGeckoId = '{8022593c-73bb-441f-b9ff-cbaaf909c82d}';

export default defineConfig({
  srcDir: 'src/app',
  entrypointsDir: path.resolve(__dirname, 'src/entrypoints'),

  alias: {
    '@content': path.resolve(__dirname, 'src/content'),
    '@background': path.resolve(__dirname, 'src/background'),
    '@type': path.resolve(__dirname, 'src/app/types'),
  },

  vite: () => ({
    plugins: [
      {
        name: 'global-polyfill',
        transform(code, id) {
          if (id.includes('node_modules') && code.includes('global') && !code.includes('var global')) {
            return { code: `var global=globalThis;\n${code}`, map: null };
          }
        },
      },
      vue2(),
    ],
    define: {
      global: 'globalThis',
    },
    optimizeDeps: {
      esbuildOptions: {
        define: {
          global: 'globalThis',
        },
      },
    },
    build: {
      rollupOptions: {
        output: {
          banner: 'var global=globalThis;',
        },
      },
    },
    resolve: {
      alias: [
        { find: 'vue-input-tag', replacement: path.resolve(__dirname, 'node_modules/vue-input-tag/src/components/index.js') },
        { find: 'pouchdb', replacement: path.resolve(__dirname, 'node_modules/pouchdb-browser/lib/index.js') },
        { find: /^os$/, replacement: path.resolve(__dirname, 'src/shims/os.js') },
        { find: /^moment$/, replacement: path.resolve(__dirname, 'node_modules/moment/moment.js') },
      ],
    },
    css: {
      preprocessorOptions: {
        scss: {},
      },
    },
  }),

  manifest: ({ browser, mode }) => {
    const isDevMode = mode === 'development';
    const name = isDevMode ? 'AutoShopee Kit Dev' : 'AutoShopee Kit';

    return {
      name,
      short_name: name,
      author: 'AutoShopee Team',
      default_locale: 'vi',
      version: pkg.verNumber,
      description: pkg.descriptionProd,
      homepage_url: 'https://kit.autoshopee.com',

      icons: {
        48: isDevMode ? 'icons/dev_icon_48.png' : 'icons/icon_48.png',
        128: isDevMode ? 'icons/dev_icon_128.png' : 'icons/icon_128.png',
      },

      permissions: ['storage', 'tabs', 'alarms', 'webRequest', 'notifications', 'cookies'],

      host_permissions: [
        'https://*.autoshopee.com/*',
        'https://*.tiki.vn/*',
        'https://*.tiktok.com/*',
        'https://*.lazada.sg/*',
        'https://*.lazada.vn/*',
        'https://*.lazada.co.id/*',
        'https://*.lazada.com.my/*',
        'https://*.lazada.com.ph/*',
        'https://*.lazada.co.th/*',
        'https://*.shopee.sg/*',
        'https://*.shopee.com.my/*',
        'https://*.shopee.ph/*',
        'https://*.shopee.co.id/*',
        'https://*.shopee.tw/*',
        'https://*.shopee.co.th/*',
        'https://*.shopee.vn/*',
        'https://*.shopee.com.br/*',
        ...(isDevMode ? ['http://localhost:3000/*', 'http://localhost:8098/*', '*://*/*'] : []),
      ],

      content_security_policy: {
        extension_pages: isDevMode
          ? "script-src 'self' http://localhost:8098 http://localhost:3000; object-src 'self'"
          : "script-src 'self'; object-src 'self'",
      },

      options_ui: {
        page: 'options/index.html',
        open_in_tab: true,
        browser_style: true,
      },

      web_accessible_resources: [
        {
          resources: ['pdfjs/pdf.min.js', 'pdfjs/pdf.js', 'pdfjs/pdf.worker.min.js', 'pdfjs/pdf.worker.js'],
          matches: ['*://*/*'],
        },
        {
          resources: ['js/content/tiktok/script/fetch.js', 'icons/icon_48.png', 'icons/icon_128.png'],
          matches: ['https://*.tiktok.com/*'],
        },
      ],

      ...(browser === 'chrome' ? { incognito: 'split' } : {}),

      ...(browser === 'firefox'
        ? {
          browser_specific_settings: {
            gecko: { id: firefoxGeckoId },
          },
        }
        : {}),
    };
  },

  imports: {
    dirs: ['src/app/lib'],
  },

  runner: {
    disabled: true,
  },

  outDir: 'dist',

  zip: {
    name: 'vite-autoshopee-kit',
  },

  analysis: {
    enabled: process.env.ANALYZE === 'true',
    open: true,
  },
});
