var __defProp = Object.defineProperty;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
import * as tsr from "tsconfig-resolver";
import * as path from "path";
import * as url from "url";
const normalize = (pathname) => String(pathname).split(path.sep).join(path.posix.sep);
const getExistingConfig = (searchName, cwd) => {
  const config = tsr.tsconfigResolverSync({ cwd, searchName });
  return config.exists ? config : null;
};
const getConfigAlias = (cwd) => {
  const config = getExistingConfig("tsconfig.json", cwd) || getExistingConfig("jsconfig.json", cwd);
  if (!config)
    return null;
  const compilerOptions = Object(config.config.compilerOptions);
  if (!compilerOptions.baseUrl)
    return null;
  const baseUrl = path.posix.resolve(path.posix.dirname(normalize(config.path)), normalize(compilerOptions.baseUrl));
  const aliases = [];
  for (let [alias, values] of Object.entries(Object(compilerOptions.paths))) {
    values = [].concat(values);
    const find = new RegExp(`^${[...alias].map((segment) => segment === "*" ? "(.+)" : segment.replace(/[\\^$*+?.()|[\]{}]/, "\\$&")).join("")}$`);
    let matchId = 0;
    for (let value of values) {
      const replacement = [...path.posix.resolve(baseUrl, value)].map((segment) => segment === "*" ? `$${++matchId}` : segment === "$" ? "$$" : segment).join("");
      aliases.push({ find, replacement });
    }
  }
  aliases.push({
    find: /^(?!\.*\/)(.+)$/,
    replacement: `${[...baseUrl].map((segment) => segment === "$" ? "$$" : segment).join("")}/$1`
  });
  return aliases;
};
function configAliasVitePlugin(astroConfig) {
  const configAlias = getConfigAlias(astroConfig.projectRoot && url.fileURLToPath(astroConfig.projectRoot));
  if (!configAlias)
    return {};
  return {
    name: "@astrojs/vite-plugin-config-alias",
    enforce: "pre",
    async resolveId(sourceId, importer, options) {
      const resolvedId = await this.resolve(sourceId, importer, __spreadValues({ skipSelf: true }, options));
      if (resolvedId)
        return resolvedId;
      for (const alias of configAlias) {
        if (alias.find.test(sourceId)) {
          const aliasedSourceId = sourceId.replace(alias.find, alias.replacement);
          const resolvedAliasedId = await this.resolve(aliasedSourceId, importer, __spreadValues({ skipSelf: true }, options));
          if (resolvedAliasedId)
            return resolvedAliasedId;
        }
      }
    }
  };
}
export {
  configAliasVitePlugin as default
};
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vLi4vc3JjL3ZpdGUtcGx1Z2luLWNvbmZpZy1hbGlhcy9pbmRleC50cyJdLAogICJtYXBwaW5ncyI6ICI7Ozs7Ozs7Ozs7Ozs7Ozs7QUFBQTtBQUNBO0FBQ0E7QUFXQSxNQUFNLFlBQVksQ0FBQyxhQUFxQixPQUFPLFVBQVUsTUFBTSxLQUFLLEtBQUssS0FBSyxLQUFLLE1BQU07QUFHekYsTUFBTSxvQkFBb0IsQ0FBQyxZQUFvQixRQUE4RDtBQUMzRyxRQUFNLFNBQVMsSUFBSSxxQkFBcUIsRUFBRSxLQUFLO0FBRS9DLFNBQU8sT0FBTyxTQUFTLFNBQVM7QUFBQTtBQUlsQyxNQUFNLGlCQUFpQixDQUFDLFFBQTRDO0FBRWxFLFFBQU0sU0FBUyxrQkFBa0IsaUJBQWlCLFFBQVEsa0JBQWtCLGlCQUFpQjtBQUc3RixNQUFJLENBQUM7QUFBUSxXQUFPO0FBR3BCLFFBQU0sa0JBQWtCLE9BQU8sT0FBTyxPQUFPO0FBRzdDLE1BQUksQ0FBQyxnQkFBZ0I7QUFBUyxXQUFPO0FBR3JDLFFBQU0sVUFBVSxLQUFLLE1BQU0sUUFBUSxLQUFLLE1BQU0sUUFBUSxVQU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