"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const lineTokens = require("./lineTokens.js");
const EMPTY_LINE_TOKENS = new Uint32Array(0).buffer;
class ContiguousTokensEditing {
  static deleteBeginning(lineTokens2, toChIndex) {
    if (lineTokens2 === null || lineTokens2 === EMPTY_LINE_TOKENS) {
      return lineTokens2;
    }
    return ContiguousTokensEditing.delete(lineTokens2, 0, toChIndex);
  }
  static deleteEnding(lineTokens2, fromChIndex) {
    if (lineTokens2 === null || lineTokens2 === EMPTY_LINE_TOKENS) {
      return lineTokens2;
    }
    const tokens = toUint32Array(lineTokens2);
    const lineTextLength = tokens[tokens.length - 2];
    return ContiguousTokensEditing.delete(lineTokens2, fromChIndex, lineTextLength);
  }
  static delete(lineTokens$1, fromChIndex, toChIndex) {
    if (lineTokens$1 === null || lineTokens$1 === EMPTY_LINE_TOKENS || fromChIndex === toChIndex) {
      return lineTokens$1;
    }
    const tokens = toUint32Array(lineTokens$1);
    const tokensCount = tokens.length >>> 1;
    if (fromChIndex === 0 && tokens[tokens.length - 2] === toChIndex) {
      return EMPTY_LINE_TOKENS;
    }
    const fromTokenIndex = lineTokens.LineTokens.findIndexInTokensArray(tokens, fromChIndex);
    const fromTokenStartOffset = fromTokenIndex > 0 ? tokens[fromTokenIndex - 1 << 1] : 0;
    const fromTokenEndOffset = tokens[fromTokenIndex << 1];
    if (toChIndex < fromTokenEndOffset) {
      const delta2 = toChIndex - fromChIndex;
      for (let i = fromTokenIndex; i < tokensCount; i++) {
        tokens[i << 1] -= delta2;
      }
      return lineTokens$1;
    }
    let dest;
    let lastEnd;
    if (fromTokenStartOffset !== fromChIndex) {
      tokens[fromTokenIndex << 1] = fromChIndex;
      dest = fromTokenIndex + 1 << 1;
      lastEnd = fromChIndex;
    } else {
      dest = fromTokenIndex << 1;
      lastEnd = fromTokenStartOffset;
    }
    const delta = toChIndex - fromChIndex;
    for (let tokenIndex = fromTokenIndex + 1; tokenIndex < tokensCount; tokenIndex++) {
      const tokenEndOffset = tokens[tokenIndex << 1] - delta;
      if (tokenEndOffset > lastEnd) {
        tokens[dest++] = tokenEndOffset;
        tokens[dest++] = tokens[(tokenIndex << 1) + 1];
        lastEnd = tokenEndOffset;
      }
    }
    if (dest === tokens.length) {
      return lineTokens$1;
    }
    const tmp = new Uint32Array(dest);
    tmp.set(tokens.subarray(0, dest), 0);
    return tmp.buffer;
  }
  static append(lineTokens2, _otherTokens) {
    if (_otherTokens === EMPTY_LINE_TOKENS) {
      return lineTokens2;
    }
    if (lineTokens2 === EMPTY_LINE_TOKENS) {
      return _otherTokens;
    }
    if (lineTokens2 === null) {
      return lineTokens2;
    }
    if (_otherTokens === null) {
      return null;
    }
    const myTokens = toUint32Array(lineTokens2);
    const otherTokens = toUint32Array(_otherTokens);
    const otherTokensCount = otherTokens.length >>> 1;
    const result = new Uint32Array(myTokens.length + otherTokens.length);
    result.set(myTokens, 0);
    let dest = myTokens.length;
    const delta = myTokens[myTokens.length - 2];
    for (let i = 0; i < otherTokensCount; i++) {
      result[dest++] = otherTokens[i << 1] + delta;
      result[dest++] = otherTokens[(i << 1) + 1];
    }
    return result.buffer;
  }
  static insert(lineTokens$1, chIndex, textLength) {
    if (lineTokens$1 === null || lineTokens$1 === EMPTY_LINE_TOKENS) {
      return lineTokens$1;
    }
    const tokens = toUint32Array(lineTokens$1);
    const tokensCount = tokens.length >>> 1;
    let fromTokenIndex = lineTokens.LineTokens.findIndexInTokensArray(tokens, chIndex);
    if (fromTokenIndex > 0) {
      const fromTokenStartOffset = tokens[fromTokenIndex - 1 << 1];
      if (fromTokenStartOffset === chIndex) {
        fromTokenIndex--;
      }
    }
    for (let tokenIndex = fromTokenIndex; tokenIndex < tokensCount; tokenIndex++) {
      tokens[tokenIndex << 1] += textLength;
    }
    return lineTokens$1;
  }
}
function toUint32Array(arr) {
  if (arr instanceof Uint32Array) {
    return arr;
  } else {
    return new Uint32Array(arr);
  }
}
exports.ContiguousTokensEditing = ContiguousTokensEditing;
exports.EMPTY_LINE_TOKENS = EMPTY_LINE_TOKENS;
exports.toUint32Array = toUint32Array;
