const config = require('./config.json');
const package = require('./package.json');

const autoprefixer = require('autoprefixer');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const HandlebarsPlugin = require('handlebars-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const path = require('path');
const RemoveEmptyScriptsPlugin = require('webpack-remove-empty-scripts');
const RtlCssPlugin = require('rtl-css-transform-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');

const paths = {
    src: {
        js: './icing/js',
        scss: './icing/scss',
        images: './src/images',
    },
    dist: {
        js: './js',
        css: './css',
        images: './assets/images',
    }
};

module.exports = (env, argv) => {
    return {
        target: 'web',
        devtool: 'source-map',
        entry: {
            icing: [
                paths.src.js + '/main.js', paths.src.scss + '/main.scss'
            ]
        },
        module: {
            rules: [
                {
                    test: /\.(sass|scss)$/,
                    include: path.resolve(__dirname, paths.src.scss.slice(2)),
                    use: [
                        {
                            loader: MiniCssExtractPlugin.loader
                        },
                        {
                            loader: 'css-loader',
                            options: {
                                url: false
                            }
                        },
                        {
                            loader: 'postcss-loader',
                            options: {
                                postcssOptions: {
                                    plugins: [['autoprefixer']]
                                }
                            }
                        },
                        {
                            loader: 'sass-loader',
                            options: {
                                sourceMap: true,
                            },
                        }
                    ]
                },
                {
                    test: /\.css$/,
                    loader: 'css-loader',
                    options: {
                        import: false
                    },
                },
                {
                    test: /\.js$/,
                    exclude: /node_modules/,
                    loader: 'babel-loader'
                }
            ]
        },
        plugins: [
            /*new CopyPlugin({
                patterns: [
                    {
                        from: paths.src.images,
                        to: paths.dist.images,
                        noErrorOnMissing: true
                    },
                    {
                        from: paths.src.favicon,
                        to: paths.dist.favicon,
                        noErrorOnMissing: true
                    }
                ]
            }),*/
            /*new HandlebarsPlugin({
                entry: path.join(process.cwd(), 'src', 'html', '**', '*.{html,hbs}'),
                partials: [path.join(process.cwd(), 'src', 'partials', '**', '*.{html,hbs}')],
                helpers: {
                    rootPath: function () {
                        return '{{rootPath}}';
                    },
                    currentYear: function () {
                        return new Date().getFullYear();
                    },
                    versionNumber: function () {
                        return 'v' + package.version;
                    },
                    json: function (obj) {
                        return JSON.stringify(obj);
                    },
                    eq: (v1, v2) => v1 === v2,
                    ne: (v1, v2) => v1 !== v2,
                    lt: (v1, v2) => v1 < v2,
                    gt: (v1, v2) => v1 > v2,
                    lte: (v1, v2) => v1 <= v2,
                    gte: (v1, v2) => v1 >= v2,
                    and() {
                        return Array.prototype.every.call(arguments, Boolean);
                    },
                    or() {
                        return Array.prototype.slice.call(arguments, 0, -1).some(Boolean);
                    }
                },
                data: config,
                onBeforeSave: function (Handlebars, resultHtml, filename) {
                    const level = filename.split('//').pop().split('/').length;
                    const finalHtml = resultHtml.split('{{rootPath}}').join('.'.repeat(level));

                    return finalHtml;
                },
                output: path.join(process.cwd(), 'dist', '[path]', '[name].html'),
            }),*/
            new MiniCssExtractPlugin({
                filename: paths.dist.css + '/[name].bundle.css'
            }),
            /*new RtlCssPlugin({
                filename: paths.dist.css + '/[name].rtl.bundle.css'
            }),*/
            //new RemoveEmptyScriptsPlugin()
        ],
        /*optimization: !production ? null : {
            splitChunks: false,
            minimizer: [
                new CssMinimizerPlugin(),
                new TerserPlugin({
                    terserOptions: {
                        output: {
                            comments: false
                        }
                    },
                    extractComments: false
                })
            ]
        },*/
        output: {
            filename: paths.dist.js + '/[name].bundle.js'
        }
    }
};
