"use strict";
/* Socket Lib - Built with rolldown */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_primordials_buffer = require('../primordials/buffer.js');
const require_node_fs = require('../node/fs.js');
const require_process_abort = require('../process/abort.js');
const require_fs_encoding = require('./encoding.js');

//#region src/fs/read-file.ts
/**
* @file File-content readers — UTF-8 / binary / safe variants (sync + async).
*   The `safe*` versions trap errors and return `undefined` (or a
*   caller-supplied `defaultValue`) so they fit the non-throwing-fallback shape
*   used elsewhere in the lib. The encoding `null` overload returns a `Buffer`;
*   the default and any string encoding return a string. The runtime fast-path
*   normalizes encoding once and forwards untouched options to `node:fs`.
*/
const abortSignal = require_process_abort.getAbortSignal();
/**
* Read a file as binary data asynchronously. Returns a Buffer without encoding
* the contents. Useful for reading images, archives, or other binary formats.
*
* @example
*   ;```ts
*   // Read an image file
*   const imageBuffer = await readFileBinary('./image.png')
*
*   // Read with abort signal
*   const buffer = await readFileBinary('./data.bin', { signal: abortSignal })
*   ```
*
* @param filepath - Path to file.
* @param options - Read options (encoding is forced to null for binary)
*
* @returns Promise resolving to Buffer containing file contents
*/
/*@__NO_SIDE_EFFECTS__*/
async function readFileBinary(filepath, options) {
	const opts = typeof options === "string" ? { encoding: options } : options;
	return await (/* @__PURE__ */ require_node_fs.getNodeFs()).promises.readFile(filepath, {
		signal: abortSignal,
		...opts,
		encoding: void 0
	});
}
/**
* Read a file as binary data synchronously. Returns a Buffer without encoding
* the contents. Useful for reading images, archives, or other binary formats.
*
* @example
*   ;```ts
*   // Read an image file
*   const imageBuffer = readFileBinarySync('./logo.png')
*
*   // Read a compressed file
*   const gzipData = readFileBinarySync('./archive.gz')
*   ```
*
* @param filepath - Path to file.
* @param options - Read options (encoding is forced to null for binary)
*
* @returns Buffer containing file contents
*/
/*@__NO_SIDE_EFFECTS__*/
function readFileBinarySync(filepath, options) {
	const opts = typeof options === "string" ? { encoding: options } : options;
	return (/* @__PURE__ */ require_node_fs.getNodeFs()).readFileSync(filepath, {
		...opts,
		encoding: void 0
	});
}
/**
* Read a file as UTF-8 text asynchronously. Returns a string with the file
* contents decoded as UTF-8. This is the most common way to read text files.
*
* @example
*   ;```ts
*   // Read a text file
*   const content = await readFileUtf8('./README.md')
*
*   // Read with custom encoding
*   const content = await readFileUtf8('./data.txt', { encoding: 'utf-8' })
*   ```
*
* @param filepath - Path to file.
* @param options - Read options including encoding and abort signal.
*
* @returns Promise resolving to string containing file contents
*/
/*@__NO_SIDE_EFFECTS__*/
async function readFileUtf8(filepath, options) {
	const opts = typeof options === "string" ? { encoding: options } : options;
	return await (/* @__PURE__ */ require_node_fs.getNodeFs()).promises.readFile(filepath, {
		signal: abortSignal,
		...opts,
		encoding: "utf8"
	});
}
/**
* Read a file as UTF-8 text synchronously. Returns a string with the file
* contents decoded as UTF-8. This is the most common way to read text files
* synchronously.
*
* @example
*   ;```ts
*   // Read a configuration file
*   const config = readFileUtf8Sync('./config.txt')
*
*   // Read with custom options
*   const data = readFileUtf8Sync('./data.txt', { encoding: 'utf8' })
*   ```
*
* @param filepath - Path to file.
* @param options - Read options including encoding.
*
* @returns String containing file contents
*/
/*@__NO_SIDE_EFFECTS__*/
function readFileUtf8Sync(filepath, options) {
	const opts = typeof options === "string" ? { encoding: options } : options;
	return (/* @__PURE__ */ require_node_fs.getNodeFs()).readFileSync(filepath, {
		...opts,
		encoding: "utf8"
	});
}
/*@__NO_SIDE_EFFECTS__*/
async function safeReadFile(filepath, options) {
	const { defaultValue, ...rawReadOpts } = typeof options === "string" ? {
		__proto__: null,
		encoding: options
	} : {
		__proto__: null,
		...options
	};
	const readOpts = {
		__proto__: null,
		...rawReadOpts
	};
	const shouldReturnBuffer = readOpts.encoding === null;
	/* c8 ignore next 3 */
	const encoding = shouldReturnBuffer ? void 0 : /* @__PURE__ */ require_fs_encoding.normalizeEncoding(readOpts.encoding);
	const fs = /* @__PURE__ */ require_node_fs.getNodeFs();
	try {
		return await fs.promises.readFile(filepath, {
			__proto__: null,
			signal: abortSignal,
			...readOpts,
			encoding
		});
	} catch {}
	if (defaultValue === void 0) return;
	if (shouldReturnBuffer) return require_primordials_buffer.BufferIsBuffer(defaultValue) ? defaultValue : void 0;
	return typeof defaultValue === "string" ? defaultValue : String(defaultValue);
}
/*@__NO_SIDE_EFFECTS__*/
function safeReadFileSync(filepath, options) {
	const { defaultValue, ...rawReadOpts } = typeof options === "string" ? {
		__proto__: null,
		encoding: options
	} : {
		__proto__: null,
		...options
	};
	const readOpts = {
		__proto__: null,
		...rawReadOpts
	};
	const shouldReturnBuffer = readOpts.encoding === null;
	/* c8 ignore next 3 */
	const encoding = shouldReturnBuffer ? void 0 : /* @__PURE__ */ require_fs_encoding.normalizeEncoding(readOpts.encoding);
	const fs = /* @__PURE__ */ require_node_fs.getNodeFs();
	try {
		return fs.readFileSync(filepath, {
			__proto__: null,
			...readOpts,
			encoding
		});
	} catch {}
	if (defaultValue === void 0) return;
	if (shouldReturnBuffer) return require_primordials_buffer.BufferIsBuffer(defaultValue) ? defaultValue : void 0;
	return typeof defaultValue === "string" ? defaultValue : String(defaultValue);
}

//#endregion
exports.readFileBinary = readFileBinary;
exports.readFileBinarySync = readFileBinarySync;
exports.readFileUtf8 = readFileUtf8;
exports.readFileUtf8Sync = readFileUtf8Sync;
exports.safeReadFile = safeReadFile;
exports.safeReadFileSync = safeReadFileSync;