"use strict";var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;exports.__esModule = true;exports.CursorHelper = void 0;var _createForOfIteratorHelperLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/createForOfIteratorHelperLoose"));



var calculateExtendedSelection = function calculateExtendedSelection(map, selection, step) {
  if (selection.direction === 'backward') {
    return {
      start: CursorHelper.calculatePosition(map, selection.start, step),
      end: selection.end,
      direction: 'backward'
    };
  }

  return {
    start: selection.start,
    end: CursorHelper.calculatePosition(map, selection.end, step),
    direction: 'forward'
  };
};var

CursorHelper = exports.CursorHelper = /*#__PURE__*/function () {function CursorHelper() {}CursorHelper.
  normalizePosition = function normalizePosition(map, position) {
    return map[Math.min(Math.max(0, position), map.length - 1)];
  };CursorHelper.

  calculatePosition = function calculatePosition(map, position, step) {
    if (position < 0 || map.length <= position) {
      throw new Error("position out of range [" + 0 + " .. " + (map.length - 1) + "], actual value: " + position);
    }
    var raw = CursorHelper.toRawPosition(map, position);
    return CursorHelper.toFormattedPosition(map, raw + step);
  };CursorHelper.

  extendSelection = function extendSelection(map, selection, step) {
    var normalizedSelection = CursorHelper.normalizeSelection(map, selection);

    return CursorHelper.normalizeSelection(map, calculateExtendedSelection(map, normalizedSelection, step));
  };CursorHelper.

  normalizeSelection = function normalizeSelection(map, selection) {
    var start = CursorHelper.normalizePosition(map, selection.start);
    var end = CursorHelper.normalizePosition(map, selection.end);

    if (start === end) {
      return {
        start: start,
        end: end,
        direction: 'none'
      };
    }

    if (start < end) {
      return {
        start: start,
        end: end,
        direction: selection.direction === 'backward' ? 'backward' : 'forward'
      };
    }

    return {
      start: end,
      end: start,
      direction: selection.direction === 'backward' ? 'forward' : 'backward'
    };
  };CursorHelper.

  toRawPosition = function toRawPosition(map, formattedPosition) {
    var count = Math.min(Math.max(0, formattedPosition), map.length - 1);
    return new Set(map.slice(0, count + 1)).size - 1;
  };CursorHelper.

  toFormattedPosition = function toFormattedPosition(map, rawPosition) {
    var count = Math.max(0, rawPosition) + 1;
    var unique = new Set();

    for (var _iterator = (0, _createForOfIteratorHelperLoose2.default)(map), _step; !(_step = _iterator()).done;) {var position = _step.value;
      unique.add(position);
      if (unique.size === count) {
        return position;
      }
    }

    return map[map.length - 1];
  };return CursorHelper;}();