"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 write_json_exports = {};
__export(write_json_exports, {
  stringify: () => stringify,
  writeJson: () => writeJson,
  writeJsonSync: () => writeJsonSync
});
module.exports = __toCommonJS(write_json_exports);
var import_fs = require("../node/fs");
var import_json = require("../primordials/json");
var import_string = require("../primordials/string");
const NEWLINE_REGEX = /\n/g;
// @__NO_SIDE_EFFECTS__
function stringify(json, EOL, finalEOL, replacer, spaces = 2) {
  const EOF = finalEOL ? EOL : "";
  const str = (0, import_json.JSONStringify)(json, replacer, spaces);
  return `${(0, import_string.StringPrototypeReplace)(str, NEWLINE_REGEX, EOL)}${EOF}`;
}
async function writeJson(filepath, jsonContent, options) {
  const opts = typeof options === "string" ? { encoding: options } : options;
  const { EOL, finalEOL, replacer, spaces, ...fsOptions } = {
    __proto__: null,
    ...opts
  };
  const fs = (0, import_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
  });
}
function writeJsonSync(filepath, jsonContent, options) {
  const opts = typeof options === "string" ? { encoding: options } : options;
  const { EOL, finalEOL, replacer, spaces, ...fsOptions } = {
    __proto__: null,
    ...opts
  };
  const fs = (0, import_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
  });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  stringify,
  writeJson,
  writeJsonSync
});
