"use strict";
/* Socket Lib - Built with rolldown */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('../_virtual/_rolldown/runtime.js');
const require_primordials_object = require('../primordials/object.js');
const require_process_abort = require('../process/abort.js');
const require_arrays_predicates = require('../arrays/predicates.js');
const require_objects_predicates = require('../objects/predicates.js');
const require_constants_socket = require('../constants/socket.js');
const require_constants_packages = require('../constants/packages.js');
const require_objects_sort = require('../objects/sort.js');
const require_packages_exports = require('./exports.js');
const require_packages_validation = require('./validation.js');
let src_external_semver = require("../external/semver");
src_external_semver = require_runtime.__toESM(src_external_semver);
let src_external_pacote = require("../external/pacote");
src_external_pacote = require_runtime.__toESM(src_external_pacote);
let src_external_npm_package_arg = require("../external/npm-package-arg");
src_external_npm_package_arg = require_runtime.__toESM(src_external_npm_package_arg);

//#region src/packages/manifest.ts
/**
* @file Package manifest and packument fetching utilities.
*/
const abortSignal = require_process_abort.getAbortSignal();
const packageDefaultNodeRange = /* @__PURE__ */ require_constants_packages.getPackageDefaultNodeRange();
const PACKAGE_DEFAULT_SOCKET_CATEGORIES = /* @__PURE__ */ require_constants_packages.getPackageDefaultSocketCategories();
const packumentCache = /* @__PURE__ */ require_constants_packages.getPackumentCache();
const pkgScopePrefixRegExp = /^@socketregistry\//;
/**
* Create a package.json object for a Socket registry package.
*
* @example
*   ;```typescript
*   const pkgJson = createPackageJson('is-number', 'packages/npm/is-number', {
*     version: '1.0.0',
*     description: 'Check if a value is a number',
*   })
*   ```
*/
/*@__NO_SIDE_EFFECTS__*/
function createPackageJson(sockRegPkgName, directory, options) {
	const { dependencies, description, engines, exports: entryExportsRaw, files, keywords, main, overrides, resolutions, sideEffects, socket, type, version } = {
		__proto__: null,
		...options
	};
	const name = `@socketregistry/${sockRegPkgName.replace(pkgScopePrefixRegExp, "")}`;
	const entryExports = /* @__PURE__ */ require_packages_exports.resolvePackageJsonEntryExports(entryExportsRaw);
	const githubUrl = `https://github.com/${require_constants_socket.SOCKET_GITHUB_ORG}/${require_constants_socket.SOCKET_REGISTRY_REPO_NAME}`;
	return {
		__proto__: null,
		name,
		version,
		license: "MIT",
		description,
		keywords,
		homepage: `${githubUrl}/tree/main/${directory}`,
		repository: {
			type: "git",
			url: `git+${githubUrl}.git`,
			directory
		},
		...type ? { type } : {},
		.../* @__PURE__ */ require_objects_predicates.isPlainObject(entryExports) ? { exports: { ...entryExports } } : {},
		...entryExports ? {} : { main: `${main ?? "./index.js"}` },
		sideEffects: sideEffects !== void 0 && !!sideEffects,
		.../* @__PURE__ */ require_objects_predicates.isPlainObject(dependencies) ? { dependencies: { ...dependencies } } : {},
		.../* @__PURE__ */ require_objects_predicates.isPlainObject(overrides) ? { overrides: { ...overrides } } : {},
		.../* @__PURE__ */ require_objects_predicates.isPlainObject(resolutions) ? { resolutions: { ...resolutions } } : {},
		.../* @__PURE__ */ require_objects_predicates.isPlainObject(engines) ? { engines: require_primordials_object.ObjectFromEntries((/* @__PURE__ */ require_objects_sort.objectEntries(engines)).map((pair) => {
			const strKey = String(pair[0]);
			const result = [strKey, pair[1]];
			if (strKey === "node") {
				const { 1: range } = result;
				if (typeof range === "string" && range && packageDefaultNodeRange) {
					const coercedRange = src_external_semver.default.coerce(range);
					if (!src_external_semver.default.satisfies(coercedRange?.version ?? "0.0.0", packageDefaultNodeRange)) result[1] = packageDefaultNodeRange;
				}
			}
			return result;
		})) } : { engines: { node: packageDefaultNodeRange } },
		files: require_arrays_predicates.isArray(files) ? files.slice() : ["*.d.ts", "*.js"],
		.../* @__PURE__ */ require_objects_predicates.isPlainObject(socket) ? { socket: { ...socket } } : { socket: { categories: PACKAGE_DEFAULT_SOCKET_CATEGORIES } }
	};
}
/**
* Fetch the manifest for a package.
*
* @example
*   ;```typescript
*   const manifest = await fetchPackageManifest('lodash@4.17.21')
*   ```
*/
/*@__NO_SIDE_EFFECTS__*/
async function fetchPackageManifest(pkgNameOrId, options) {
	const pacoteOptions = {
		__proto__: null,
		signal: abortSignal,
		...options,
		packumentCache,
		preferOffline: true
	};
	const { signal } = pacoteOptions;
	if (signal?.aborted) return;
	let result;
	try {
		result = await src_external_pacote.default.manifest(pkgNameOrId, pacoteOptions);
	} catch {}
	if (signal?.aborted) return;
	if (result) {
		if (/* @__PURE__ */ require_packages_validation.isRegistryFetcherType((0, src_external_npm_package_arg.default)(pkgNameOrId, pacoteOptions.where).type)) return result;
	}
	if (result) {
		const typedResult = result;
		return await /* @__PURE__ */ fetchPackageManifest(`${typedResult.name}@${typedResult.version}`, pacoteOptions);
	}
}
/**
* Fetch the packument (package document) for a package.
*
* @example
*   ;```typescript
*   const packument = await fetchPackagePackument('lodash')
*   ```
*/
/*@__NO_SIDE_EFFECTS__*/
async function fetchPackagePackument(pkgNameOrId, options) {
	try {
		return await src_external_pacote.default.packument(pkgNameOrId, {
			__proto__: null,
			signal: abortSignal,
			...options,
			packumentCache,
			preferOffline: true
		});
	} catch {}
}

//#endregion
exports.createPackageJson = createPackageJson;
exports.fetchPackageManifest = fetchPackageManifest;
exports.fetchPackagePackument = fetchPackagePackument;