"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

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 parseParagraph = function parseParagraph(paragraph, end) {
  if (end) {
    return paragraph.innerHTML;
  }

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

var parseParagraphs = function parseParagraphs(dom) {
  var paragraphs = dom.querySelectorAll('div[separator=true]'); // separate variable for easily debugging, if needed

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

var prepareText = function prepareText(text) {
  var txtDom = createElementFromHTML(text);
  var div = document.createElement('div');
  div.innerHTML = '<div separator=\'true\'>'.concat(txtDom.innerHTML, '</div>');
  txtDom = div;
  var allDomElements = Array.from(txtDom.querySelectorAll('*'));

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

  return parseParagraphs(txtDom);
};

var _default = function _default(model) {
  if (!model) return model; // parsing

  var modelText = prepareText(model.text || '');
  var newTokens = (model.tokens || []).reduce(function (acc) {
    var token = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
    var tokenText = prepareText((model.text || '').slice(token.start, token.end));

    if (!tokenText) {
      return [].concat((0, _toConsumableArray2["default"])(acc), [token]);
    }

    var getStartIndex = function getStartIndex(start) {
      var length = start;

      while (length >= 0) {
        var newStartIndex = modelText.slice(length).indexOf(tokenText);

        if (newStartIndex >= 0) {
          return newStartIndex + length;
        }

        length--;
      }

      return 0;
    };

    var lastToken = acc[acc.length - 1];
    var newStart = getStartIndex(lastToken ? lastToken.end : token.start);
    var newEnd = newStart + tokenText.length;
    return [].concat((0, _toConsumableArray2["default"])(acc), [_objectSpread(_objectSpread({}, token), {}, {
      text: tokenText,
      start: newStart,
      end: newEnd,
      // needed for getScore when tokens position is recalculated
      oldStart: token.start,
      oldEnd: token.end
    })]);
  }, []);
  return _objectSpread(_objectSpread({}, model), {}, {
    tokens: newTokens,
    text: modelText
  });
};

exports["default"] = _default;
//# sourceMappingURL=utils.js.map