import { deepEqual } from "../../../shared/src/json.js";
const SPLICE_UNASSIGNED = -1;
const SPLICE_AT = 0;
const SPLICE_REMOVED = 1;
const SPLICE_ADDED = 2;
const SPLICE_FROM = 3;
const KEY = 0;
const VALUE = 1;
function* computeSplices(previous, current) {
  let previousIndex = 0;
  let currentIndex = 0;
  let splice;
  function ensureAssigned(splice2, index) {
    if (splice2[SPLICE_FROM] === SPLICE_UNASSIGNED) {
      splice2[SPLICE_FROM] = index;
    }
  }
  function newSplice() {
    return [previousIndex, 0, 0, SPLICE_UNASSIGNED];
  }
  while (previousIndex < previous.length && currentIndex < current.length) {
    if (previous[previousIndex][KEY] === current[currentIndex][KEY]) {
      if (deepEqual(
        // These are really Hash | InternalValue
        previous[previousIndex][VALUE],
        current[currentIndex][VALUE]
      )) {
        if (splice) {
          ensureAssigned(splice, 0);
          yield splice;
          splice = void 0;
        }
      } else {
        if (!splice) {
          splice = newSplice();
        }
        splice[SPLICE_ADDED]++;
        splice[SPLICE_REMOVED]++;
        ensureAssigned(splice, currentIndex);
      }
      previousIndex++;
      currentIndex++;
    } else if (previous[previousIndex][KEY] < current[currentIndex][KEY]) {
      if (!splice) {
        splice = newSplice();
      }
      splice[SPLICE_REMOVED]++;
      previousIndex++;
    } else {
      if (!splice) {
        splice = newSplice();
      }
      splice[SPLICE_ADDED]++;
      ensureAssigned(splice, currentIndex);
      currentIndex++;
    }
  }
  if (currentIndex < current.length) {
    if (!splice) {
      splice = newSplice();
    }
    splice[SPLICE_ADDED] += current.length - currentIndex;
    ensureAssigned(splice, currentIndex);
  }
  if (previousIndex < previous.length) {
    if (!splice) {
      splice = newSplice();
    }
    splice[SPLICE_REMOVED] += previous.length - previousIndex;
  }
  if (splice) {
    ensureAssigned(splice, 0);
    yield splice;
  }
}
export {
  SPLICE_ADDED,
  SPLICE_AT,
  SPLICE_FROM,
  SPLICE_REMOVED,
  computeSplices
};
//# sourceMappingURL=splice.js.map
