"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/core-node-modules.ts
var core_node_modules_exports = {};
__export(core_node_modules_exports, {
  linkCoreNodeModules: () => linkCoreNodeModules,
  resolveInstalledCoreNodeModules: () => resolveInstalledCoreNodeModules
});
module.exports = __toCommonJS(core_node_modules_exports);
var import_fs = __toESM(require("fs"), 1);
var import_node_path = __toESM(require("path"), 1);
var isDirectory = (directory) => {
  try {
    return import_fs.default.statSync(directory).isDirectory();
  } catch (error) {
    return false;
  }
};
var hasAstroDependency = (nodeModulesDirectory) => {
  const astroBin = process.platform === "win32" ? "astro.cmd" : "astro";
  return import_fs.default.existsSync(import_node_path.default.join(nodeModulesDirectory, "astro", "package.json")) || import_fs.default.existsSync(import_node_path.default.join(nodeModulesDirectory, ".bin", astroBin));
};
var isSymbolicLink = (targetPath) => {
  try {
    return import_fs.default.lstatSync(targetPath).isSymbolicLink();
  } catch (error) {
    return false;
  }
};
var isSameDirectory = (a, b) => {
  try {
    return import_fs.default.realpathSync.native(a) === import_fs.default.realpathSync.native(b);
  } catch (error) {
    return false;
  }
};
var resolveInstalledCoreNodeModules = (currentDir) => {
  const candidates = [];
  const seen = /* @__PURE__ */ new Set();
  const addCandidate = (candidate) => {
    const resolvedCandidate = import_node_path.default.resolve(candidate);
    if (!seen.has(resolvedCandidate)) {
      candidates.push(resolvedCandidate);
      seen.add(resolvedCandidate);
    }
  };
  addCandidate(import_node_path.default.resolve(currentDir, "..", "node_modules"));
  let directory = import_node_path.default.resolve(currentDir);
  while (true) {
    if (import_node_path.default.basename(directory) === "node_modules") {
      addCandidate(directory);
    }
    addCandidate(import_node_path.default.join(directory, "node_modules"));
    const parentDirectory = import_node_path.default.dirname(directory);
    if (parentDirectory === directory) {
      break;
    }
    directory = parentDirectory;
  }
  return candidates.find((candidate) => isDirectory(candidate) && hasAstroDependency(candidate)) ?? candidates.find(isDirectory);
};
var linkCoreNodeModules = ({
  coreNodeModules,
  installedCoreNodeModules
}) => {
  if (!installedCoreNodeModules) return;
  if (isSymbolicLink(coreNodeModules)) {
    if (isSameDirectory(coreNodeModules, installedCoreNodeModules)) {
      return;
    }
    import_fs.default.unlinkSync(coreNodeModules);
  } else if (import_fs.default.existsSync(coreNodeModules)) {
    return;
  }
  import_fs.default.symlinkSync(installedCoreNodeModules, coreNodeModules, process.platform === "win32" ? "junction" : "dir");
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  linkCoreNodeModules,
  resolveInstalledCoreNodeModules
});
