"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 packages_exports = {};
__export(packages_exports, {
  isDlxPackageInstalled: () => isDlxPackageInstalled,
  listDlxPackages: () => listDlxPackages,
  listDlxPackagesAsync: () => listDlxPackagesAsync,
  removeDlxPackage: () => removeDlxPackage,
  removeDlxPackageSync: () => removeDlxPackageSync
});
module.exports = __toCommonJS(packages_exports);
var import_read_dir = require("../fs/read-dir");
var import_safe = require("../fs/safe");
var import_socket = require("../paths/socket");
var import_paths = require("./paths");
var import_array = require("../primordials/array");
var import_error = require("../primordials/error");
var import_fs = require("../node/fs");
function isDlxPackageInstalled(packageName) {
  const fs = (0, import_fs.getNodeFs)();
  return fs.existsSync((0, import_paths.getDlxInstalledPackageDir)(packageName));
}
function listDlxPackages() {
  try {
    return (0, import_read_dir.readDirNamesSync)((0, import_socket.getSocketDlxDir)(), { sort: true });
  } catch {
    return [];
  }
}
async function listDlxPackagesAsync() {
  const fs = (0, import_fs.getNodeFs)();
  try {
    const entries = await fs.promises.readdir((0, import_socket.getSocketDlxDir)(), {
      withFileTypes: true
    });
    return (0, import_array.ArrayPrototypeFilter)(entries, (e) => e.isDirectory()).map((e) => e.name).sort();
  } catch {
    return [];
  }
}
async function removeDlxPackage(packageName) {
  const packageDir = (0, import_paths.getDlxPackageDir)(packageName);
  try {
    await (0, import_safe.safeDelete)(packageDir, { recursive: true, force: true });
  } catch (e) {
    throw new import_error.ErrorCtor(`Failed to remove DLX package "${packageName}"`, {
      cause: e
    });
  }
}
function removeDlxPackageSync(packageName) {
  const packageDir = (0, import_paths.getDlxPackageDir)(packageName);
  try {
    (0, import_safe.safeDeleteSync)(packageDir, { recursive: true, force: true });
  } catch (e) {
    const code = e.code;
    if (code === "EACCES" || code === "EPERM") {
      throw new import_error.ErrorCtor(
        `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 import_error.ErrorCtor(
        `Cannot remove DLX package "${packageName}" from read-only filesystem
Directory: ${packageDir}
The filesystem is mounted read-only.`,
        { cause: e }
      );
    }
    throw new import_error.ErrorCtor(
      `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 = {
  isDlxPackageInstalled,
  listDlxPackages,
  listDlxPackagesAsync,
  removeDlxPackage,
  removeDlxPackageSync
});
