export function getInputContent(editor) {
  const contents = [];
  const nodesSectionsCollector = toNodeSections(editor);
  nodesSectionsCollector.forEach((section) => {
    const sectionContent = [];
    section.forEach((node) => {
      sectionContent.push(node.textContent);
    });
    contents.push(sectionContent.join(""));
  });
  return contents.join("\n");
}

function toNodeSections(node) {
  let sections = [];
  let curSection = [];

  const checkIsDiv = (elem) => {
    return elem.nodeType == Node.ELEMENT_NODE && elem.nodeName === "DIV";
  };

  const parseNode = (node) => {
    node.childNodes.forEach((elem) => {
      if (checkIsDiv(elem) && curSection.length) {
        sections.push(curSection);
        curSection = [];
      }

      if (elem.childNodes && elem.childNodes.length) {
        parseNode(elem);
      } else {
        curSection.push(elem);
      }
    });
  };

  parseNode(node);

  if (curSection.length) {
    sections.push(curSection);
  }

  // console.log(sections);
  return sections;
}
