"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.findAll = findAll;
exports.simplifyAreas = simplifyAreas;
exports.makeSpans = makeSpans;
exports.createHighlighter = exports.compareArea = void 0;

var _react = _interopRequireDefault(require("react"));

var R = _interopRequireWildcard(require("ramda"));

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }

function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function findAll(targetText, snippet) {
  var matches = [];
  var pos = targetText.indexOf(snippet, 0);

  while (pos >= 0) {
    matches.push([pos, snippet.length]);
    pos = targetText.indexOf(snippet, pos + 1);
  }

  return matches;
}

var compareArea = function compareArea(_ref, _ref2) {
  var _ref3 = _slicedToArray(_ref, 2),
      start1 = _ref3[0],
      len1 = _ref3[1];

  var _ref4 = _slicedToArray(_ref2, 2),
      start2 = _ref4[0],
      len2 = _ref4[1];

  return start1 === start2 ? len1 - len2 : start1 - start2;
};

exports.compareArea = compareArea;

function simplifyAreas(matches) {
  var areas = [];
  var start = 0,
      length = 0;
  var _iteratorNormalCompletion = true;
  var _didIteratorError = false;
  var _iteratorError = undefined;

  try {
    for (var _iterator = matches[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
      var _step$value = _slicedToArray(_step.value, 2),
          st = _step$value[0],
          ln = _step$value[1];

      var overlaps = start + length >= st;

      if (length > 0 && overlaps) {
        var end = Math.max(start + length, st + ln);
        start = Math.min(start, st);
        length = end - start;
      } else {
        if (length > 0) areas.push([start, length]);
        start = st;
        length = ln;
      }
    }
  } catch (err) {
    _didIteratorError = true;
    _iteratorError = err;
  } finally {
    try {
      if (!_iteratorNormalCompletion && _iterator.return != null) {
        _iterator.return();
      }
    } finally {
      if (_didIteratorError) {
        throw _iteratorError;
      }
    }
  }

  if (length > 0) areas.push([start, length]);
  return areas;
}

function makeSpans(targetText, highlightAreas, highlightClass) {
  var spans = [];

  var pushSpan = function pushSpan(txt, i, highlightClass) {
    return highlightClass ? spans.push(_react.default.createElement("span", {
      key: "".concat(i, "-").concat(txt),
      className: highlightClass
    }, txt)) : spans.push(_react.default.createElement("span", {
      key: "".concat(i, "-").concat(txt)
    }, txt));
  };

  var last = 0;
  var i = 0;
  var _iteratorNormalCompletion2 = true;
  var _didIteratorError2 = false;
  var _iteratorError2 = undefined;

  try {
    for (var _iterator2 = highlightAreas[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
      var _step2$value = _slicedToArray(_step2.value, 2),
          start = _step2$value[0],
          length = _step2$value[1];

      if (start > last) pushSpan(targetText.substring(last, start), i++);
      last = start + length;
      pushSpan(targetText.substring(start, last), i, highlightClass);
      ++i;
    }
  } catch (err) {
    _didIteratorError2 = true;
    _iteratorError2 = err;
  } finally {
    try {
      if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
        _iterator2.return();
      }
    } finally {
      if (_didIteratorError2) {
        throw _iteratorError2;
      }
    }
  }

  if (last < targetText.length) pushSpan(targetText.substring(last, targetText.length), i);
  return spans;
}

var createHighlighter = function createHighlighter(getName, highlightClass, sizeThreshold) {
  return function (nodeId, textToFind) {
    var targetText = getName(nodeId);
    var snippetArray = textToFind.toLowerCase().split(' ').filter(function (x) {
      return x.length >= sizeThreshold;
    });
    if (snippetArray.length === 0) return [_react.default.createElement("span", {
      key: '0-' + targetText
    }, targetText)];
    var matchedAreas = R.chain(function (snippet) {
      return findAll(targetText.toLowerCase(), snippet);
    }, snippetArray).sort(compareArea);
    var simplifiedAreas = simplifyAreas(matchedAreas);
    return makeSpans(targetText, simplifiedAreas, highlightClass);
  };
};

exports.createHighlighter = createHighlighter;