function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
import "core-js/modules/es.array.iterator.js";
import "core-js/modules/es.object.to-string.js";
import "core-js/modules/es.string.iterator.js";
import "core-js/modules/es.weak-map.js";
import "core-js/modules/web.dom-collections.iterator.js";
import "core-js/modules/es.weak-set.js";
import "core-js/modules/es.symbol.to-primitive.js";
import "core-js/modules/es.date.to-primitive.js";
import "core-js/modules/es.symbol.js";
import "core-js/modules/es.symbol.description.js";
import "core-js/modules/es.number.constructor.js";
import "core-js/modules/es.symbol.iterator.js";
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _classPrivateMethodInitSpec(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet); privateSet.add(obj); }
function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); }
function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
function _classPrivateFieldGet(receiver, privateMap) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); return _classApplyDescriptorGet(receiver, descriptor); }
function _classApplyDescriptorGet(receiver, descriptor) { if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; }
function _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return fn; }
function _classPrivateFieldSet(receiver, privateMap, value) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); _classApplyDescriptorSet(receiver, descriptor, value); return value; }
function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to " + action + " private field on non-instance"); } return privateMap.get(receiver); }
function _classApplyDescriptorSet(receiver, descriptor, value) { if (descriptor.set) { descriptor.set.call(receiver, value); } else { if (!descriptor.writable) { throw new TypeError("attempted to set read only private field"); } descriptor.value = value; } }
import { mixin } from "../helpers/object.mjs";
import localHooks from "./../mixins/localHooks.mjs";
/**
 * The Transformation class implements algorithms for transforming coordinates based on current settings
 * passed to the Handsontable. The class performs the calculations based on the renderable indexes.
 *
 * Transformation is always applied relative to the current selection.
 *
 * The class operates on a table size defined by the renderable indexes. If the `navigableHeaders`
 * option is enabled, the table size is increased by the number of row and/or column headers.
 * Because the headers are treated as cells as part of the table size (indexes always go from 0 to N),
 * the algorithm can be written as simply as possible (without new if's that distinguish the headers
 * logic).
 *
 * @class Transformation
 * @util
 */
var _range = /*#__PURE__*/new WeakMap();
var _options = /*#__PURE__*/new WeakMap();
var _offset = /*#__PURE__*/new WeakMap();
var _setOffsetSize = /*#__PURE__*/new WeakSet();
var _clampCoords = /*#__PURE__*/new WeakSet();
var _getTableSize = /*#__PURE__*/new WeakSet();
var _visualToZeroBasedCoords = /*#__PURE__*/new WeakSet();
var _zeroBasedToVisualCoords = /*#__PURE__*/new WeakSet();
var Transformation = /*#__PURE__*/function () {
  function Transformation(range, options) {
    _classCallCheck(this, Transformation);
    /**
     * Translates the zero-based coordinates to visual ones.
     *
     * @param {CellCoords} zeroBasedCoords The coordinates to process.
     * @returns {CellCoords}
     */
    _classPrivateMethodInitSpec(this, _zeroBasedToVisualCoords);
    /**
     * Translates the visual coordinates to zero-based ones.
     *
     * @param {CellCoords} visualCoords The visual coords to process.
     * @returns {{x: number, y: number}}
     */
    _classPrivateMethodInitSpec(this, _visualToZeroBasedCoords);
    /**
     * Gets the table size in number of rows with headers as "height" and number of columns with
     * headers as "width".
     *
     * @returns {{width: number, height: number}}
     */
    _classPrivateMethodInitSpec(this, _getTableSize);
    /**
     * Clamps the coords to make sure they points to the cell (or header) in the table range.
     *
     * @param {CellCoords} zeroBasedCoords The coords object to clamp.
     * @returns {{rowDir: 1|0|-1, colDir: 1|0|-1}}
     */
    _classPrivateMethodInitSpec(this, _clampCoords);
    /**
     * Sets the additional offset in table size that may occur when the `navigableHeaders` option
     * is enabled.
     *
     * @param {{x: number, y: number}} offset Offset as x and y properties.
     */
    _classPrivateMethodInitSpec(this, _setOffsetSize);
    /**
     * Instance of the SelectionRange, holder for visual coordinates applied to the table.
     *
     * @type {SelectionRange}
     */
    _classPrivateFieldInitSpec(this, _range, {
      writable: true,
      value: void 0
    });
    /**
     * Additional options which define the state of the settings which can infer transformation and
     * give the possibility to translate indexes.
     *
     * @type {object}
     */
    _classPrivateFieldInitSpec(this, _options, {
      writable: true,
      value: void 0
    });
    /**
     * Increases the table size by applying the offsets. The option is used by the `navigableHeaders`
     * option.
     *
     * @type {{ x: number, y: number }}
     */
    _classPrivateFieldInitSpec(this, _offset, {
      writable: true,
      value: {
        x: 0,
        y: 0
      }
    });
    _classPrivateFieldSet(this, _range, range);
    _classPrivateFieldSet(this, _options, options);
  }

  /**
   * Selects cell relative to the current cell (if possible).
   *
   * @param {number} rowDelta Rows number to move, value can be passed as negative number.
   * @param {number} colDelta Columns number to move, value can be passed as negative number.
   * @param {boolean} [createMissingRecords=false] If `true` the new rows/columns will be created if necessary. Otherwise, row/column will
   *                        be created according to `minSpareRows/minSpareCols` settings of Handsontable.
   * @returns {CellCoords} Visual coordinates after transformation.
   */
  _createClass(Transformation, [{
    key: "transformStart",
    value: function transformStart(rowDelta, colDelta) {
      var createMissingRecords = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
      _classPrivateMethodGet(this, _setOffsetSize, _setOffsetSize2).call(this, {
        x: _classPrivateFieldGet(this, _options).navigableHeaders() ? _classPrivateFieldGet(this, _options).countRowHeaders() : 0,
        y: _classPrivateFieldGet(this, _options).navigableHeaders() ? _classPrivateFieldGet(this, _options).countColHeaders() : 0
      });
      var delta = _classPrivateFieldGet(this, _options).createCellCoords(rowDelta, colDelta);
      var visualCoords = _classPrivateFieldGet(this, _range).current().highlight;
      var highlightRenderableCoords = _classPrivateFieldGet(this, _options).visualToRenderableCoords(visualCoords);
      var rowTransformDir = 0;
      var colTransformDir = 0;
      this.runLocalHooks('beforeTransformStart', delta);
      if (highlightRenderableCoords.row !== null && highlightRenderableCoords.col !== null) {
        var _classPrivateMethodGe = _classPrivateMethodGet(this, _getTableSize, _getTableSize2).call(this),
          width = _classPrivateMethodGe.width,
          height = _classPrivateMethodGe.height;
        var _classPrivateMethodGe2 = _classPrivateMethodGet(this, _visualToZeroBasedCoords, _visualToZeroBasedCoords2).call(this, visualCoords),
          x = _classPrivateMethodGe2.x,
          y = _classPrivateMethodGe2.y;
        var fixedRowsBottom = _classPrivateFieldGet(this, _options).fixedRowsBottom();
        var minSpareRows = _classPrivateFieldGet(this, _options).minSpareRows();
        var minSpareCols = _classPrivateFieldGet(this, _options).minSpareCols();
        var autoWrapRow = _classPrivateFieldGet(this, _options).autoWrapRow();
        var autoWrapCol = _classPrivateFieldGet(this, _options).autoWrapCol();
        var rawCoords = {
          row: y + delta.row,
          col: x + delta.col
        };
        if (rawCoords.row >= height) {
          if (createMissingRecords && minSpareRows > 0 && fixedRowsBottom === 0) {
            this.runLocalHooks('insertRowRequire', _classPrivateFieldGet(this, _options).countRenderableRows());
          } else if (autoWrapCol) {
            var nextColumn = rawCoords.col + 1;
            rawCoords.row = rawCoords.row - height;
            rawCoords.col = nextColumn >= width ? nextColumn - width : nextColumn;
          }
        } else if (rawCoords.row < 0) {
          if (autoWrapCol) {
            var previousColumn = rawCoords.col - 1;
            rawCoords.row = height + rawCoords.row;
            rawCoords.col = previousColumn < 0 ? width + previousColumn : previousColumn;
          }
        }
        if (rawCoords.col >= width) {
          if (createMissingRecords && minSpareCols > 0) {
            this.runLocalHooks('insertColRequire', _classPrivateFieldGet(this, _options).countRenderableColumns());
          } else if (autoWrapRow) {
            var nextRow = rawCoords.row + 1;
            rawCoords.row = nextRow >= height ? nextRow - height : nextRow;
            rawCoords.col = rawCoords.col - width;
          }
        } else if (rawCoords.col < 0) {
          if (autoWrapRow) {
            var previousRow = rawCoords.row - 1;
            rawCoords.row = previousRow < 0 ? height + previousRow : previousRow;
            rawCoords.col = width + rawCoords.col;
          }
        }
        var coords = _classPrivateFieldGet(this, _options).createCellCoords(rawCoords.row, rawCoords.col);
        var _classPrivateMethodGe3 = _classPrivateMethodGet(this, _clampCoords, _clampCoords2).call(this, coords),
          rowDir = _classPrivateMethodGe3.rowDir,
          colDir = _classPrivateMethodGe3.colDir;
        rowTransformDir = rowDir;
        colTransformDir = colDir;
        visualCoords = _classPrivateMethodGet(this, _zeroBasedToVisualCoords, _zeroBasedToVisualCoords2).call(this, coords);
      }
      this.runLocalHooks('afterTransformStart', visualCoords, rowTransformDir, colTransformDir);
      return visualCoords;
    }

    /**
     * Sets selection end cell relative to the current selection end cell (if possible).
     *
     * @param {number} rowDelta Rows number to move, value can be passed as negative number.
     * @param {number} colDelta Columns number to move, value can be passed as negative number.
     * @returns {CellCoords} Visual coordinates after transformation.
     */
  }, {
    key: "transformEnd",
    value: function transformEnd(rowDelta, colDelta) {
      _classPrivateMethodGet(this, _setOffsetSize, _setOffsetSize2).call(this, {
        x: _classPrivateFieldGet(this, _options).navigableHeaders() ? _classPrivateFieldGet(this, _options).countRowHeaders() : 0,
        y: _classPrivateFieldGet(this, _options).navigableHeaders() ? _classPrivateFieldGet(this, _options).countColHeaders() : 0
      });
      var delta = _classPrivateFieldGet(this, _options).createCellCoords(rowDelta, colDelta);
      var cellRange = _classPrivateFieldGet(this, _range).current();
      var highlightRenderableCoords = _classPrivateFieldGet(this, _options).visualToRenderableCoords(cellRange.highlight);
      var visualCoords = cellRange.to.clone();
      var rowTransformDir = 0;
      var colTransformDir = 0;
      this.runLocalHooks('beforeTransformEnd', delta);
      if (highlightRenderableCoords.row !== null && highlightRenderableCoords.col !== null) {
        var _classPrivateMethodGe4 = _classPrivateMethodGet(this, _visualToZeroBasedCoords, _visualToZeroBasedCoords2).call(this, cellRange.to),
          x = _classPrivateMethodGe4.x,
          y = _classPrivateMethodGe4.y;
        var rawCoords = {
          row: y + delta.row,
          col: x + delta.col
        };
        var coords = _classPrivateFieldGet(this, _options).createCellCoords(rawCoords.row, rawCoords.col);
        var _classPrivateMethodGe5 = _classPrivateMethodGet(this, _clampCoords, _clampCoords2).call(this, coords),
          rowDir = _classPrivateMethodGe5.rowDir,
          colDir = _classPrivateMethodGe5.colDir;
        rowTransformDir = rowDir;
        colTransformDir = colDir;
        var newVisualCoords = _classPrivateMethodGet(this, _zeroBasedToVisualCoords, _zeroBasedToVisualCoords2).call(this, coords);
        if (delta.row === 0 && delta.col !== 0) {
          visualCoords.col = newVisualCoords.col;
        } else if (delta.row !== 0 && delta.col === 0) {
          visualCoords.row = newVisualCoords.row;
        } else {
          visualCoords.row = newVisualCoords.row;
          visualCoords.col = newVisualCoords.col;
        }
      }
      this.runLocalHooks('afterTransformEnd', visualCoords, rowTransformDir, colTransformDir);
      return visualCoords;
    }
  }]);
  return Transformation;
}();
function _setOffsetSize2(_ref) {
  var x = _ref.x,
    y = _ref.y;
  _classPrivateFieldSet(this, _offset, {
    x: x,
    y: y
  });
}
function _clampCoords2(zeroBasedCoords) {
  var _classPrivateMethodGe6 = _classPrivateMethodGet(this, _getTableSize, _getTableSize2).call(this),
    width = _classPrivateMethodGe6.width,
    height = _classPrivateMethodGe6.height;
  var rowDir = 0;
  var colDir = 0;
  if (zeroBasedCoords.row < 0) {
    rowDir = -1;
    zeroBasedCoords.row = 0;
  } else if (zeroBasedCoords.row > 0 && zeroBasedCoords.row >= height) {
    rowDir = 1;
    zeroBasedCoords.row = height - 1;
  }
  if (zeroBasedCoords.col < 0) {
    colDir = -1;
    zeroBasedCoords.col = 0;
  } else if (zeroBasedCoords.col > 0 && zeroBasedCoords.col >= width) {
    colDir = 1;
    zeroBasedCoords.col = width - 1;
  }
  return {
    rowDir: rowDir,
    colDir: colDir
  };
}
function _getTableSize2() {
  return {
    width: _classPrivateFieldGet(this, _offset).x + _classPrivateFieldGet(this, _options).countRenderableColumns(),
    height: _classPrivateFieldGet(this, _offset).y + _classPrivateFieldGet(this, _options).countRenderableRows()
  };
}
function _visualToZeroBasedCoords2(visualCoords) {
  var _classPrivateFieldGet2 = _classPrivateFieldGet(this, _options).visualToRenderableCoords(visualCoords),
    row = _classPrivateFieldGet2.row,
    col = _classPrivateFieldGet2.col;
  return {
    x: _classPrivateFieldGet(this, _offset).x + col,
    y: _classPrivateFieldGet(this, _offset).y + row
  };
}
function _zeroBasedToVisualCoords2(zeroBasedCoords) {
  zeroBasedCoords.col = zeroBasedCoords.col - _classPrivateFieldGet(this, _offset).x;
  zeroBasedCoords.row = zeroBasedCoords.row - _classPrivateFieldGet(this, _offset).y;
  return _classPrivateFieldGet(this, _options).renderableToVisualCoords(zeroBasedCoords);
}
mixin(Transformation, localHooks);
export default Transformation;