// @ts-check
const { DefinePlugin } = require("webpack");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const HTMLWebpackPlugin = require("html-webpack-plugin");
const {
  kemonoSite,
  sentryDSN,
  siteName,
  iconsPrepend,
  bannersPrepend,
  thumbnailsPrepend,
  artistsOrCreators,
  disableDMs,
  disableFAQ,
  disableFilehaus,
  sidebarItems,
  footerItems,
  bannerGlobal,
  bannerWelcome,
  homeBackgroundImage,
  homeMascotPath,
  homeLogoPath,
  paysiteList,
  homeWelcomeCredits,
  homeAnnouncements,
  headerAd,
  middleAd,
  footerAd,
  sliderAd,
  videoAd,
  isArchiveServerEnabled,
  apiServerBaseURL,
  apiServerPort,
  analyticsEnabled,
  analyticsCode,
  gitCommitHash,
  isFileServingEnabled,
  buildDate,
} = require("./configs/vars.mjs");

/**
 * @type import("webpack").Configuration
 */
const webpackConfig = {
  entry: {
    index: "./src/index.tsx",
  },
  plugins: [
    // ...pagePlugins,
    new HTMLWebpackPlugin({
      title: siteName,
      filename: "index.html",
      favicon: "./static/favicon.ico",
      meta: {
        "og:type": "website",
        "og:site_name": siteName,
        "og:title": siteName,
        "og:image": `${kemonoSite}/static/kemono-logo.svg`,
        "og:image:width": "150",
        "og:image:height": "150",
      },
      templateParameters: {
        analytics: !analyticsEnabled
          ? undefined
          : !analyticsCode
          ? undefined
          : atob(analyticsCode),
      },
      template: "./src/index.html",
    }),

    // https://webpack.js.org/plugins/define-plugin/
    new DefinePlugin({
      BUNDLER_ENV_KEMONO_SITE: JSON.stringify(kemonoSite),
      BUNDLER_ENV_SENTRY_DSN: JSON.stringify(sentryDSN),
      BUNDLER_ENV_SITE_NAME: JSON.stringify(siteName),
      BUNDLER_ENV_ICONS_PREPEND: JSON.stringify(iconsPrepend),
      BUNDLER_ENV_BANNERS_PREPEND: JSON.stringify(bannersPrepend),
      BUNDLER_ENV_THUMBNAILS_PREPEND: JSON.stringify(thumbnailsPrepend),
      BUNDLER_ENV_ARTISTS_OR_CREATORS: JSON.stringify(artistsOrCreators),
      BUNDLER_ENV_DISABLE_DMS: JSON.stringify(disableDMs),
      BUNDLER_ENV_DISABLE_FAQ: JSON.stringify(disableFAQ),
      BUNDLER_ENV_DISABLE_FILEHAUS: JSON.stringify(disableFilehaus),
      BUNDLER_ENV_SIDEBAR_ITEMS: JSON.stringify(sidebarItems),
      BUNDLER_ENV_FOOTER_ITEMS: JSON.stringify(footerItems),
      BUNDLER_ENV_BANNER_GLOBAL: JSON.stringify(bannerGlobal),
      BUNDLER_ENV_BANNER_WELCOME: JSON.stringify(bannerWelcome),
      BUNDLER_ENV_HOME_BACKGROUND_IMAGE: JSON.stringify(homeBackgroundImage),
      BUNDLER_ENV_HOME_MASCOT_PATH: JSON.stringify(homeMascotPath),
      BUNDLER_ENV_HOME_LOGO_PATH: JSON.stringify(homeLogoPath),
      BUNDLER_ENV_HOME_WELCOME_CREDITS: JSON.stringify(homeWelcomeCredits),
      BUNDLER_ENV_HOME_ANNOUNCEMENTS: JSON.stringify(homeAnnouncements),
      BUNDLER_ENV_PAYSITE_LIST: JSON.stringify(paysiteList),
      BUNDLER_ENV_HEADER_AD: JSON.stringify(headerAd),
      BUNDLER_ENV_MIDDLE_AD: JSON.stringify(middleAd),
      BUNDLER_ENV_FOOTER_AD: JSON.stringify(footerAd),
      BUNDLER_ENV_SLIDER_AD: JSON.stringify(sliderAd),
      BUNDLER_ENV_VIDEO_AD: JSON.stringify(videoAd),
      BUNDLER_ENV_IS_ARCHIVER_ENABLED: JSON.stringify(isArchiveServerEnabled),
      BUNDLER_ENV_API_SERVER_BASE_URL: JSON.stringify(apiServerBaseURL),
      BUNDLER_ENV_API_SERVER_PORT: JSON.stringify(apiServerPort),
      BUNDLER_ENV_GIT_COMMIT_HASH: JSON.stringify(gitCommitHash),
      BUNDLER_ENV_BUILD_DATE: JSON.stringify(buildDate),
      BUNDLER_ENV_IS_FILE_SERVING_ENABLED: JSON.stringify(isFileServingEnabled),
    }),
    new CopyWebpackPlugin({
      patterns: [
        {
          from: "static",
          to: "static",
        },
      ],
    }),
  ],
  resolve: {
    extensions: [".tsx", ".ts", ".js"],
    fallback: {
      stream: false,
    },
  },
};

module.exports = webpackConfig;
