// next.config.js
const withPlugins = require('next-compose-plugins')
const optimizedImages = require('next-optimized-images')
const webpack = require("webpack")
const withCSS = require('@ostai/next-css')
const withOffline = require('next-offline')
const Dotenv = require('dotenv-webpack');

const { ANALYZE, DEV } = process.env

module.exports = withPlugins(
  [
    [withCSS],
    [optimizedImages, {
      /* config for next-optimized-images */
    }],
    [withOffline, {
      dontAutoRegisterSw: DEV ? true : false,
      workboxOpts: {
        runtimeCaching: [
          // { urlPattern: /.*auth.*/, handler: 'networkOnly' },
          // { urlPattern: /.*account.*/, handler: 'networkOnly' },
          { urlPattern: /^https?.*/, handler: 'NetworkFirst' },
          {
            urlPattern: /\.(?:png|jpg|jpeg|svg|webp)$/,
            handler: "CacheFirst",
            options: {
              cacheName: "images",
              expiration: {
                maxEntries: 100
              }
            }
          }
        ],
        importScripts:
          ['/statique/js/Edeclic-service-worker.js',
            '/statique/js/backgroundSync-sw.js'
          ]
      },
    }],
    [{
      publicRuntimeConfig: {
        // DEV
        // symphonyUrl: 'http://localhost/app_dev.php/',
        // blogUrl: 'https://blog.talentstube.com',
        // frontServerUrl: 'http://localhost:3000',
        // cvThequeUrl: 'https://recruteur.talentstube.com',
        // PROD
        symphonyUrl: 'https://recruteur.talentstube.com/',
        blogUrl: 'https://blog.talentstube.com',
        frontServerUrl: 'https://www.talentstube.com/',
        cvThequeUrl: 'https://recruteur.talentstube.com',
        // PRE PROD
        // symphonyUrl: 'https://pp.talentstube.com/',
        // blogUrl: 'https://blog.talentstube.com',
        // frontServerUrl: 'https://pp.app.talentstube.com/',
        // cvThequeUrl: 'https://recruteur.talentstube.com',
        adListDefaultUrl: '/statique/images/default/default_picture_company.svg',
        cachedMediaUrl: '/media/cache/profile_thumb/uploads/img/',
        postedContentUrl: '/uploads/img/',
        postedContentCv: '/uploads/cv/',
        ReCAPTCHA: {
          "siteKey": "6LdyqLwUAAAAADKI5uC7d5W9VqVgzd8VEmMU2Yi-"
        },
        applicationServerPublicKey: 'BNFyiyPiLH97Dgc0m1vXluUNaCN6y3Hkgd4YcqS2lVkP8my3tq2D5gM5g7cY6R4VxC7GljSzkLRu+k+oO+2TtjU='
        //applicationServerPublicKey: 'BG2lV673DvB67dzEwX9GKfds3sXaEdtZo2oWO1cO2xUdTJAFZ_rV_UrP1nHQz-F8LJNO9HxG0kmQRIb54JL1ZCA'
      }
    }]
  ],
  {
    webpack: (config, { isServer }) => {

      if (ANALYZE) {
        const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer')

        config.plugins.push(new BundleAnalyzerPlugin({
          analyzerMode: 'server',
          analyzerPort: isServer ? 8888 : 8889,
          openAnalyzer: true
        }))
      }
      const videojsPlugin = new webpack.ProvidePlugin({
        videojs: "video.js/dist/video.cjs.js",
        RecordRTC: "recordrtc",
        MediaStreamRecorder: ["recordrtc", "MediaStreamRecorder"]
      });
      const videojsAlias = {
        videojs: "video.js",
        WaveSurfer: "wavesurfer.js",
        RecordRTC: "recordrtc"
      };

      config.resolve.alias = { ...config.resolve.alias, ...videojsAlias };
      config.plugins.push(videojsPlugin);

      config.plugins.push(new Dotenv());

      return config
    }
  },
  {
    generateBuildId: async () => {
      // For example get the latest git commit hash here

      return 'v0.1.0'
    }
  },

);