"use strict";
/* Socket Lib - Built with rolldown */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_primordials_string = require('../primordials/string.js');
const require_node_fs = require('../node/fs.js');
const require_primordials_json = require('../primordials/json.js');

//#region src/fs/write-json.ts
/**
* @file JSON writers that match the wire-format conventions a tool ecosystem
*   expects: configurable indentation, configurable EOL, trailing newline by
*   default. The `stringify` helper is exported so callers that already have an
*   `fs` handle can render a buffer without round-tripping through this
*   module's writers.
*/
const NEWLINE_REGEX = /\n/g;
/**
* Stringify JSON with custom formatting options. Formats JSON with configurable
* line endings and indentation.
*
* @param json - Value to stringify.
* @param EOL - End-of-line sequence.
* @param finalEOL - Whether to add final newline.
* @param replacer - JSON replacer function.
* @param spaces - Indentation spaces or string.
*
* @returns Formatted JSON string
*/
/*@__NO_SIDE_EFFECTS__*/
function stringify(json, EOL, finalEOL, replacer, spaces = 2) {
	const EOF = finalEOL ? EOL : "";
	return `${require_primordials_string.StringPrototypeReplace(require_primordials_json.JSONStringify(json, replacer, spaces), NEWLINE_REGEX, EOL)}${EOF}`;
}
/**
* Write JSON content to a file asynchronously with formatting. Stringifies the
* value with configurable indentation and line endings. Automatically adds a
* final newline by default for POSIX compliance.
*
* @example
*   ;```ts
*   // Write formatted JSON with default 2-space indentation
*   await writeJson('./data.json', { name: 'example', version: '1.0.0' })
*
*   // Write with custom indentation
*   await writeJson('./config.json', config, { spaces: 4 })
*
*   // Write with tabs instead of spaces
*   await writeJson('./data.json', data, { spaces: '\t' })
*
*   // Write without final newline
*   await writeJson('./inline.json', obj, { finalEOL: false })
*
*   // Write with Windows line endings
*   await writeJson('./win.json', data, { EOL: '\r\n' })
*   ```
*
* @param filepath - Path to write to.
* @param jsonContent - Value to stringify and write.
* @param options - Write options including formatting and encoding.
*
* @returns Promise that resolves when write completes
*/
async function writeJson(filepath, jsonContent, options) {
	const { EOL, finalEOL, replacer, spaces, ...fsOptions } = {
		__proto__: null,
		...typeof options === "string" ? { encoding: options } : options
	};
	const fs = /* @__PURE__ */ require_node_fs.getNodeFs();
	const jsonString = /* @__PURE__ */ stringify(jsonContent, EOL || "\n", finalEOL !== void 0 ? finalEOL : true, replacer, spaces);
	await fs.promises.writeFile(filepath, jsonString, {
		encoding: "utf8",
		...fsOptions,
		__proto__: null
	});
}
/**
* Write JSON content to a file synchronously with formatting. Stringifies the
* value with configurable indentation and line endings. Automatically adds a
* final newline by default for POSIX compliance.
*
* @example
*   ;```ts
*   // Write formatted JSON with default 2-space indentation
*   writeJsonSync('./package.json', pkg)
*
*   // Write with custom indentation
*   writeJsonSync('./tsconfig.json', tsconfig, { spaces: 4 })
*
*   // Write with tabs for indentation
*   writeJsonSync('./data.json', data, { spaces: '\t' })
*
*   // Write compacted (no indentation)
*   writeJsonSync('./compact.json', data, { spaces: 0 })
*   ```
*
* @param filepath - Path to write to.
* @param jsonContent - Value to stringify and write.
* @param options - Write options including formatting and encoding.
*/
function writeJsonSync(filepath, jsonContent, options) {
	const { EOL, finalEOL, replacer, spaces, ...fsOptions } = {
		__proto__: null,
		...typeof options === "string" ? { encoding: options } : options
	};
	const fs = /* @__PURE__ */ require_node_fs.getNodeFs();
	const jsonString = /* @__PURE__ */ stringify(jsonContent, EOL || "\n", finalEOL !== void 0 ? finalEOL : true, replacer, spaces);
	fs.writeFileSync(filepath, jsonString, {
		encoding: "utf8",
		...fsOptions,
		__proto__: null
	});
}

//#endregion
exports.stringify = stringify;
exports.writeJson = writeJson;
exports.writeJsonSync = writeJsonSync;