"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_error = require('../primordials/error.js');
const require_primordials_string = require('../primordials/string.js');
const require_node_fs = require('../node/fs.js');
const require_node_path = require('../node/path.js');
const require_primordials_json = require('../primordials/json.js');
const require_node_util = require('../node/util.js');
const require_json_format = require('../json/format.js');
let src_external__npmcli_package_json = require("../external/@npmcli/package-json");
src_external__npmcli_package_json = require_runtime.__toESM(src_external__npmcli_package_json);
let src_external__npmcli_package_json_lib_read_package = require("../external/@npmcli/package-json/lib/read-package");
let src_external__npmcli_package_json_lib_sort = require("../external/@npmcli/package-json/lib/sort");

//#region src/packages/edit-class.ts
/**
* @file The `EditablePackageJson` class factory. Split out of
*   `packages/edit.ts` for size hygiene. Wraps `@npmcli/package-json` with
*   Socket-specific knobs: path tracking, a `_canSave` guard for content-only
*   instances, async + sync save paths that honor `EditablePackageJsonOptions`
*   formatting.
*
*   - `getEditablePackageJsonClass` — lazy class accessor (Webpack-safe)
*/
let _EditablePackageJsonClass;
/**
* Get the EditablePackageJson class for package.json manipulation.
*
* @example
*   ;```typescript
*   const EditablePackageJson = getEditablePackageJsonClass()
*   const pkg = await EditablePackageJson.load('/tmp/my-project')
*   console.log(pkg.content.name)
*   ```
*/
/*@__NO_SIDE_EFFECTS__*/
function getEditablePackageJsonClass() {
	if (_EditablePackageJsonClass === void 0) _EditablePackageJsonClass = class EditablePackageJson extends src_external__npmcli_package_json.default {
		static fixSteps = src_external__npmcli_package_json.default.fixSteps;
		static normalizeSteps = src_external__npmcli_package_json.default.normalizeSteps;
		static prepareSteps = src_external__npmcli_package_json.default.prepareSteps;
		_canSave = true;
		_path = void 0;
		_readFileContent = "";
		_readFileJson = void 0;
		get content() {
			return super.content;
		}
		get filename() {
			const path = this._path;
			if (!path) return "";
			if (require_primordials_string.StringPrototypeEndsWith(path, "package.json")) return path;
			return (/* @__PURE__ */ require_node_path.getNodePath()).join(path, "package.json");
		}
		static async create(path, opts = {}) {
			const p = new _EditablePackageJsonClass();
			await p.create(path);
			return opts.data ? p.update(opts.data) : p;
		}
		static async fix(path, opts) {
			const p = new _EditablePackageJsonClass();
			await p.load(path, true);
			return await p.fix(opts);
		}
		static async load(path, opts = {}) {
			const p = new _EditablePackageJsonClass();
			if (!opts.create) return await p.load(path);
			try {
				return await p.load(path);
			} catch (err) {
				if (!err.message.startsWith("Could not read package.json")) throw err;
				return p.create(path);
			}
		}
		static async normalize(path, opts) {
			const p = new _EditablePackageJsonClass();
			await p.load(path);
			return await p.normalize(opts);
		}
		static async prepare(path, opts) {
			const p = new _EditablePackageJsonClass();
			await p.load(path, true);
			return await p.prepare(opts);
		}
		create(path) {
			super.create(path);
			this._path = path;
			return this;
		}
		async fix(opts = {}) {
			await super.fix(opts);
			return this;
		}
		fromContent(data) {
			super.fromContent(data);
			this._canSave = false;
			return this;
		}
		fromJSON(data) {
			super.fromJSON(data);
			return this;
		}
		async load(path, create) {
			this._path = path;
			const { promises: fsPromises } = /* @__PURE__ */ require_node_fs.getNodeFs();
			let parseErr;
			try {
				this._readFileContent = await (0, src_external__npmcli_package_json_lib_read_package.read)(this.filename);
			} catch (e) {
				if (!create) throw e;
				parseErr = e;
			}
			if (parseErr) {
				const indexFile = (/* @__PURE__ */ require_node_path.getNodePath()).resolve(this.path || "", "index.js");
				let indexFileContent;
				try {
					indexFileContent = await fsPromises.readFile(indexFile, "utf8");
				} catch {
					throw parseErr;
				}
				try {
					this.fromContent(indexFileContent);
				} catch {
					throw parseErr;
				}
				this._canSave = false;
				return this;
			}
			this.fromJSON(this._readFileContent);
			this._readFileJson = (0, src_external__npmcli_package_json_lib_read_package.parse)(this._readFileContent);
			return this;
		}
		async normalize(opts = {}) {
			await super.normalize(opts);
			return this;
		}
		get path() {
			return this._path;
		}
		async prepare(opts = {}) {
			await super.prepare(opts);
			return this;
		}
		async save(options) {
			if (!this._canSave || this.content === void 0) throw new require_primordials_error.ErrorCtor("No package.json to save to");
			if (!require_json_format.shouldSave(this.content, this._readFileJson, this._readFileContent, {
				...options,
				sortFn: options?.sort ? src_external__npmcli_package_json_lib_sort.packageSort : void 0
			})) return false;
			const content = require_json_format.stripFormattingSymbols(this.content);
			const fileContent = require_json_format.stringifyWithFormatting(options?.sort ? (0, src_external__npmcli_package_json_lib_sort.packageSort)(content) : content, require_json_format.getFormattingFromContent(this.content));
			const { promises: fsPromises } = /* @__PURE__ */ require_node_fs.getNodeFs();
			await fsPromises.writeFile(this.filename, fileContent);
			this._readFileContent = fileContent;
			this._readFileJson = (0, src_external__npmcli_package_json_lib_read_package.parse)(fileContent);
			return true;
		}
		saveSync(options) {
			if (!this._canSave || this.content === void 0) throw new require_primordials_error.ErrorCtor("No package.json to save to");
			const { ignoreWhitespace = false, sort = false } = {
				__proto__: null,
				...options
			};
			const { [Symbol.for("indent")]: indent, [Symbol.for("newline")]: newline, ...rest } = this.content;
			const content = sort ? (0, src_external__npmcli_package_json_lib_sort.packageSort)(rest) : rest;
			if (ignoreWhitespace && (/* @__PURE__ */ require_node_util.getNodeUtil()).isDeepStrictEqual(content, this._readFileJson)) return false;
			const format = indent === void 0 || indent === null ? "  " : indent;
			const eol = newline === void 0 || newline === null ? "\n" : newline;
			const fileContent = `${require_primordials_json.JSONStringify(content, void 0, format)}\n`.replace(/\n/g, eol);
			if (!ignoreWhitespace && fileContent.trim() === this._readFileContent.trim()) return false;
			(/* @__PURE__ */ require_node_fs.getNodeFs()).writeFileSync(this.filename, fileContent);
			this._readFileContent = fileContent;
			this._readFileJson = (0, src_external__npmcli_package_json_lib_read_package.parse)(fileContent);
			return true;
		}
		update(content) {
			super.update(content);
			return this;
		}
		willSave(options) {
			const { ignoreWhitespace = false, sort = false } = {
				__proto__: null,
				...options
			};
			if (!this._canSave || this.content === void 0) return false;
			const { [Symbol.for("indent")]: indent, [Symbol.for("newline")]: newline, ...rest } = this.content;
			const content = sort ? (0, src_external__npmcli_package_json_lib_sort.packageSort)(rest) : rest;
			if (ignoreWhitespace && (/* @__PURE__ */ require_node_util.getNodeUtil()).isDeepStrictEqual(content, this._readFileJson)) return false;
			const format = indent === void 0 || indent === null ? "  " : indent;
			const eol = newline === void 0 || newline === null ? "\n" : newline;
			const fileContent = `${require_primordials_json.JSONStringify(content, void 0, format)}\n`.replace(/\n/g, eol);
			if (!ignoreWhitespace && fileContent.trim() === this._readFileContent.trim()) return false;
			return true;
		}
	};
	return _EditablePackageJsonClass;
}

//#endregion
exports.getEditablePackageJsonClass = getEditablePackageJsonClass;