import { XMLParser, XMLBuilder } from "fast-xml-parser";

const input = `

`;

function convert(element) {
  const codes = [
    ...element.MouseRect.CallbackCode.matchAll(/([\d]*)[\s]*\(>L:CEVENT\)/gm),
  ].map((m) => m[1]);

  return {
    "@_Name": "MD11_Infinite_Knob_Push",
    NODE_NAME: element.Name,
    ANIM_LENGTH_KNOB: 10,
    ANIM_CODE: `10 (L:${element.Name}) 1 * -`,
    ANIM_NAME_PUSH: `${element.Name.replace("KB", "BT")}`,
    ANIM_CODE_PUSH: `(L:${element.Name.replace("KB", "BT")}) 10 *`,
    INC_CODE: `${codes[0]} (>L:CEVENT)`,
    DEC_CODE: `${codes[1]} (>L:CEVENT)`,
    PUSH_CODE: `${codes[2]} (>L:CEVENT)`,
    REL_CODE: null,
  };
}

const parser = new XMLParser({
  ignoreAttributes: false,
});
let jObj = parser.parse(input);

const output = [];

if (Array.isArray(jObj.PartInfo)) {
  jObj.PartInfo.forEach((element) => {
    output.push(convert(element));
  });
} else {
  output.push(convert(jObj.PartInfo));
}

const builder = new XMLBuilder({
  ignoreAttributes: false,
  arrayNodeName: "UseTemplate",
  format: true,
});
const xmlContent = builder.build(output);

console.log(xmlContent);
