const {merge} = require('webpack-merge');
const { sentryWebpackPlugin } = require("@sentry/webpack-plugin");
const commonConfiguration = require('./webpack.common.js');
const {CleanWebpackPlugin} = require('clean-webpack-plugin');
const CopyWebpackPlugin = require("copy-webpack-plugin");
const path = require("path");
const webpack = require('webpack')

// available CI variables https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
const SENTRY_DSN = process.env.SENTRY_DSN_DEV;

const version = require(path.resolve(process.cwd(), 'package.json')).version;
const SENTRY_RELEASE = `${process.env.CI_PROJECT_NAME} ${version}`;
const AMPLITUDE_API_KEY = process.env.AMPLITUDE_API_KEY_TEST

module.exports = (env) => {
  return merge(
    commonConfiguration(env),
    {
      context: path.resolve(process.cwd()), // Корень проекта
      entry: './src/staging.js', // Относительный путь от контекста
      mode: 'production',
      devtool: 'source-map',
      plugins:
        [
          new webpack.DefinePlugin({
            __SENTRY_DSN: JSON.stringify(SENTRY_DSN),
            __SENTRY_RELEASE: JSON.stringify(SENTRY_RELEASE),
            __AMPLITUDE_API_KEY: JSON.stringify(AMPLITUDE_API_KEY),
          }),
          new CleanWebpackPlugin(),
          new CopyWebpackPlugin({
            patterns: [
              { from: path.resolve(__dirname, '../loader/loader.js') },
            ],
          }),
          sentryWebpackPlugin({
            authToken: process.env.SENTRY_AUTH_TOKEN,
            org: "sentry",
            project: process.env.SENTRY_PROJECT,
            url: "https://sentry.popiplay.dev",
          }),
        ],
    },
  );
}
