// src/util/highlightWebpackPlugin.ts
import { uploadSourcemaps } from "@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", () => {
      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);
  }
};
export {
  withHighlightConfig
};
//# sourceMappingURL=config.js.map