interface TextPart {
  text: string;       
  isLatex: boolean;
  isImage: boolean;
  key: number;
}

export const parseTextAndLatex = (input: string = ""): TextPart[] => {
  const out: TextPart[] = [];

  const parts = input.split(/(\$\$\$[\s\S]+?\$\$\$|\$\$[\s\S]+?\$\$|<<img>>[\s\S]+?<<img>>)/g);

  parts.forEach((part, index) => {
    if (!part) return;

        if (part.startsWith("$$$") && part.endsWith("$$$")) {
      out.push({ text: part.slice(3, -3), isLatex: true, isImage: false, key: index });
    }else if (part.startsWith("$$") && part.endsWith("$$")) {
      out.push({ text: part.slice(2, -2), isLatex: true, isImage: false, key: index });
    } else if (part.startsWith("<<img>>") && part.endsWith("<<img>>")) {
      const url = part.slice("<<img>>".length, -"<<img>>".length).trim();
      out.push({ text: url, isLatex: false, isImage: true, key: index });
    } else {
      out.push({ text: part, isLatex: false, isImage: false, key: index });
    }
  });

  return out;
};
