"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);

// src/config.ts
var config_exports = {};
__export(config_exports, {
  withHighlightConfig: () => withHighlightConfig
});
module.exports = __toCommonJS(config_exports);

// src/util/highlightWebpackPlugin.ts
var import_lib = require("@highlight-run/sourcemap-uploader/dist/lib");
var HighlightWebpackPlugin = class {
  apiKey;
  appVersion;
  path;
  basePath;
  constructor(apiKey, appVersion, path, basePath) {
    this.apiKey = apiKey;
    this.appVersion = appVersion;
    this.path = path;
    this.basePath = basePath;
  }
  apply(compiler) {
    compiler.hooks.afterEmit.tap("HighlightWebpackPlugin", () => {
      (0, import_lib.uploadSourcemaps)({
        apiKey: this.apiKey,
        appVersion: this.appVersion,
        path: this.path,
        basePath: this.basePath,
        allowNoop: true
      });
    });
    return compiler;
  }
};

// src/util/withHighlightConfig.ts
var getDefaultOpts = async (config, highlightOpts) => {
  const isProdBuild = process.env.NODE_ENV === "production";
  const hasSourcemapApiKey = !!process.env.HIGHLIGHT_SOURCEMAP_UPLOAD_API_KEY || !!highlightOpts?.apiKey;
  let version = null;
  if (config.generateBuildId) {
    const buildId = config.generateBuildId();
    if (typeof buildId === "string") {
      version = buildId;
    } else {
      version = await buildId;
    }
  }
  return {
    uploadSourceMaps: isProdBuild && (highlightOpts?.uploadSourceMaps ?? (!config.productionBrowserSourceMaps && hasSourcemapApiKey)),
    configureHighlightProxy: highlightOpts?.configureHighlightProxy ?? true,
    apiKey: highlightOpts?.apiKey ?? "",
    appVersion: highlightOpts?.appVersion ?? version ?? "",
    serviceName: highlightOpts?.serviceName ?? "",
    sourceMapsPath: highlightOpts?.sourceMapsPath ?? ".next/",
    sourceMapsBasePath: highlightOpts?.sourceMapsBasePath ?? "_next/"
  };
};
var getHighlightConfig = async (config, highlightOpts) => {
  const defaultOpts = await getDefaultOpts(config, highlightOpts);
  let newRewrites = config.rewrites;
  if (defaultOpts.uploadSourceMaps || defaultOpts.configureHighlightProxy) {
    newRewrites = async () => {
      let re;
      if (!config.rewrites) {
        re = new Array();
      } else {
        re = await config.rewrites();
      }
      const sourcemapRewrite = {
        source: "/:path*.map",
        destination: "/404"
      };
      const highlightRewrite = {
        source: "/highlight-events",
        destination: "https://pub.highlight.run"
      };
      if (!re || Array.isArray(re)) {
        return {
          beforeFiles: defaultOpts.uploadSourceMaps ? [sourcemapRewrite] : [],
          afterFiles: defaultOpts.configureHighlightProxy ? (re ?? []).concat(highlightRewrite) : [],
          fallback: []
        };
      } else {
        return {
          beforeFiles: defaultOpts.uploadSourceMaps ? (re.beforeFiles ?? []).concat(sourcemapRewrite) : re.beforeFiles,
          afterFiles: defaultOpts.configureHighlightProxy ? (re.afterFiles ?? []).concat(highlightRewrite) : re.afterFiles,
          fallback: re.fallback
        };
      }
    };
  }
  let newWebpack = config.webpack;
  if (defaultOpts.uploadSourceMaps) {
    newWebpack = (webpackConfig, opts) => {
      let originalConfig = webpackConfig;
      if (config.webpack) {
        originalConfig = config.webpack(webpackConfig, opts);
      }
      originalConfig.plugins.push(
        new HighlightWebpackPlugin(
          defaultOpts.apiKey,
          defaultOpts.appVersion,
          defaultOpts.sourceMapsPath,
          defaultOpts.sourceMapsBasePath
        )
      );
      return originalConfig;
    };
  }
  return {
    ...config,
    productionBrowserSourceMaps: defaultOpts.uploadSourceMaps || config.productionBrowserSourceMaps,
    rewrites: newRewrites,
    webpack: newWebpack
  };
};
var withHighlightConfig = async (config, highlightOpts) => {
  if (typeof config === "function") {
    return async (phase, { defaultConfig }) => {
      const userNextConfigObject = config(phase, { defaultConfig });
      if (typeof userNextConfigObject === "function") {
        const nc = await userNextConfigObject;
        return await getHighlightConfig(nc, highlightOpts);
      } else {
        return await getHighlightConfig(
          userNextConfigObject,
          highlightOpts
        );
      }
    };
  } else {
    return await getHighlightConfig(config, highlightOpts);
  }
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  withHighlightConfig
});
//# sourceMappingURL=config.cjs.map