const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
//we're currently using next-pwa (which uses GeneraeSW automatically), so we don't need to use workbox-webpack-plugin
// const { InjectManifest, GenerateSW } = require('workbox-webpack-plugin');

const withPWA = require('next-pwa')({
  dest: 'public',
  register: true, // ?
  publicExcludes: ["!_error*.js"], //?
  skipWaiting: true,
  // disable: process.env.NODE_ENV === 'development',
})

module.exports = withPWA({
  typescript: {
    // !! WARN !!
    // Dangerously allow production builds to successfully complete even if
    // your project has type errors.
    // !! WARN !!
    ignoreBuildErrors: true,
  },
  compress: true,
  pageExtensions: ['ts', 'tsx', 'md', 'mdx'], // Replace `jsx?` with `tsx?`
  env: {
    env: process.env.APP_ENV,
    server: process.env.NEXT_PUBLIC_PUBLIC_URL
  },
  // pwa: {
  //   dest: 'public',
  //   register: true,
  //   publicExcludes: ["!_error*.js"],
  //   disable: process.env.NODE_ENV === 'development',
  //   // sw: './worker/index.js', // Custom service worker file name
  // },

  // plugins: [
  //   // new InjectManifest({
  //   //   // These are some common options, and not all are required.
  //   //   // Consult the docs for more info.
  //   //   //exclude: [/.../, '...'],
  //   //   maximumFileSizeToCacheInBytes: 1 * 1024 * 1024,
  //   //   swSrc: './worker.js',
  //   // }),
  //   // new GenerateSW({
  //   //   //disable all files 
  //   //   maximumFileSizeToCacheInBytes: 10 * 1024 * 1024,
  //   //   // swSrc: './worker.js',
  //   // }),
  // ],
  webpack: (config, { isServer, buildId, dev }) => {
    // Configure optimization and source maps
    config.optimization.minimize = !dev;
    //config.productionBrowserSourceMaps = true;
    // Enable source maps based on non-production environments
    if (!dev) {
      //config.devtool = 'source-map';
      /* ⨯ TypeError: Invalid character in header content ["Location"]
      at ServerResponse.setHeader (node:_http_outgoing:655:3)
      at _res.setHeader (D:\DEV\workspace\REPOS\git.d-popov.com\mwhitnessing\node_modules\next\dist\server\base-server.js:481:24)
      at NodeNextResponse.setHeader (D:\DEV\workspace\REPOS\git.d-popov.com\mwhitnessing\node_modules\next\dist\server\base-http\node.js:74:19)
      at NodeNextResponse.redirect (D:\DEV\workspace\REPOS\git.d-popov.com\mwhitnessing\node_modules\next\dist\server\base-http\index.js:43:14)
      at handleRedirect (D:\DEV\workspace\REPOS\git.d-popov.com\mwhitnessing\node_modules\next\dist\server\base-server.js:1208:17)
      at DevServer.renderToResponseWithComponentsImpl (D:\DEV\workspace\REPOS\git.d-popov.com\mwhitnessing\node_modules\next\dist\server\base-server.js:1666:23)
      at async DevServer.renderPageComponent (D:\DEV\workspace\REPOS\git.d-popov.com\mwhitnessing\node_modules\next\dist\server\base-server.js:1856:24)
      at async DevServer.renderToResponseImpl (D:\DEV\workspace\REPOS\git.d-popov.com\mwhitnessing\node_modules\next\dist\server\base-server.js:1894:32)
      at async DevServer.pipeImpl (D:\DEV\workspace\REPOS\git.d-popov.com\mwhitnessing\node_modules\next\dist\server\base-server.js:911:25)
      at async NextNodeServer.handleCatchallRenderRequest (D:\DEV\workspace\REPOS\git.d-popov.com\mwhitnessing\node_modules\next\dist\server\next-server.js:271:17)
      at async DevServer.handleRequestImpl (D:\DEV\workspace\REPOS\git.d-popov.com\mwhitnessing\node_modules\next\dist\server\base-server.js:807:17)
      at async D:\DEV\workspace\REPOS\git.d-popov.com\mwhitnessing\node_modules\next\dist\server\dev\next-dev-server.js:331:20
      at async Span.traceAsyncFn (D:\DEV\workspace\REPOS\git.d-popov.com\mwhitnessing\node_modules\next\dist\trace\trace.js:151:20)
      at async DevServer.handleRequest (D:\DEV\workspace\REPOS\git.d-popov.com\mwhitnessing\node_modules\next\dist\server\dev\next-dev-server.js:328:24) {
    code: 'ERR_INVALID_CHAR',
    page: '/cart/publishers/edit/react_devtools_backend_compact.js.map' */
    }
    // Add custom fallbacks
    config.resolve.fallback = { ...config.resolve.fallback, fs: false };

    // InjectManifest configuration
    if (!isServer) {
      // config.plugins.push(new InjectManifest({
      //   // swSrc: './worker.js', // Path to source service worker file
      //   // swDest: '/worker.js', // Destination filename in the build output
      //   maximumFileSizeToCacheInBytes: 10 * 1024 * 1024, // Adjust as needed
      //   exclude: [/\.map$/, /_error.js$/, /favicon.ico$/] // Customize exclusion patterns
      // })
      // );
    }

    // Bundle Analyzer Configuration
    if (process.env.ANALYZE && !isServer) {
      //const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
      config.plugins.push(
        new BundleAnalyzerPlugin({
          analyzerMode: 'static',
          openAnalyzer: true,
          generateStatsFile: true,
        })
      );
    }

    return config;
  },
  i18n: {
    // next-intl
    // https://next-intl-docs.vercel.app/docs/usage/messages
    // using https://next-intl-docs.vercel.app/docs/getting-started/pages-router
    locales: ['bg', 'en', 'ru'],
    defaultLocale: 'bg',
    localeDetection: false,
  },
})