import { defineConfig, mergeConfig } from "vite";
import { VitePWA } from "vite-plugin-pwa";
import { createBuildConfig } from "./vite.config.common.mts";
import { loadAppConfig } from "./vite.config.utils.mts";

export default defineConfig(async () => {
  const baseConfig = await createBuildConfig('web');
  const appConfig = await loadAppConfig();

  return mergeConfig(baseConfig, {
    plugins: [
      VitePWA({
        registerType: 'autoUpdate',
        manifest: appConfig.pwaConfig?.manifest,
        devOptions: {
          enabled: false
        },
        workbox: {
          cleanupOutdatedCaches: true,
          skipWaiting: true,
          clientsClaim: true,
          sourcemap: true
        }
      })
    ]
  });
});
