import { defineConfig, type Plugin } from 'vite'
import vue from '@vitejs/plugin-vue'

/** WKWebView 用 file:// 加载时，crossorigin 会导致脚本无法执行、页面空白 */
function stripCrossOriginForIOS(): Plugin {
  return {
    name: 'strip-crossorigin-ios',
    apply: 'build',
    enforce: 'post',
    transformIndexHtml(html) {
      return html.replace(/\s+crossorigin(="[^"]*")?/g, '')
    },
  }
}

/** iOS 内嵌启动配置：在模块脚本之前注入，避免 file:// 下懒加载 chunk 失败 */
function injectIOSRuntimeBootstrap(): Plugin {
  return {
    name: 'inject-ios-runtime-bootstrap',
    apply: 'build',
    transformIndexHtml(html) {
      const bootstrap = `<script>
window.__WORDLOOP_RUNTIME__ = {
  embedded: true,
  iosApp: true,
  apiBase: 'https://w.tkmind.cn/api'
};
(function () {
  if (!location.hash || location.hash === '#') {
    var base = location.href.split('#')[0];
    location.replace(base + '#/');
  }
})();
</script>`
      if (html.includes('__WORDLOOP_RUNTIME__')) return html
      const withoutModule = html
        .replace(/\s+crossorigin(="[^"]*")?/g, '')
        .replace('<script type="module"', '<script')
        .replace(/\.\/assets\//g, 'wordloop://app/assets/')
      return withoutModule.replace('<script src=', `${bootstrap}\n    <script src=`)
    },
  }
}

export default defineConfig(({ mode }) => ({
  base: mode === 'ios' ? './' : '/',
  plugins: [
    vue(),
    ...(mode === 'ios' ? [stripCrossOriginForIOS(), injectIOSRuntimeBootstrap()] : []),
  ],
  build: mode === 'ios'
    ? {
        modulePreload: false,
        cssCodeSplit: false,
        rollupOptions: {
          output: {
            // WKWebView file:// 下 type=module 常无法执行，改为 IIFE 普通脚本
            format: 'iife',
            inlineDynamicImports: true,
            entryFileNames: 'assets/app.js',
            assetFileNames: 'assets/app.[ext]',
          },
        },
      }
    : undefined,
  server: {
    port: 18003,
    proxy: {
      '/api': {
        target: 'http://localhost:18004',
        changeOrigin: true,
      },
    },
  },
}))
