"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.prepareText = exports.parseParagraphs = exports.parseParagraph = exports.parseBrs = void 0;

var createElementFromHTML = function createElementFromHTML() {
  var htmlString = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  var div = document.createElement('div');
  div.innerHTML = htmlString.trim();
  return div;
};

var parseBrs = function parseBrs(dom) {
  var brs = dom.querySelectorAll('br');
  brs.forEach(function (br) {
    return br.replaceWith('\n');
  });
  dom.innerHTML = dom.innerHTML.replace(/\n\n/g, '\n');
};

exports.parseBrs = parseBrs;

var parseParagraph = function parseParagraph(paragraph, end) {
  if (end) {
    return paragraph.innerHTML;
  }

  return "".concat(paragraph.innerHTML, "\n\n");
};

exports.parseParagraph = parseParagraph;

var parseParagraphs = function parseParagraphs(dom) {
  var paragraphs = dom.querySelectorAll('p'); // separate variable for easily debugging, if needed

  var str = '';
  paragraphs.forEach(function (par, index) {
    str += parseParagraph(par, index === paragraphs.length - 1);
  });
  return str || null;
};

exports.parseParagraphs = parseParagraphs;

var prepareText = function prepareText(text) {
  var txtDom = createElementFromHTML(text);
  var allDomElements = Array.from(txtDom.querySelectorAll('*'));

  if (txtDom.querySelectorAll('p').length === 0) {
    var div = document.createElement('div');
    div.innerHTML = "<p>".concat(txtDom.innerHTML, "</p>");
    txtDom = div;
  } // if no dom elements, we just return the text


  if (allDomElements.length === 0) {
    return text;
  }

  parseBrs(txtDom);
  return parseParagraphs(txtDom);
};

exports.prepareText = prepareText;
//# sourceMappingURL=utils.js.map