const path = require("path");
const { DefinePlugin } = require("webpack");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const { buildHTMLWebpackPluginsRecursive } = require("./configs/build-templates");
const {
  kemonoSite,
  nodeEnv,
  iconsPrepend,
  bannersPrepend,
  thumbnailsPrepend,
  creatorsLocation,
} = require("./configs/vars");

const projectPath = path.resolve(__dirname, "src");
const pagesPath = path.join(projectPath, "pages");
const pagePlugins = buildHTMLWebpackPluginsRecursive(pagesPath, {
  fileExtension: "html",
  pluginOptions: {
    inject: false,
    minify: false,
  },
});

/**
 * TODO: make separate entries for `admin` and `moderator`
 * @type import("webpack").Configuration
 */
const webpackConfig = {
  entry: {
    global: path.join(projectPath, "js", "global.js"),
    admin: path.join(projectPath, "js", "admin.js"),
    // moderator: path.join(projectPath, "js", "moderator.js"),
  },
  plugins: [
    ...pagePlugins,
    // https://webpack.js.org/plugins/define-plugin/
    new DefinePlugin({
      BUNDLER_ENV_KEMONO_SITE: JSON.stringify(kemonoSite),
      BUNDLER_ENV_NODE_ENV: JSON.stringify(nodeEnv),
      BUNDLER_ENV_ICONS_PREPEND: JSON.stringify(iconsPrepend),
      BUNDLER_ENV_BANNERS_PREPEND: JSON.stringify(bannersPrepend),
      BUNDLER_ENV_THUMBNAILS_PREPEND: JSON.stringify(thumbnailsPrepend),
      BUNDLER_ENV_CREATORS_LOCATION: JSON.stringify(creatorsLocation),
    }),
    new CopyWebpackPlugin({
      patterns: [
        {
          from: "static",
          to: "static",
        },
      ],
    }),
  ],
  resolve: {
    extensions: [".js"],
    alias: {
      ["@wp/pages"]: path.join(projectPath, "pages", "_index.js"),
      ["@wp/components"]: path.join(projectPath, "pages", "components", "_index.js"),
      ["@wp/env"]: path.join(projectPath, "env"),
      ["@wp/lib"]: path.join(projectPath, "lib"),
      ["@wp/js"]: path.join(projectPath, "js"),
      ["@wp/css"]: path.join(projectPath, "css"),
      ["@wp/assets"]: path.join(projectPath, "assets"),
      ["@wp/api"]: path.join(projectPath, "api", "_index.js"),
      ["@wp/utils"]: path.join(projectPath, "utils", "_index.js"),
    },
    fallback: {
      stream: false,
    },
  },
};

module.exports = webpackConfig;
