require('dotenv').config();
const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');

module.exports = (env, argv) => {
    const mode = argv.mode || process.env.APP_ENV || 'production';
    
    return {
        mode: mode,
        entry: {},
        output: {
            path: path.resolve(__dirname, 'public/')
        },
        module: {
            rules: [
                {
                    test: /\.js$/,
                    exclude: /node_modules/,
                    use: {
                        loader: 'babel-loader',
                        options: {
                            presets: ['@babel/preset-env']
                        }
                    }
                }
            ]
        },
        plugins: [
            new CopyWebpackPlugin({
                patterns: [ 
                    { from: './vendor/twbs/bootstrap4/dist/css/*.css', to: 'bootstrap/4/css/[name][ext]' },
                    { from: './vendor/twbs/bootstrap4/dist/js/*.js', to: 'bootstrap/4/js/[name][ext]' },
                    { from: './vendor/twbs/bootstrap5/dist/css/*.css', to: 'bootstrap/5/css/[name][ext]' },
                    { from: './vendor/twbs/bootstrap5/dist/js/*.js', to: 'bootstrap/5/js/[name][ext]' },
                    { from: './node_modules/bootstrap-datepicker/dist/css/*.css', to: 'assets/datepicker/css/[name][ext]' },
                    { from: './node_modules/bootstrap-datepicker/dist/js/*.js', to: 'assets/datepicker/js/[name][ext]' },
                    { from: './node_modules/@fortawesome/fontawesome-free/css/*.css', to: 'assets/fontawesome/css/[name][ext]' },
                    { from: './node_modules/@fortawesome/fontawesome-free/js/*.js', to: 'assets/fontawesome/js/[name][ext]' },
                    { from: './node_modules/@fortawesome/fontawesome-free/webfonts/*', to: 'assets/fontawesome/webfonts/[name][ext]' },
                    { from: './node_modules/print-js/dist/*.css', to: 'assets/printjs/css/[name][ext]' },
                    { from: './node_modules/print-js/dist/*.js', to: 'assets/printjs/js/[name][ext]' },
                    { from: './node_modules/select2/dist/css/*.css', to: 'assets/select/css/[name][ext]' },
                    { from: './node_modules/select2/dist/js/*.js', to: 'assets/select/js/[name][ext]' },
                    { from: './node_modules/toastr/build/*.css', to: 'assets/toastr/css/[name][ext]' },
                    { from: './node_modules/toastr/build/*.js', to: 'assets/toastr/js/[name][ext]' }, 
                    { from: './node_modules/chart.js/dist/*.js', to: 'assets/charts/js/[name][ext]' },
                    { from: './node_modules/sweetalert2/dist/*.css', to: 'assets/sweetalert/css/[name][ext]' },
                    { from: './node_modules/sweetalert2/dist/*.js', to: 'assets/sweetalert/js/[name][ext]' },
                    { from: './node_modules/popper.js/dist/*.js', to: 'assets/vendor/libs/popper/[name][ext]' },
                    { from: './node_modules/datatables/media/css/*.css', to: 'assets/jquery/css/[name][ext]' },
                    { from: './node_modules/datatables/media/js/*.js', to: 'assets/jquery/js/[name][ext]' },
                    { from: './vendor/components/jquery/*.js', to: 'assets/jquery/js/[name][ext]' },
                    { from: './node_modules/datatables/media/images', to: 'assets/jquery/images' },
                    { from: './node_modules/@popperjs/core/dist/umd/*.js', to: 'assets/popper/js/[name][ext]' },
                    // ADD NEW RESOURCE TO BE COPIED HERE IF NEEDED.
                ]
            })
        ],
        resolve: {
            extensions: ['.js']
        },
        devtool: 'source-map' // Optional, for easier debugging
    };
};
