// next.config.js
const fetch = require('isomorphic-unfetch')
const withPlugins = require('next-compose-plugins');

const withOffline = require('next-offline')
const withCSS = require('@ostai/next-css')

const webpack = require("webpack");

const withTM = require('next-transpile-modules')
const withAlias = require('@blunck/next-alias')

const optimizedImages = require('next-optimized-images')

const { ANALYZE, DEV } = process.env
 const videojsPlugin = new webpack.ProvidePlugin({
                videojs: "video.js/dist/video.cjs.js",
                RecordRTC: "recordrtc",
                MediaStreamRecorder: ["recordrtc", "MediaStreamRecorder"]
            });
module.exports = withPlugins(
    [
        [withCSS],
        [optimizedImages, {
            /* config for next-optimized-images */
        }],
       // [videojsPlugin],
        // [withAlias,{videojs: "video.js",
        //      WaveSurfer: "wavesurfer.js",
        //      RecordRTC: "recordrtc",
        // }],
     
        [withOffline, {
            dontAutoRegisterSw: DEV ? true : false,
            workboxOpts: {
                // globPatterns: ['static/**/*'],
                // globDirectory: '.',
                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:
                    ['/static/js/firebase-messaging-sw.js',
                        '/static/js/backgroundSync-sw.js'
                    ]
            },

        }],
       
        [{
            // serverRuntimeConfig: { // Will only be available on the server side
            //     // mySecret: 'secret',
            //     // secondSecret: process.env.SECOND_SECRET // Pass through env variables
            // },
            publicRuntimeConfig: { // Will be available on both server and client
                staticFolder: '/static',
                push: {
                    'defaultAction': 'https://application.bzh/',
                    'defaultIconLink': 'https://application.bzh/static/images/icons/e-declicIco152x152.png',
                    'defaultImageLink': 'https://application.bzh/static/images/banner/atc-site-web-responsive.jpg',
                    'topics': [
                        { key: 'e-declic', value: false },
                        { key: 'PWA', value: false }
                    ]
                },
                wordpressUrl: {
                    'menu': 'https://api.e-declic.net/wp-json/menus/v1/menus/top',
                    'posts': 'https://api.e-declic.net/wp-json/wp/v2/posts',
                    'postDetail': 'https://api.e-declic.net/wp-json/wp/v2/posts',
                    'pageDetail': 'https://api.e-declic.net/wp-json/wp/v2/pages',
                    'categories': 'https://api.e-declic.net/wp-json/wp/v2/categories',
                    'media': 'https://api.e-declic.net/wp-json/wp/v2/media'

                },
                ReCAPTCHA: {
                    "siteKey": "6LcCvHcUAAAAAEw2gBZkoJcVvsNp1IgU4MnrIaRI"
                },
                eDeclicUrl: 'http://www.e-declic.com',
            }
        }]

    ],
    //  [withTM,{ transpileModules: ['webrtc-adapter', 'videojs-record']}],
    {
        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.module.rules.push(
            //     {
            //         test: /\.css$/,
            //         loader: 'style-loader!css-loader',
            //         // include: [/node_modules/]
            //     },
            
            // )

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

            return 'v0.1.0'
        }
    },



);