"use strict";
/* Socket Lib - Built with rolldown */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_paths_predicates = require('../paths/predicates.js');
const require_primordials_json = require('../primordials/json.js');
const require_packages_normalize = require('./normalize.js');
const require_paths_packages = require('../paths/packages.js');
const require_packages_edit_class = require('./edit-class.js');

//#region src/packages/edit.ts
/**
* @file Editable package.json manipulation utilities. Convenience wrappers
*   around the `EditablePackageJson` class. Both the in-memory and path-backed
*   flows funnel through the same two entry points — pass `path` to persist,
*   omit it to stay in memory:
*
*   - `EditablePackageJsonInstance` — public interface for instances
*   - `toEditablePackageJson` — async (in-memory if no `path`, loads from disk if
*     `path` set)
*   - `toEditablePackageJsonSync` — sync version of the above The class factory
*     lives in a sibling leaf and is re-exported here so existing
*     `packages/edit` importers keep working unchanged:
*   - `getEditablePackageJsonClass` — `./edit-class` Lazy `node:fs` / `node:path`
*     / `node:util` loaders use the canonical `getNodeFs` / `getNodePath` /
*     `getNodeUtil` helpers from `@socketsecurity/lib/node/{fs,path,util}`.
*/
/**
* Convert package.json to editable instance with file persistence.
*
* @example
*   ;```typescript
*   const editable = await toEditablePackageJson(
*     { name: 'my-pkg', version: '1.0.0' },
*     { path: '/tmp/my-project' },
*   )
*   ```
*/
/*@__NO_SIDE_EFFECTS__*/
async function toEditablePackageJson(pkgJson, options) {
	const { path: filepath, ...restOptions } = {
		__proto__: null,
		...options
	};
	const { normalize, ...normalizeOptions } = restOptions;
	const EditablePackageJson = /* @__PURE__ */ require_packages_edit_class.getEditablePackageJsonClass();
	if (typeof filepath !== "string") return new EditablePackageJson().fromContent(normalize ? /* @__PURE__ */ require_packages_normalize.normalizePackageJson(pkgJson, normalizeOptions) : pkgJson);
	const pkgJsonPath = /* @__PURE__ */ require_paths_packages.resolvePackageJsonDirname(filepath);
	return (await EditablePackageJson.load(pkgJsonPath, { create: true })).fromJSON(`${require_primordials_json.JSONStringify(normalize ? /* @__PURE__ */ require_packages_normalize.normalizePackageJson(pkgJson, {
		.../* @__PURE__ */ require_paths_predicates.isNodeModules(pkgJsonPath) ? {} : { preserve: ["repository"] },
		...normalizeOptions
	}) : pkgJson, void 0, 2)}\n`);
}
/**
* Convert package.json to editable instance with file persistence
* synchronously.
*
* @example
*   ;```typescript
*   const editable = toEditablePackageJsonSync(
*     { name: 'my-pkg', version: '1.0.0' },
*     { path: '/tmp/my-project' },
*   )
*   ```
*/
/*@__NO_SIDE_EFFECTS__*/
function toEditablePackageJsonSync(pkgJson, options) {
	const { path: filepath, ...restOptions } = {
		__proto__: null,
		...options
	};
	const { normalize, ...normalizeOptions } = restOptions;
	const EditablePackageJson = /* @__PURE__ */ require_packages_edit_class.getEditablePackageJsonClass();
	if (typeof filepath !== "string") return new EditablePackageJson().fromContent(normalize ? /* @__PURE__ */ require_packages_normalize.normalizePackageJson(pkgJson, normalizeOptions) : pkgJson);
	const pkgJsonPath = /* @__PURE__ */ require_paths_packages.resolvePackageJsonDirname(filepath);
	return new EditablePackageJson().create(pkgJsonPath).fromJSON(`${require_primordials_json.JSONStringify(normalize ? /* @__PURE__ */ require_packages_normalize.normalizePackageJson(pkgJson, {
		.../* @__PURE__ */ require_paths_predicates.isNodeModules(pkgJsonPath) ? {} : { preserve: ["repository"] },
		...normalizeOptions
	}) : pkgJson, void 0, 2)}\n`);
}

//#endregion
exports.getEditablePackageJsonClass = require_packages_edit_class.getEditablePackageJsonClass;
exports.toEditablePackageJson = toEditablePackageJson;
exports.toEditablePackageJsonSync = toEditablePackageJsonSync;