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_Button",
    NODE_NAME: element.Name,
    CODE_PRESS: `${codes[0]} (>L:CEVENT)`,
    CODE_RELEASE: `${codes[1]} (>L:CEVENT)`,
  };
}

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);
