"use strict";
/* Socket Lib - Built with esbuild */
"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 detect_exports = {};
__export(detect_exports, {
  detectDlxExecutableType: () => detectDlxExecutableType,
  detectExecutableType: () => detectExecutableType,
  detectLocalExecutableType: () => detectLocalExecutableType,
  findPackageJson: () => findPackageJson,
  isJsFilePath: () => isJsFilePath,
  isNativeBinary: () => isNativeBinary,
  isNodePackage: () => isNodePackage,
  packageJsonPathCacheSet: () => packageJsonPathCacheSet,
  readPackageJson: () => readPackageJson
});
module.exports = __toCommonJS(detect_exports);
var import_paths = require("./paths");
var import_socket = require("../paths/socket");
var import_date = require("../primordials/date");
var import_json = require("../primordials/json");
var import_map_set = require("../primordials/map-set");
var import_fs = require("../node/fs");
var import_path = require("../node/path");
const NODE_JS_EXTENSIONS = new import_map_set.SetCtor([".js", ".mjs", ".cjs"]);
const PACKAGE_JSON_PATH_CACHE_MAX_SIZE = 200;
const PACKAGE_JSON_NEGATIVE_TTL_MS = 1e4;
const packageJsonPathCache = new import_map_set.MapCtor();
function packageJsonPathCacheSet(key, value) {
  if (packageJsonPathCache.has(key)) {
    packageJsonPathCache.delete(key);
  } else if (packageJsonPathCache.size >= PACKAGE_JSON_PATH_CACHE_MAX_SIZE) {
    const oldest = packageJsonPathCache.keys().next().value;
    if (oldest !== void 0) {
      packageJsonPathCache.delete(oldest);
    }
  }
  packageJsonPathCache.set(key, { path: value, at: Date.now() });
}
const packageJsonContentCache = new import_map_set.MapCtor();
function findPackageJson(filePath) {
  const fs = (0, import_fs.getNodeFs)();
  const path = (0, import_path.getNodePath)();
  const startDir = path.dirname(path.resolve(filePath));
  const cached = packageJsonPathCache.get(startDir);
  if (cached !== void 0) {
    if (cached.path === void 0) {
      if ((0, import_date.DateNow)() - cached.at < PACKAGE_JSON_NEGATIVE_TTL_MS) {
        return void 0;
      }
      packageJsonPathCache.delete(startDir);
    } else if (fs.existsSync(cached.path)) {
      packageJsonPathCacheSet(startDir, cached.path);
      return cached.path;
    } else {
      packageJsonPathCache.delete(startDir);
    }
  }
  let currentDir = startDir;
  const root = path.parse(currentDir).root;
  while (currentDir !== root) {
    const packageJsonPath = path.join(currentDir, "package.json");
    if (fs.existsSync(packageJsonPath)) {
      packageJsonPathCacheSet(startDir, packageJsonPath);
      return packageJsonPath;
    }
    currentDir = path.dirname(currentDir);
  }
  packageJsonPathCacheSet(startDir, void 0);
  return void 0;
}
function readPackageJson(packageJsonPath) {
  const fs = (0, import_fs.getNodeFs)();
  let mtimeMs = 0;
  try {
    mtimeMs = fs.statSync(packageJsonPath).mtimeMs;
  } catch {
    packageJsonContentCache.delete(packageJsonPath);
    return void 0;
  }
  const cached = packageJsonContentCache.get(packageJsonPath);
  if (cached !== void 0 && cached.mtimeMs === mtimeMs) {
    return cached.content;
  }
  try {
    const content = (0, import_json.JSONParse)(fs.readFileSync(packageJsonPath, "utf8"));
    packageJsonContentCache.set(packageJsonPath, { mtimeMs, content });
    return content;
  } catch {
    packageJsonContentCache.set(packageJsonPath, {
      mtimeMs,
      content: void 0
    });
    return void 0;
  }
}
function detectDlxExecutableType(filePath) {
  const fs = (0, import_fs.getNodeFs)();
  const path = (0, import_path.getNodePath)();
  const dlxDir = (0, import_socket.getSocketDlxDir)();
  const absolutePath = path.resolve(filePath);
  const relativePath = path.relative(dlxDir, absolutePath);
  const cacheKey = relativePath.split(path.sep)[0] ?? "";
  const cacheDir = path.join(dlxDir, cacheKey);
  if (fs.existsSync(path.join(cacheDir, "node_modules"))) {
    return {
      type: "package",
      method: "dlx-cache",
      inDlxCache: true
    };
  }
  return {
    type: "binary",
    method: "dlx-cache",
    inDlxCache: true
  };
}
function detectExecutableType(filePath) {
  if ((0, import_paths.isInSocketDlx)(filePath)) {
    return detectDlxExecutableType(filePath);
  }
  return detectLocalExecutableType(filePath);
}
function detectLocalExecutableType(filePath) {
  const packageJsonPath = findPackageJson(filePath);
  if (packageJsonPath !== void 0) {
    const packageJson = readPackageJson(packageJsonPath);
    if (packageJson?.["bin"]) {
      return {
        type: "package",
        method: "package-json",
        packageJsonPath,
        inDlxCache: false
      };
    }
  }
  if (isJsFilePath(filePath)) {
    return {
      inDlxCache: false,
      method: "file-extension",
      type: "package"
    };
  }
  return {
    type: "binary",
    method: "file-extension",
    inDlxCache: false
  };
}
function isJsFilePath(filePath) {
  const path = (0, import_path.getNodePath)();
  const ext = path.extname(filePath).toLowerCase();
  return NODE_JS_EXTENSIONS.has(ext);
}
function isNativeBinary(filePath) {
  return detectExecutableType(filePath).type === "binary";
}
function isNodePackage(filePath) {
  return detectExecutableType(filePath).type === "package";
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  detectDlxExecutableType,
  detectExecutableType,
  detectLocalExecutableType,
  findPackageJson,
  isJsFilePath,
  isNativeBinary,
  isNodePackage,
  packageJsonPathCacheSet,
  readPackageJson
});
