const defaultConfig = require('@wordpress/scripts/config/webpack.config');
const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');

const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports = {
  ...defaultConfig,
  entry: {
    'blocks': path.resolve(process.cwd(), 'src', 'blocks.js'),
    'settings': path.resolve(process.cwd(), 'src', 'settings.js'),
    'queues': path.resolve(process.cwd(), 'src', 'queues.js'),
    'automate': path.resolve(process.cwd(), 'src', 'automate.js'),
    'wizard': path.resolve(process.cwd(), 'src', 'wizard.js'),
    'interact': path.resolve(process.cwd(), 'src', 'interact.js'),
  },
  output: {
    ...defaultConfig.output,
    filename: '[name].js', // 自动生成 .min.js 文件
  },
  optimization: {
    ...defaultConfig.optimization,
    splitChunks: false,
    runtimeChunk: false,
    minimize: true, // 启用代码压缩
    minimizer: [
      '...', // 保留默认的 minimizer (如 Terser)
      new CssMinimizerPlugin(),
    ],
  },
  module: {
    ...defaultConfig.module,
    rules: [
      ...defaultConfig.module.rules.filter(
        rule => !(rule.test && rule.test.toString().includes('.css'))
      ),
      {
        test: /\.css$/,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader',
          {
            loader: 'postcss-loader',
            options: {
              postcssOptions: {
                plugins: [
                  require('tailwindcss'),
                  require('autoprefixer'),
                ],
              },
            },
          },
        ],
      },
    ],
  },
  plugins: [
    ...defaultConfig.plugins,
    new MiniCssExtractPlugin({
      filename: '[name].css',
    }),
    // 复制并压缩 assets 目录下的 CSS 和 JS 文件
    new CopyWebpackPlugin({
      patterns: [
        {
          from: path.resolve(process.cwd(), 'assets/css/thread-page.css'),
          to: path.resolve(process.cwd(), 'assets/css/thread-page.min.css'),
          transform: {
            async transformer(content) {
              // 使用 postcss + cssnano 压缩 CSS
              const postcss = require('postcss');
              const cssnano = require('cssnano');
              const result = await postcss([cssnano({ preset: 'default' })]).process(content.toString(), { from: undefined });
              return result.css;
            },
          },
        },
        {
          from: path.resolve(process.cwd(), 'assets/js/thread-page.js'),
          to: path.resolve(process.cwd(), 'assets/js/thread-page.min.js'),
          transform: {
            transformer(content) {
              // 使用 Terser 压缩 JS
              const { minify } = require('terser');
              return minify(content.toString(), {
                compress: true,
                mangle: true,
              }).then(result => result.code);
            },
          },
        },
      ],
    }),
  ],
};
