"use strict";
/* Socket Lib - Built with esbuild */
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var read_file_exports = {};
__export(read_file_exports, {
  readFileBinary: () => readFileBinary,
  readFileBinarySync: () => readFileBinarySync,
  readFileUtf8: () => readFileUtf8,
  readFileUtf8Sync: () => readFileUtf8Sync,
  safeReadFile: () => safeReadFile,
  safeReadFileSync: () => safeReadFileSync
});
module.exports = __toCommonJS(read_file_exports);
var import_abort = require("../process/abort");
var import_fs = require("../node/fs");
var import_buffer = require("../primordials/buffer");
var import_encoding = require("./encoding");
const abortSignal = (0, import_abort.getAbortSignal)();
// @__NO_SIDE_EFFECTS__
async function readFileBinary(filepath, options) {
  const opts = typeof options === "string" ? { encoding: options } : options;
  const fs = (0, import_fs.getNodeFs)();
  return await fs.promises.readFile(filepath, {
    signal: abortSignal,
    ...opts,
    encoding: void 0
  });
}
// @__NO_SIDE_EFFECTS__
function readFileBinarySync(filepath, options) {
  const opts = typeof options === "string" ? { encoding: options } : options;
  const fs = (0, import_fs.getNodeFs)();
  return fs.readFileSync(filepath, {
    ...opts,
    encoding: void 0
  });
}
// @__NO_SIDE_EFFECTS__
async function readFileUtf8(filepath, options) {
  const opts = typeof options === "string" ? { encoding: options } : options;
  const fs = (0, import_fs.getNodeFs)();
  return await fs.promises.readFile(filepath, {
    signal: abortSignal,
    ...opts,
    encoding: "utf8"
  });
}
// @__NO_SIDE_EFFECTS__
function readFileUtf8Sync(filepath, options) {
  const opts = typeof options === "string" ? { encoding: options } : options;
  const fs = (0, import_fs.getNodeFs)();
  return fs.readFileSync(filepath, {
    ...opts,
    encoding: "utf8"
  });
}
// @__NO_SIDE_EFFECTS__
async function safeReadFile(filepath, options) {
  const opts = typeof options === "string" ? { __proto__: null, encoding: options } : { __proto__: null, ...options };
  const { defaultValue, ...rawReadOpts } = opts;
  const readOpts = { __proto__: null, ...rawReadOpts };
  const shouldReturnBuffer = readOpts.encoding === null;
  const encoding = shouldReturnBuffer ? void 0 : (0, import_encoding.normalizeEncoding)(readOpts.encoding);
  const fs = (0, import_fs.getNodeFs)();
  try {
    return await fs.promises.readFile(filepath, {
      __proto__: null,
      signal: abortSignal,
      ...readOpts,
      encoding
    });
  } catch {
  }
  if (defaultValue === void 0) {
    return void 0;
  }
  if (shouldReturnBuffer) {
    return (0, import_buffer.BufferIsBuffer)(defaultValue) ? defaultValue : void 0;
  }
  return typeof defaultValue === "string" ? defaultValue : String(defaultValue);
}
// @__NO_SIDE_EFFECTS__
function safeReadFileSync(filepath, options) {
  const opts = typeof options === "string" ? { __proto__: null, encoding: options } : { __proto__: null, ...options };
  const { defaultValue, ...rawReadOpts } = opts;
  const readOpts = { __proto__: null, ...rawReadOpts };
  const shouldReturnBuffer = readOpts.encoding === null;
  const encoding = shouldReturnBuffer ? void 0 : (0, import_encoding.normalizeEncoding)(readOpts.encoding);
  const fs = (0, import_fs.getNodeFs)();
  try {
    return fs.readFileSync(filepath, {
      __proto__: null,
      ...readOpts,
      encoding
    });
  } catch {
  }
  if (defaultValue === void 0) {
    return void 0;
  }
  if (shouldReturnBuffer) {
    return (0, import_buffer.BufferIsBuffer)(defaultValue) ? defaultValue : void 0;
  }
  return typeof defaultValue === "string" ? defaultValue : String(defaultValue);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  readFileBinary,
  readFileBinarySync,
  readFileUtf8,
  readFileUtf8Sync,
  safeReadFile,
  safeReadFileSync
});
