/* eslint-env node */

import * as webpack from 'webpack';
import * as path from 'path';
import { ConsoleRemotePlugin } from "@openshift-console/dynamic-plugin-sdk-webpack";
import CopyWebpackPlugin from 'copy-webpack-plugin';
import { Configuration as WebpackConfiguration } from "webpack";
import { Configuration as WebpackDevServerConfiguration } from "webpack-dev-server";

interface Configuration extends WebpackConfiguration {
  devServer?: WebpackDevServerConfiguration;
}

const isProd = process.env.NODE_ENV === "production";

const config: Configuration = {
  mode: isProd ? "production" : "development",
  entry: {},  // No regular entry points needed. All plugin related scripts are generated via ConsoleRemotePlugin
  context: path.resolve(__dirname, "src"),
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: isProd ? '[name]-bundle-[hash].min.js' : '[name]-bundle.js',
    chunkFilename: isProd ? '[name]-chunk-[chunkhash].min.js' : '[name]-chunk.js',
  },
  resolve: {
    extensions: [".ts", ".tsx", ".js", ".jsx"],
  },
  module: {
    rules: [
      {
        test: /\.(jsx?|tsx?)$/,
        exclude: /\/node_modules\//,
        use: [
          {
            loader: "ts-loader",
            options: {
              configFile: path.resolve(__dirname, "tsconfig.json"),
            },
          },
        ],
      },
      {
        test: /\.(css)$/,
        use: ["style-loader", "css-loader"],
      },
      {
        test: /\.(png|jpg|jpeg|gif|svg|woff2?|ttf|eot|otf)(\?.*$|$)/,
        type: "asset/resource",
        generator: {
          filename: isProd ? 'assets/[contenthash][ext]' : 'assets/[name][ext]',
        },
      },
      {
        test: /\.(m?js)$/,
        resolve: {
          fullySpecified: false,
        },
      },
    ],
  },
  plugins: [
    new ConsoleRemotePlugin({
      pluginMetadata: {
        name: "project-pause-plugin",
        version: "0.0.1",
        displayName: "Project Pause Plugin",
        description: "Adds ability to pause and resume projects",
        exposedModules: {
          projectActions: "./components/project-actions",
        },
        dependencies: {
          "@openshift-console/dynamic-plugin-sdk": "*",
          "@patternfly/react-core": "*",
          "react": "*",
          "react-dom": "*",
          "react-i18next": "*"
        }
      },
      extensions: [
        {
          type: "console.action/provider",
          properties: {
            contextId: "namespace-overview-actions",
            provider: {
              $codeRef: "projectActions.PauseProjectAction",
            },
          },
        },
      ]
    }),
    new CopyWebpackPlugin({
      patterns: [{ from: 'locales', to: 'locales' }],
    }),
  ],
  devServer: {
    static: "./dist",
    port: 9001,
    allowedHosts: "all",
    headers: {
      "Access-Control-Allow-Origin": "*",
      "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, PATCH, OPTIONS",
      "Access-Control-Allow-Headers": "X-Requested-With, Content-Type, Authorization"
    },
    devMiddleware: {
      writeToDisk: true,
    },
  },
  devtool: isProd ? false : "source-map",
  optimization: {
    chunkIds: isProd ? 'deterministic' : 'named',
    minimize: isProd,
  },
};

export default config;
