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 xml_exports = {};
__export(xml_exports, {
  parseXML: () => parseXML,
  stringifyXML: () => stringifyXML
});
module.exports = __toCommonJS(xml_exports);
var import_fast_xml_parser = require("fast-xml-parser");
var import_xml_common = require("./xml.common.js");
function getCommonOptions(options) {
  return {
    attributesGroupName: import_xml_common.XML_ATTRKEY,
    textNodeName: options.xmlCharKey ?? import_xml_common.XML_CHARKEY,
    ignoreAttributes: false,
    suppressBooleanAttributes: false
  };
}
function getSerializerOptions(options = {}) {
  return {
    ...getCommonOptions(options),
    attributeNamePrefix: "@_",
    format: true,
    suppressEmptyNode: true,
    indentBy: "",
    rootNodeName: options.rootName ?? "root",
    cdataPropName: options.cdataPropName ?? "__cdata"
  };
}
function getParserOptions(options = {}) {
  return {
    ...getCommonOptions(options),
    parseAttributeValue: false,
    parseTagValue: false,
    attributeNamePrefix: "",
    stopNodes: options.stopNodes,
    processEntities: true,
    trimValues: false
  };
}
function stringifyXML(obj, opts = {}) {
  const parserOptions = getSerializerOptions(opts);
  const j2x = new import_fast_xml_parser.XMLBuilder(parserOptions);
  const node = { [parserOptions.rootNodeName]: obj };
  const xmlData = j2x.build(node);
  return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>${xmlData}`.replace(/\n/g, "");
}
async function parseXML(str, opts = {}) {
  if (!str) {
    throw new Error("Document is empty");
  }
  const validation = import_fast_xml_parser.XMLValidator.validate(str);
  if (validation !== true) {
    throw validation;
  }
  const parser = new import_fast_xml_parser.XMLParser(getParserOptions(opts));
  const parsedXml = parser.parse(str);
  if (parsedXml["?xml"]) {
    delete parsedXml["?xml"];
  }
  if (!opts.includeRoot) {
    for (const key of Object.keys(parsedXml)) {
      const value = parsedXml[key];
      return typeof value === "object" ? { ...value } : value;
    }
  }
  return parsedXml;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  parseXML,
  stringifyXML
});
//# sourceMappingURL=xml.js.map
