"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_string = require('../primordials/string.js');
const require_primordials_array = require('../primordials/array.js');
const require_primordials_map_set = require('../primordials/map-set.js');
const require_primordials_object = require('../primordials/object.js');
const require_constants_sentinels = require('../constants/sentinels.js');
const require_arrays_predicates = require('../arrays/predicates.js');
const require_objects_predicates = require('../objects/predicates.js');

//#region src/packages/exports.ts
/**
* @file Package exports field utilities.
*/
/**
* Find types definition for a specific subpath in package exports.
*
* @example
*   ;```typescript
*   const exports = {
*     '.': { types: './dist/index.d.ts', import: './dist/index.js' },
*   }
*   const types = findTypesForSubpath(exports, './dist/index.js')
*   // types === './dist/index.d.ts'
*   ```
*/
/*@__NO_SIDE_EFFECTS__*/
function findTypesForSubpath(entryExports, subpath) {
	const queue = [entryExports];
	let pos = 0;
	while (pos < queue.length) {
		if (pos === 1e6) throw new require_primordials_error.ErrorCtor("Detected infinite loop in entry exports crawl of getTypesForSubpath");
		const value = queue[pos++];
		if (require_arrays_predicates.isArray(value)) for (let i = 0, { length } = value; i < length; i += 1) {
			const item = value[i];
			if (item === subpath) return value.types;
			if (/* @__PURE__ */ require_objects_predicates.isObject(item)) queue.push(item);
		}
		else if (/* @__PURE__ */ require_objects_predicates.isObject(value)) {
			const keys = require_primordials_object.ObjectGetOwnPropertyNames(value);
			for (let i = 0, { length } = keys; i < length; i += 1) {
				const item = value[keys[i]];
				if (item === subpath) return value.types;
				if (/* @__PURE__ */ require_objects_predicates.isObject(item)) queue.push(item);
			}
		}
	}
}
/**
* Get file paths from package exports.
*
* @example
*   ;```typescript
*   const exports = { '.': './dist/index.js', './utils': './dist/utils.js' }
*   getExportFilePaths(exports) // ['./dist/index.js', './dist/utils.js']
*   ```
*/
/*@__NO_SIDE_EFFECTS__*/
function getExportFilePaths(entryExports) {
	if (!/* @__PURE__ */ require_objects_predicates.isObject(entryExports)) return [];
	const paths = [];
	for (const key of require_primordials_object.ObjectGetOwnPropertyNames(entryExports)) {
		if (!require_primordials_string.StringPrototypeStartsWith(key, ".")) continue;
		const value = entryExports[key];
		if (typeof value === "string") require_primordials_array.ArrayPrototypePush(paths, value);
		else if (/* @__PURE__ */ require_objects_predicates.isObject(value)) for (const subKey of require_primordials_object.ObjectGetOwnPropertyNames(value)) {
			const subValue = value[subKey];
			if (typeof subValue === "string") require_primordials_array.ArrayPrototypePush(paths, subValue);
			else if (require_arrays_predicates.isArray(subValue)) {
				for (const item of subValue) if (typeof item === "string") require_primordials_array.ArrayPrototypePush(paths, item);
				else if (/* @__PURE__ */ require_objects_predicates.isObject(item)) for (const nestedKey of require_primordials_object.ObjectGetOwnPropertyNames(item)) {
					const nestedValue = item[nestedKey];
					if (typeof nestedValue === "string") require_primordials_array.ArrayPrototypePush(paths, nestedValue);
				}
			}
		}
	}
	return [...new require_primordials_map_set.SetCtor(paths)].filter((p) => require_primordials_string.StringPrototypeStartsWith(p, "./"));
}
/**
* Get subpaths from package exports.
*
* @example
*   ;```typescript
*   const exports = { '.': './index.js', './utils': './utils.js' }
*   getSubpaths(exports) // ['.', './utils']
*   ```
*/
/*@__NO_SIDE_EFFECTS__*/
function getSubpaths(entryExports) {
	if (!/* @__PURE__ */ require_objects_predicates.isObject(entryExports)) return [];
	return require_primordials_object.ObjectGetOwnPropertyNames(entryExports).filter((key) => require_primordials_string.StringPrototypeStartsWith(key, "."));
}
/**
* Check if package exports use conditional patterns (e.g., import/require).
*
* @example
*   ;```typescript
*   isConditionalExports({ import: './index.mjs', require: './index.cjs' }) // true
*   isConditionalExports({ '.': './index.js' }) // false
*   ```
*/
/*@__NO_SIDE_EFFECTS__*/
function isConditionalExports(entryExports) {
	if (!/* @__PURE__ */ require_objects_predicates.isPlainObject(entryExports)) return false;
	const keys = require_primordials_object.ObjectGetOwnPropertyNames(entryExports);
	const { length } = keys;
	if (!length) return false;
	for (let i = 0; i < length; i += 1) {
		const key = keys[i];
		if (key.length > 0 && require_primordials_string.StringPrototypeCharCodeAt(key, 0) === 46) return false;
	}
	return true;
}
/**
* Check if package exports use subpath patterns (keys starting with '.').
*
* @example
*   ;```typescript
*   isSubpathExports({ '.': './index.js', './utils': './utils.js' }) // true
*   isSubpathExports({ import: './index.mjs' }) // false
*   ```
*/
/*@__NO_SIDE_EFFECTS__*/
function isSubpathExports(entryExports) {
	if (/* @__PURE__ */ require_objects_predicates.isPlainObject(entryExports)) {
		const keys = require_primordials_object.ObjectGetOwnPropertyNames(entryExports);
		for (let i = 0, { length } = keys; i < length; i += 1) if (keys[i]?.charCodeAt(0) === 46) return true;
	}
	return false;
}
/**
* Normalize package.json exports field to canonical format.
*
* @example
*   ;```typescript
*   resolvePackageJsonEntryExports('./index.js')
*   // { '.': './index.js' }
*
*   resolvePackageJsonEntryExports({ '.': './index.js' })
*   // { '.': './index.js' }
*   ```
*/
/*@__NO_SIDE_EFFECTS__*/
function resolvePackageJsonEntryExports(entryExports) {
	if (typeof entryExports === "string" || require_arrays_predicates.isArray(entryExports)) return { ".": entryExports };
	if (/* @__PURE__ */ isConditionalExports(entryExports)) return entryExports;
	return /* @__PURE__ */ require_objects_predicates.isObject(entryExports) ? entryExports : void 0;
}

//#endregion
exports.findTypesForSubpath = findTypesForSubpath;
exports.getExportFilePaths = getExportFilePaths;
exports.getSubpaths = getSubpaths;
exports.isConditionalExports = isConditionalExports;
exports.isSubpathExports = isSubpathExports;
exports.resolvePackageJsonEntryExports = resolvePackageJsonEntryExports;