"use strict";
/* Socket Lib - Built with esbuild */
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 dlx_exports = {};
__export(dlx_exports, {
  clearDlx: () => clearDlx,
  clearDlxSync: () => clearDlxSync,
  dlxDirExists: () => dlxDirExists,
  dlxDirExistsAsync: () => dlxDirExistsAsync,
  ensureDlxDir: () => ensureDlxDir,
  ensureDlxDirSync: () => ensureDlxDirSync,
  generateCacheKey: () => generateCacheKey,
  getDlxInstalledPackageDir: () => getDlxInstalledPackageDir,
  getDlxPackageDir: () => getDlxPackageDir,
  getDlxPackageJsonPath: () => getDlxPackageJsonPath,
  getDlxPackageNodeModulesDir: () => getDlxPackageNodeModulesDir,
  isDlxPackageInstalled: () => isDlxPackageInstalled,
  isDlxPackageInstalledAsync: () => isDlxPackageInstalledAsync,
  isInSocketDlx: () => isInSocketDlx,
  listDlxPackages: () => listDlxPackages,
  listDlxPackagesAsync: () => listDlxPackagesAsync,
  removeDlxPackage: () => removeDlxPackage,
  removeDlxPackageSync: () => removeDlxPackageSync
});
module.exports = __toCommonJS(dlx_exports);
var import_crypto = require("crypto");
var import_fs = require("./fs");
var import_normalize = require("./paths/normalize");
var import_socket = require("./paths/socket");
var import_promises = require("./promises");
let _fs;
// @__NO_SIDE_EFFECTS__
function getFs() {
  if (_fs === void 0) {
    _fs = require("node:fs");
  }
  return _fs;
}
function generateCacheKey(spec) {
  return (0, import_crypto.createHash)("sha512").update(spec).digest("hex").substring(0, 16);
}
let _path;
// @__NO_SIDE_EFFECTS__
function getPath() {
  if (_path === void 0) {
    _path = require("node:path");
  }
  return _path;
}
async function clearDlx() {
  const packages = await listDlxPackagesAsync();
  await (0, import_promises.pEach)(packages, (pkg) => removeDlxPackage(pkg));
}
function clearDlxSync() {
  const packages = listDlxPackages();
  for (const pkg of packages) {
    removeDlxPackageSync(pkg);
  }
}
function dlxDirExists() {
  const fs = /* @__PURE__ */ getFs();
  return fs.existsSync((0, import_socket.getSocketDlxDir)());
}
async function dlxDirExistsAsync() {
  const fs = /* @__PURE__ */ getFs();
  try {
    await fs.promises.access((0, import_socket.getSocketDlxDir)());
    return true;
  } catch {
    return false;
  }
}
async function ensureDlxDir() {
  await (0, import_fs.safeMkdir)((0, import_socket.getSocketDlxDir)());
}
function ensureDlxDirSync() {
  (0, import_fs.safeMkdirSync)((0, import_socket.getSocketDlxDir)());
}
function getDlxInstalledPackageDir(packageName) {
  const path = /* @__PURE__ */ getPath();
  return (0, import_normalize.normalizePath)(
    path.join(getDlxPackageNodeModulesDir(packageName), packageName)
  );
}
function getDlxPackageDir(packageName) {
  const path = /* @__PURE__ */ getPath();
  return (0, import_normalize.normalizePath)(path.join((0, import_socket.getSocketDlxDir)(), packageName));
}
function getDlxPackageJsonPath(packageName) {
  const path = /* @__PURE__ */ getPath();
  return (0, import_normalize.normalizePath)(
    path.join(getDlxInstalledPackageDir(packageName), "package.json")
  );
}
function getDlxPackageNodeModulesDir(packageName) {
  const path = /* @__PURE__ */ getPath();
  return (0, import_normalize.normalizePath)(path.join(getDlxPackageDir(packageName), "node_modules"));
}
function isInSocketDlx(filePath) {
  if (!filePath) {
    return false;
  }
  const path = /* @__PURE__ */ getPath();
  const dlxDir = (0, import_socket.getSocketDlxDir)();
  const absolutePath = (0, import_normalize.normalizePath)(path.resolve(filePath));
  return absolutePath.startsWith(`${dlxDir}/`);
}
function isDlxPackageInstalled(packageName) {
  const fs = /* @__PURE__ */ getFs();
  return fs.existsSync(getDlxInstalledPackageDir(packageName));
}
async function isDlxPackageInstalledAsync(packageName) {
  const fs = /* @__PURE__ */ getFs();
  try {
    await fs.promises.access(getDlxInstalledPackageDir(packageName));
    return true;
  } catch {
    return false;
  }
}
function listDlxPackages() {
  try {
    return (0, import_fs.readDirNamesSync)((0, import_socket.getSocketDlxDir)(), { sort: true });
  } catch {
    return [];
  }
}
async function listDlxPackagesAsync() {
  const fs = /* @__PURE__ */ getFs();
  try {
    const entries = await fs.promises.readdir((0, import_socket.getSocketDlxDir)(), {
      withFileTypes: true
    });
    return entries.filter((e) => e.isDirectory()).map((e) => e.name).sort();
  } catch {
    return [];
  }
}
async function removeDlxPackage(packageName) {
  const packageDir = getDlxPackageDir(packageName);
  try {
    await (0, import_fs.safeDelete)(packageDir, { recursive: true, force: true });
  } catch (e) {
    throw new Error(`Failed to remove DLX package "${packageName}"`, {
      cause: e
    });
  }
}
function removeDlxPackageSync(packageName) {
  const fs = /* @__PURE__ */ getFs();
  const packageDir = getDlxPackageDir(packageName);
  try {
    fs.rmSync(packageDir, { recursive: true, force: true });
  } catch (e) {
    const code = e.code;
    if (code === "EACCES" || code === "EPERM") {
      throw new Error(
        `Permission denied removing DLX package "${packageName}"
Directory: ${packageDir}
To resolve:
  1. Check file/directory permissions
  2. Close any programs using files in this directory
  3. Try running with elevated privileges if necessary
  4. Manually remove: rm -rf "${packageDir}"`,
        { cause: e }
      );
    }
    if (code === "EROFS") {
      throw new Error(
        `Cannot remove DLX package "${packageName}" from read-only filesystem
Directory: ${packageDir}
The filesystem is mounted read-only.`,
        { cause: e }
      );
    }
    throw new Error(
      `Failed to remove DLX package "${packageName}"
Directory: ${packageDir}
Check permissions and ensure no programs are using this directory.`,
      { cause: e }
    );
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  clearDlx,
  clearDlxSync,
  dlxDirExists,
  dlxDirExistsAsync,
  ensureDlxDir,
  ensureDlxDirSync,
  generateCacheKey,
  getDlxInstalledPackageDir,
  getDlxPackageDir,
  getDlxPackageJsonPath,
  getDlxPackageNodeModulesDir,
  isDlxPackageInstalled,
  isDlxPackageInstalledAsync,
  isInSocketDlx,
  listDlxPackages,
  listDlxPackagesAsync,
  removeDlxPackage,
  removeDlxPackageSync
});
