"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var output_exports = {};
__export(output_exports, {
  applyOutputPlugin: () => applyOutputPlugin
});
module.exports = __toCommonJS(output_exports);
var import_path = require("path");
var import_fs = require("../fs");
var import_constants = require("../constants");
var import_utils = require("../utils");
function applyOutputPlugin(api) {
  api.modifyBundlerChain(
    async (chain, { isProd, isServer, isServiceWorker }) => {
      const config = api.getNormalizedConfig();
      const jsPath = (0, import_fs.getDistPath)(config.output, "js");
      const publicPath = getPublicPath({
        config,
        isProd,
        context: api.context
      });
      const jsFilename = (0, import_fs.getFilename)(config.output, "js", isProd);
      chain.output.path(api.context.distPath).filename(import_path.posix.join(jsPath, jsFilename)).chunkFilename(import_path.posix.join(jsPath, `async/${jsFilename}`)).publicPath(publicPath).pathinfo(false).hashFunction("xxhash64");
      if (isServer) {
        const serverPath = (0, import_fs.getDistPath)(config.output, "server");
        const filename = import_path.posix.join(serverPath, `[name].js`);
        chain.output.filename(filename).chunkFilename(filename).libraryTarget("commonjs2");
      }
      if (isServiceWorker) {
        const workerPath = (0, import_fs.getDistPath)(config.output, "worker");
        const filename = import_path.posix.join(workerPath, `[name].js`);
        chain.output.filename(filename).chunkFilename(filename).libraryTarget("commonjs2");
      }
    }
  );
}
function getPublicPath({
  config,
  isProd,
  context
}) {
  var _a, _b, _c;
  const { dev, output } = config;
  let publicPath = import_constants.DEFAULT_ASSET_PREFIX;
  if (isProd) {
    if (output.assetPrefix) {
      publicPath = output.assetPrefix;
    }
  } else if (typeof dev.assetPrefix === "string") {
    publicPath = dev.assetPrefix;
  } else if (dev.assetPrefix === true) {
    const protocol = ((_a = context.devServer) == null ? void 0 : _a.https) ? "https" : "http";
    const hostname = ((_b = context.devServer) == null ? void 0 : _b.hostname) || import_constants.DEFAULT_DEV_HOST;
    const port = ((_c = context.devServer) == null ? void 0 : _c.port) || import_constants.DEFAULT_PORT;
    if (hostname === import_constants.DEFAULT_DEV_HOST) {
      const localHostname = `localhost`;
      publicPath = `${protocol}://${localHostname}:${port}/`;
    } else {
      publicPath = `${protocol}://${hostname}:${port}/`;
    }
  }
  return (0, import_utils.addTrailingSlash)(publicPath);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  applyOutputPlugin
});
