"use strict";
/* Socket Lib - Built with rolldown */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_primordials_error = require('../primordials/error.js');
const require_primordials_array = require('../primordials/array.js');
const require_node_fs = require('../node/fs.js');
const require_paths_socket = require('../paths/socket.js');
const require_fs_safe = require('../fs/safe.js');
const require_dlx_paths = require('./paths.js');
const require_fs_read_dir = require('../fs/read-dir.js');

//#region src/dlx/packages.ts
/**
* @file Package management utilities for DLX installations.
*/
/**
* Check if a package is installed in DLX.
*
* @example
*   ;```typescript
*   if (isDlxPackageInstalled('prettier')) {
*     console.log('prettier is installed')
*   }
*   ```
*/
function isDlxPackageInstalled(packageName) {
	return (/* @__PURE__ */ require_node_fs.getNodeFs()).existsSync(require_dlx_paths.getDlxInstalledPackageDir(packageName));
}
/**
* List all packages installed in DLX.
*
* @example
*   ;```typescript
*   const packages = listDlxPackages()
*   console.log('Installed:', packages.join(', '))
*   ```
*/
function listDlxPackages() {
	try {
		return /* @__PURE__ */ require_fs_read_dir.readDirNamesSync(require_paths_socket.getSocketDlxDir(), { sort: true });
	} catch {
		return [];
	}
}
/**
* List all packages installed in DLX asynchronously.
*
* @example
*   ;```typescript
*   const packages = await listDlxPackagesAsync()
*   console.log('Installed:', packages.join(', '))
*   ```
*/
async function listDlxPackagesAsync() {
	const fs = /* @__PURE__ */ require_node_fs.getNodeFs();
	try {
		return require_primordials_array.ArrayPrototypeFilter(await fs.promises.readdir(require_paths_socket.getSocketDlxDir(), { withFileTypes: true }), (e) => e.isDirectory()).map((e) => e.name).sort();
	} catch {
		return [];
	}
}
/**
* Remove a DLX package installation.
*
* @example
*   ;```typescript
*   await removeDlxPackage('prettier')
*   ```
*/
async function removeDlxPackage(packageName) {
	const packageDir = require_dlx_paths.getDlxPackageDir(packageName);
	try {
		await require_fs_safe.safeDelete(packageDir, {
			recursive: true,
			force: true
		});
	} catch (e) {
		throw new require_primordials_error.ErrorCtor(`Failed to remove DLX package "${packageName}"`, { cause: e });
	}
}
/**
* Remove a DLX package installation synchronously.
*
* @example
*   ;```typescript
*   removeDlxPackageSync('prettier')
*   ```
*/
function removeDlxPackageSync(packageName) {
	const packageDir = require_dlx_paths.getDlxPackageDir(packageName);
	try {
		require_fs_safe.safeDeleteSync(packageDir, {
			recursive: true,
			force: true
		});
	} catch (e) {
		const code = e.code;
		if (code === "EACCES" || code === "EPERM") throw new require_primordials_error.ErrorCtor(`Permission denied removing DLX package "${packageName}"\nDirectory: ${packageDir}\nTo 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 require_primordials_error.ErrorCtor(`Cannot remove DLX package "${packageName}" from read-only filesystem\nDirectory: ${packageDir}\nThe filesystem is mounted read-only.`, { cause: e });
		throw new require_primordials_error.ErrorCtor(`Failed to remove DLX package "${packageName}"\nDirectory: ${packageDir}\nCheck permissions and ensure no programs are using this directory.`, { cause: e });
	}
}

//#endregion
exports.isDlxPackageInstalled = isDlxPackageInstalled;
exports.listDlxPackages = listDlxPackages;
exports.listDlxPackagesAsync = listDlxPackagesAsync;
exports.removeDlxPackage = removeDlxPackage;
exports.removeDlxPackageSync = removeDlxPackageSync;