"use strict";
/* Socket Lib - Built with esbuild */
"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);
var repo_exports = {};
__export(repo_exports, {
  findGitRoot: () => findGitRoot,
  getCachedRealpath: () => getCachedRealpath,
  getCwd: () => getCwd,
  getFs: () => import_fs2.getNodeFs,
  getPath: () => import_path2.getNodePath,
  gitRootCache: () => gitRootCache,
  realpathCache: () => realpathCache
});
module.exports = __toCommonJS(repo_exports);
var import_node_process = __toESM(require("node:process"));
var import_map_set = require("../primordials/map-set");
var import_fs = require("../node/fs");
var import_path = require("../node/path");
var import_fs2 = require("../node/fs");
var import_path2 = require("../node/path");
const realpathCache = new import_map_set.MapCtor();
const gitRootCache = new import_map_set.MapCtor();
function findGitRoot(startPath) {
  const fs = (0, import_fs.getNodeFs)();
  const path = (0, import_path.getNodePath)();
  const cached = gitRootCache.get(startPath);
  if (cached) {
    if (fs.existsSync(path.join(cached, ".git"))) {
      return cached;
    }
    gitRootCache.delete(startPath);
  }
  let currentPath = startPath;
  while (true) {
    try {
      const gitPath = path.join(currentPath, ".git");
      if (fs.existsSync(gitPath)) {
        gitRootCache.set(startPath, currentPath);
        return currentPath;
      }
    } catch {
    }
    const parentPath = path.dirname(currentPath);
    if (parentPath === currentPath) {
      return startPath;
    }
    currentPath = parentPath;
  }
}
function getCachedRealpath(pathname) {
  const fs = (0, import_fs.getNodeFs)();
  const cached = realpathCache.get(pathname);
  if (cached) {
    if (fs.existsSync(cached)) {
      return cached;
    }
    realpathCache.delete(pathname);
  }
  let resolved;
  try {
    resolved = fs.realpathSync(pathname);
  } catch (e) {
    const code = e.code;
    if (code === "ENOENT" || code === "ENOTDIR") {
      throw e;
    }
    resolved = pathname;
  }
  realpathCache.set(pathname, resolved);
  return resolved;
}
function getCwd() {
  return getCachedRealpath(import_node_process.default.cwd());
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  findGitRoot,
  getCachedRealpath,
  getCwd,
  getFs,
  getPath,
  gitRootCache,
  realpathCache
});
