import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
export var simpleSequenceToEntitySequence = function simpleSequenceToEntitySequence(simpleSeq) {
  var entSeq = [];
  var charsPassed = 0;

  var _iterator = _createForOfIteratorHelper(simpleSeq || []),
      _step;

  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var seq = _step.value;

      if (seq.label) {
        entSeq.push({
          text: seq.text,
          label: seq.label,
          start: charsPassed,
          end: charsPassed + seq.text.length
        });
      }

      charsPassed += seq.text.length;
    }
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }

  return entSeq;
};
export var simpleSequenceAndRelationsToEntitySequence = function simpleSequenceAndRelationsToEntitySequence(_ref) {
  var simpleSeq = _ref.sequence,
      relationships = _ref.relationships;
  var textIdsInRelation = new Set((relationships || []).flatMap(function (r) {
    return [r.from, r.to];
  }));
  var entSeq = [];
  var charsPassed = 0;

  var _iterator2 = _createForOfIteratorHelper(simpleSeq || []),
      _step2;

  try {
    for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
      var seq = _step2.value;

      if (seq.label || seq.textId && textIdsInRelation.has(seq.textId)) {
        entSeq.push({
          text: seq.text,
          textId: seq.textId,
          label: seq.label,
          start: charsPassed,
          end: charsPassed + seq.text.length - 1
        });
      }

      charsPassed += seq.text.length;
    }
  } catch (err) {
    _iterator2.e(err);
  } finally {
    _iterator2.f();
  }

  return {
    entities: entSeq,
    relations: relationships
  };
};
export var entitySequenceToSimpleSeq = function entitySequenceToSimpleSeq(doc, entSeq) {
  if (!entSeq) return undefined;
  var simpleSeq = [];
  entSeq = _toConsumableArray(entSeq);
  entSeq.sort(function (a, b) {
    return a.start - b.start;
  });
  var nextEntity = 0;

  for (var i = 0; i < doc.length; i++) {
    if ((entSeq[nextEntity] || {}).start === i) {
      simpleSeq.push({
        text: entSeq[nextEntity].text,
        label: entSeq[nextEntity].label,
        textId: entSeq[nextEntity].textId || Math.random().toString(36).slice(-6)
      }); // i = entSeq[nextEntity].end

      i += entSeq[nextEntity].text.length - 1;
      simpleSeq.push({
        text: ""
      });
      nextEntity += 1;
    } else {
      if (simpleSeq.length === 0 || simpleSeq[simpleSeq.length - 1].label) {
        simpleSeq.push({
          text: doc[i],
          textId: Math.random().toString(36).slice(-6)
        });
      } else {
        simpleSeq[simpleSeq.length - 1].text += doc[i];
      }
    }
  }

  if (simpleSeq.map(function (s) {
    return s.text;
  }).join("") !== doc) {
    throw new Error("Combining annotation entities with document didn't match document.\n\n" + simpleSeq.map(function (s) {
      return s.text;
    }).join("") + "\n\nvs\n\n" + doc);
  }

  return simpleSeq.filter(function (item) {
    return item.text.length > 0;
  });
};