const HTMLWebpackPlugin = require('html-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const path = require('path');
const WebpackBar = require('webpackbar');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;

const merge = require('webpack-merge');

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

const deploy = package && package.deploy;

// local
const parts = require('./parts');
const commonConfig = require('./common');
const paths = require('../paths');

const createConfig = config => {
  const isProduction = config.mode === 'production';
  // const isDevelopment = config.mode === 'development';

  return merge([
    // clean in prod
    isProduction && parts.clean(paths.defaultBuild, paths.baseDir),
    {
      mode: config.mode,
      name: 'app',
      // Entries configuration
      entry: {
        main: [paths.main],
      },
      // Source mapping or not
      devtool: isProduction ? 'none' : 'eval-source-map',
    },
    // default
    parts.outputPath({
      publicPath: (isProduction && deploy && deploy.publicPath) || paths.publicPath,
      filename: isProduction ? '[name].[contenthash].js' : undefined,
    }),
    parts.loadJavaScript({
      prod: isProduction,
    }),
    parts.loadCSS({
      prod: isProduction,
    }),
    parts.setVariables({
      __BROWSER__: true,
      __SSR__: false,
      __TEST__: false,
      // used for application with different "mode"
      __PLATFORM__: config.platform || 'default',
      __PROVIDER__: config.provider || 'default',
      // environment targeted (dev vs staging vs prod vs beta ...)
      __ENV__: config.environment || 'default',
    }),
    // launch dev server
    !isProduction && {
      devServer: {
        // Enable gzip compression for everything served:
        compress: true,
        port: config.port || 4000,
        // When using the HTML5 History API, the index.html page will likely have to be served in place of any 404 r
        historyApiFallback: true,
        hot: true,
        allowedHosts: ['.lvh.me'],
      },
    },
    config.analyze && {
      plugins: [
        // analyze dependencies sizes
        new BundleAnalyzerPlugin({
          analyzerPort: config.port ? config.port + 1000 : 5000,
        }),
      ],
    },
    // Common plugins
    {
      plugins: [
        new CopyPlugin({
          patterns: [
            {
              from: path.join(paths.baseDir, config.environment === 'staging' ? 'static-staging' : 'static'),
              to: path.join(paths.defaultBuild, paths.publicPath),
              noErrorOnMissing: true,
            },
          ],
        }),
        new HTMLWebpackPlugin({
          template: paths.inAppSrc('index.html'),
        }),
        new WebpackBar(),
      ],
    },
    // Prod plugins
    isProduction && parts.imageOptimization(),
    parts.optimize(),
  ]);
};

// env can be given with --env.variable
// other params will be in params
module.exports = (env, params) => {
  params = params || {};
  const config = createConfig({
    mode: params.locale ? 'development' : 'production',
    port: params.port,
    analyze: params.analyze,
    platform: params.platform,
    provider: params.provider,
    environment: params.environment,
  });

  // mode : Possible values for mode are: none, development or production(default).
  return merge(config, commonConfig);
};
