"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 format_exports = {};
__export(format_exports, {
  INDENT_SYMBOL: () => INDENT_SYMBOL,
  NEWLINE_SYMBOL: () => NEWLINE_SYMBOL,
  detectIndent: () => detectIndent,
  detectNewline: () => detectNewline,
  extractFormatting: () => extractFormatting,
  getDefaultFormatting: () => getDefaultFormatting,
  getFormattingFromContent: () => getFormattingFromContent,
  shouldSave: () => shouldSave,
  sortKeys: () => sortKeys,
  stringifyWithFormatting: () => stringifyWithFormatting,
  stripFormattingSymbols: () => stripFormattingSymbols
});
module.exports = __toCommonJS(format_exports);
var import_json = require("../primordials/json");
var import_object = require("../primordials/object");
const INDENT_SYMBOL = Symbol.for("indent");
const NEWLINE_SYMBOL = Symbol.for("newline");
function detectIndent(json) {
  const match = json.match(/^[{[][\r\n]+(\s+)/m);
  if (!match) {
    return 2;
  }
  const indent = match[1];
  if (/^ +$/.test(indent)) {
    return indent.length;
  }
  return indent;
}
function detectNewline(json) {
  const match = json.match(/\r?\n/);
  return match ? match[0] : "\n";
}
function extractFormatting(json) {
  return {
    indent: detectIndent(json),
    newline: detectNewline(json)
  };
}
function getDefaultFormatting() {
  return {
    indent: 2,
    newline: "\n"
  };
}
function getFormattingFromContent(content) {
  const indent = content[INDENT_SYMBOL];
  const newline = content[NEWLINE_SYMBOL];
  return {
    indent: indent === void 0 || indent === null ? 2 : indent,
    newline: newline === void 0 || newline === null ? "\n" : newline
  };
}
function shouldSave(currentContent, originalContent, originalFileContent, options = {}) {
  const { ignoreWhitespace = false, sort = false, sortFn } = options;
  const content = stripFormattingSymbols(currentContent);
  const sortedContent = sortFn ? sortFn(content) : sort ? sortKeys(content) : content;
  const origContent = originalContent ? stripFormattingSymbols(originalContent) : {};
  if (ignoreWhitespace) {
    const util = require("node:util");
    return !util.isDeepStrictEqual(sortedContent, origContent);
  }
  const formatting = getFormattingFromContent(currentContent);
  const newFileContent = stringifyWithFormatting(sortedContent, formatting);
  return newFileContent.trim() !== originalFileContent.trim();
}
function sortKeys(obj) {
  const sorted = { __proto__: null };
  const keys = (0, import_object.ObjectKeys)(obj).sort();
  for (const key of keys) {
    sorted[key] = obj[key];
  }
  return sorted;
}
function stringifyWithFormatting(content, formatting) {
  const { indent, newline } = formatting;
  const format = indent === void 0 || indent === null ? "  " : indent;
  const eol = newline === void 0 || newline === null ? "\n" : newline;
  return `${(0, import_json.JSONStringify)(content, void 0, format)}
`.replace(/\n/g, eol);
}
function stripFormattingSymbols(content) {
  const {
    [INDENT_SYMBOL]: _indent,
    [NEWLINE_SYMBOL]: _newline,
    ...rest
  } = content;
  return rest;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  INDENT_SYMBOL,
  NEWLINE_SYMBOL,
  detectIndent,
  detectNewline,
  extractFormatting,
  getDefaultFormatting,
  getFormattingFromContent,
  shouldSave,
  sortKeys,
  stringifyWithFormatting,
  stripFormattingSymbols
});
