"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_json_exports = {};
__export(read_json_exports, {
  readJson: () => readJson,
  readJsonSync: () => readJsonSync
});
module.exports = __toCommonJS(read_json_exports);
var import_parse = require("../json/parse");
var import_fs = require("../node/fs");
var import_error = require("../primordials/error");
// @__NO_SIDE_EFFECTS__
async function readJson(filepath, options) {
  const opts = typeof options === "string" ? { encoding: options } : options;
  const { reviver, throws, ...fsOptions } = {
    __proto__: null,
    ...opts
  };
  const shouldThrow = throws === void 0 || !!throws;
  const fs = (0, import_fs.getNodeFs)();
  let content = "";
  try {
    content = await fs.promises.readFile(filepath, {
      __proto__: null,
      ...fsOptions,
      encoding: "utf8"
    });
  } catch (e) {
    if (shouldThrow) {
      const code = e.code;
      if (code === "ENOENT") {
        throw new import_error.ErrorCtor(
          `JSON file not found: ${filepath}
Ensure the file exists or create it with the expected structure.`,
          { cause: e }
        );
      }
      if (code === "EACCES" || code === "EPERM") {
        throw new import_error.ErrorCtor(
          `Permission denied reading JSON file: ${filepath}
Check file permissions or run with appropriate access.`,
          { cause: e }
        );
      }
      throw e;
    }
    return void 0;
  }
  return (0, import_parse.parseJson)(content, {
    filepath: String(filepath),
    reviver,
    throws: shouldThrow
  });
}
// @__NO_SIDE_EFFECTS__
function readJsonSync(filepath, options) {
  const opts = typeof options === "string" ? { encoding: options } : options;
  const { reviver, throws, ...fsOptions } = {
    __proto__: null,
    ...opts
  };
  const shouldThrow = throws === void 0 || !!throws;
  const fs = (0, import_fs.getNodeFs)();
  let content = "";
  try {
    content = fs.readFileSync(filepath, {
      __proto__: null,
      ...fsOptions,
      encoding: "utf8"
    });
  } catch (e) {
    if (shouldThrow) {
      const code = e.code;
      if (code === "ENOENT") {
        throw new import_error.ErrorCtor(
          `JSON file not found: ${filepath}
Ensure the file exists or create it with the expected structure.`,
          { cause: e }
        );
      }
      if (code === "EACCES" || code === "EPERM") {
        throw new import_error.ErrorCtor(
          `Permission denied reading JSON file: ${filepath}
Check file permissions or run with appropriate access.`,
          { cause: e }
        );
      }
      throw e;
    }
    return void 0;
  }
  return (0, import_parse.parseJson)(content, {
    filepath: String(filepath),
    reviver,
    throws: shouldThrow
  });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  readJson,
  readJsonSync
});
