"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 resolve_exports = {};
__export(resolve_exports, {
  applyResolvePlugin: () => applyResolvePlugin
});
module.exports = __toCommonJS(resolve_exports);
var import_config = require("../config");
var import_utils = require("../utils");
var import_mergeChainedOptions = require("../mergeChainedOptions");
function applyResolvePlugin(api) {
  api.modifyBundlerChain((chain, { target, CHAIN_ID }) => {
    const config = api.getNormalizedConfig();
    const isTsProject = Boolean(api.context.tsconfigPath);
    applyExtensions({
      chain,
      config,
      target,
      isTsProject
    });
    applyAlias({
      chain,
      config,
      rootPath: api.context.rootPath
    });
    applyMainFields({
      chain,
      config,
      target
    });
    applyFullySpecified({ chain, config, CHAIN_ID });
  });
}
function applyFullySpecified({
  chain,
  CHAIN_ID
}) {
  chain.module.rule(CHAIN_ID.RULE.MJS).test(/\.m?js/).resolve.set("fullySpecified", false);
}
function applyExtensions({
  chain,
  config,
  target,
  isTsProject
}) {
  const extensions = (0, import_config.getExtensions)({
    target,
    isTsProject,
    resolveExtensionPrefix: config.source.resolveExtensionPrefix
  });
  chain.resolve.extensions.merge(extensions);
}
function applyAlias({
  chain,
  config,
  rootPath
}) {
  const { alias } = config.source;
  if (!alias) {
    return;
  }
  const mergedAlias = (0, import_mergeChainedOptions.mergeChainedOptions)({
    defaults: {},
    options: alias
  });
  Object.keys(mergedAlias).forEach((name) => {
    const values = (0, import_utils.castArray)(mergedAlias[name]);
    const formattedValues = values.map((value) => {
      if (typeof value === "string" && value.startsWith(".")) {
        return (0, import_utils.ensureAbsolutePath)(rootPath, value);
      }
      return value;
    });
    chain.resolve.alias.set(
      name,
      formattedValues.length === 1 ? formattedValues[0] : formattedValues
    );
  });
}
function applyMainFields({
  chain,
  config,
  target
}) {
  const { resolveMainFields } = config.source;
  if (!resolveMainFields) {
    return;
  }
  const mainFields = Array.isArray(resolveMainFields) ? resolveMainFields : resolveMainFields[target];
  if (mainFields) {
    mainFields.reduce((result, fields) => {
      if (Array.isArray(fields)) {
        result.push(...fields);
      } else {
        result.push(fields);
      }
      return result;
    }, []).forEach((field) => {
      chain.resolve.mainFields.add(field);
    });
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  applyResolvePlugin
});
