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); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import "core-js/modules/es.array.iterator.js";
import "core-js/modules/es.object.to-string.js";
import "core-js/modules/es.set.js";
import "core-js/modules/es.string.iterator.js";
import "core-js/modules/web.dom-collections.iterator.js";
import "core-js/modules/es.weak-map.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";
import "core-js/modules/es.array.slice.js";
import "core-js/modules/es.regexp.to-string.js";
import "core-js/modules/es.function.name.js";
import "core-js/modules/es.array.from.js";
import "core-js/modules/es.regexp.exec.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 _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return fn; }
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 _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; } }
/* eslint-disable no-continue */
import { addClass } from "../../../../helpers/dom/element.mjs";
/**
 * Selection scanner module scans the rendered cells and headers and if it finds an intersection with
 * the coordinates of the Selection class (highlight) it returns the DOM elements.
 *
 * @private
 */
var _selection = /*#__PURE__*/new WeakMap();
var _activeOverlaysWot = /*#__PURE__*/new WeakMap();
var _scanCellsRange = /*#__PURE__*/new WeakSet();
export var SelectionScanner = /*#__PURE__*/function () {
  function SelectionScanner() {
    _classCallCheck(this, SelectionScanner);
    /**
     * The method triggers a callback for each rendered cell.
     *
     * @param {function(number, number): void} callback The callback function to trigger.
     */
    _classPrivateMethodInitSpec(this, _scanCellsRange);
    /**
     * Active Selection instance to process.
     *
     * @type {Selection}
     */
    _classPrivateFieldInitSpec(this, _selection, {
      writable: true,
      value: void 0
    });
    /**
     * The Walkontable instance that the scans depends on.
     *
     * @type {Walkontable}
     */
    _classPrivateFieldInitSpec(this, _activeOverlaysWot, {
      writable: true,
      value: void 0
    });
  }
  _createClass(SelectionScanner, [{
    key: "setActiveOverlay",
    value:
    /**
     * Sets the Walkontable instance that will be taking into account while scanning the table.
     *
     * @param {Walkontable} activeOverlaysWot The Walkontable instance.
     * @returns {SelectionScanner}
     */
    function setActiveOverlay(activeOverlaysWot) {
      _classPrivateFieldSet(this, _activeOverlaysWot, activeOverlaysWot);
      return this;
    }

    /**
     * Sets the Selection instance to process.
     *
     * @param {Selection} selection The Selection instance.
     * @returns {SelectionScanner}
     */
  }, {
    key: "setActiveSelection",
    value: function setActiveSelection(selection) {
      _classPrivateFieldSet(this, _selection, selection);
      return this;
    }

    /**
     * Scans the rendered table with selection and returns elements that intersects
     * with selection coordinates.
     *
     * @returns {HTMLTableElement[]}
     */
  }, {
    key: "scan",
    value: function scan() {
      var selectionType = _classPrivateFieldGet(this, _selection).settings.selectionType;
      var elements = new Set();

      // TODO(improvement): use heuristics from coords to detect what type of scan
      // the Selection needs instead of using `selectionType` property.
      if (selectionType === 'active-header') {
        this.scanColumnsInHeadersRange(function (element) {
          return elements.add(element);
        });
        this.scanRowsInHeadersRange(function (element) {
          return elements.add(element);
        });
      } else if (selectionType === 'area') {
        this.scanCellsRange(function (element) {
          return elements.add(element);
        });
      } else if (selectionType === 'focus') {
        this.scanColumnsInHeadersRange(function (element) {
          return elements.add(element);
        });
        this.scanRowsInHeadersRange(function (element) {
          return elements.add(element);
        });
        this.scanCellsRange(function (element) {
          return elements.add(element);
        });
      } else if (selectionType === 'fill') {
        this.scanCellsRange(function (element) {
          return elements.add(element);
        });
      } else if (selectionType === 'header') {
        this.scanColumnsInHeadersRange(function (element) {
          return elements.add(element);
        });
        this.scanRowsInHeadersRange(function (element) {
          return elements.add(element);
        });
      } else if (selectionType === 'row') {
        this.scanRowsInHeadersRange(function (element) {
          return elements.add(element);
        });
        this.scanRowsInCellsRange(function (element) {
          return elements.add(element);
        });
      } else if (selectionType === 'column') {
        this.scanColumnsInHeadersRange(function (element) {
          return elements.add(element);
        });
        this.scanColumnsInCellsRange(function (element) {
          return elements.add(element);
        });
      }
      return elements;
    }

    /**
     * Scans the table (only rendered headers) and collect all column headers (TH) that match
     * the coordinates passed in the Selection instance.
     *
     * @param {function(HTMLTableElement): void} callback The callback function to trigger.
     */
  }, {
    key: "scanColumnsInHeadersRange",
    value: function scanColumnsInHeadersRange(callback) {
      var _classPrivateFieldGet2 = _classPrivateFieldGet(this, _selection).getCorners(),
        _classPrivateFieldGet3 = _slicedToArray(_classPrivateFieldGet2, 4),
        topRow = _classPrivateFieldGet3[0],
        topColumn = _classPrivateFieldGet3[1],
        bottomRow = _classPrivateFieldGet3[2],
        bottomColumn = _classPrivateFieldGet3[3];
      var _classPrivateFieldGet4 = _classPrivateFieldGet(this, _activeOverlaysWot),
        wtTable = _classPrivateFieldGet4.wtTable;
      var renderedColumnsCount = wtTable.getRenderedColumnsCount();
      var columnHeadersCount = wtTable.getColumnHeadersCount();
      var cursor = 0;
      for (var column = -wtTable.getRowHeadersCount(); column < renderedColumnsCount; column++) {
        var sourceColumn = wtTable.columnFilter.renderedToSource(column);
        if (sourceColumn < topColumn || sourceColumn > bottomColumn) {
          continue;
        }
        for (var headerLevel = -columnHeadersCount; headerLevel < 0; headerLevel++) {
          if (headerLevel < topRow || headerLevel > bottomRow) {
            continue;
          }
          var positiveBasedHeaderLevel = headerLevel + columnHeadersCount;
          var TH = wtTable.getColumnHeader(sourceColumn, positiveBasedHeaderLevel);
          var newSourceCol = _classPrivateFieldGet(this, _activeOverlaysWot).getSetting('onBeforeHighlightingColumnHeader', sourceColumn, positiveBasedHeaderLevel, {
            selectionType: _classPrivateFieldGet(this, _selection).settings.selectionType,
            columnCursor: cursor,
            selectionWidth: bottomColumn - topColumn + 1
          });
          if (newSourceCol === null) {
            continue;
          }
          if (newSourceCol !== sourceColumn) {
            TH = wtTable.getColumnHeader(newSourceCol, positiveBasedHeaderLevel);
          }
          callback(TH);
        }
        cursor += 1;
      }
    }

    /**
     * Scans the table (only rendered headers) and collect all row headers (TH) that match
     * the coordinates passed in the Selection instance.
     *
     * @param {function(HTMLTableElement): void} callback The callback function to trigger.
     */
  }, {
    key: "scanRowsInHeadersRange",
    value: function scanRowsInHeadersRange(callback) {
      var _classPrivateFieldGet5 = _classPrivateFieldGet(this, _selection).getCorners(),
        _classPrivateFieldGet6 = _slicedToArray(_classPrivateFieldGet5, 4),
        topRow = _classPrivateFieldGet6[0],
        topColumn = _classPrivateFieldGet6[1],
        bottomRow = _classPrivateFieldGet6[2],
        bottomColumn = _classPrivateFieldGet6[3];
      var _classPrivateFieldGet7 = _classPrivateFieldGet(this, _activeOverlaysWot),
        wtTable = _classPrivateFieldGet7.wtTable;
      var renderedRowsCount = wtTable.getRenderedRowsCount();
      var rowHeadersCount = wtTable.getRowHeadersCount();
      var cursor = 0;
      for (var row = -wtTable.getColumnHeadersCount(); row < renderedRowsCount; row++) {
        var sourceRow = wtTable.rowFilter.renderedToSource(row);
        if (sourceRow < topRow || sourceRow > bottomRow) {
          continue;
        }
        for (var headerLevel = -rowHeadersCount; headerLevel < 0; headerLevel++) {
          if (headerLevel < topColumn || headerLevel > bottomColumn) {
            continue;
          }
          var positiveBasedHeaderLevel = headerLevel + rowHeadersCount;
          var TH = wtTable.getRowHeader(sourceRow, positiveBasedHeaderLevel);
          var newSourceRow = _classPrivateFieldGet(this, _activeOverlaysWot).getSetting('onBeforeHighlightingRowHeader', sourceRow, positiveBasedHeaderLevel, {
            selectionType: _classPrivateFieldGet(this, _selection).settings.selectionType,
            rowCursor: cursor,
            selectionHeight: bottomRow - topRow + 1
          });
          if (newSourceRow === null) {
            continue;
          }
          if (newSourceRow !== sourceRow) {
            TH = wtTable.getRowHeader(newSourceRow, positiveBasedHeaderLevel);
          }
          callback(TH);
        }
        cursor += 1;
      }
    }

    /**
     * Scans the table (only rendered cells) and collect all cells (TR) that match
     * the coordinates passed in the Selection instance.
     *
     * @param {function(HTMLTableElement): void} callback The callback function to trigger.
     */
  }, {
    key: "scanCellsRange",
    value: function scanCellsRange(callback) {
      var _this = this;
      var _classPrivateFieldGet8 = _classPrivateFieldGet(this, _selection).getCorners(),
        _classPrivateFieldGet9 = _slicedToArray(_classPrivateFieldGet8, 4),
        topRow = _classPrivateFieldGet9[0],
        topColumn = _classPrivateFieldGet9[1],
        bottomRow = _classPrivateFieldGet9[2],
        bottomColumn = _classPrivateFieldGet9[3];
      var _classPrivateFieldGet10 = _classPrivateFieldGet(this, _activeOverlaysWot),
        wtTable = _classPrivateFieldGet10.wtTable;
      _classPrivateMethodGet(this, _scanCellsRange, _scanCellsRange2).call(this, function (sourceRow, sourceColumn) {
        if (sourceRow >= topRow && sourceRow <= bottomRow && sourceColumn >= topColumn && sourceColumn <= bottomColumn) {
          var cell = wtTable.getCell(_classPrivateFieldGet(_this, _activeOverlaysWot).createCellCoords(sourceRow, sourceColumn));

          // support for old API
          var additionalSelectionClass = _classPrivateFieldGet(_this, _activeOverlaysWot).getSetting('onAfterDrawSelection', sourceRow, sourceColumn, _classPrivateFieldGet(_this, _selection).settings.layerLevel);
          if (typeof additionalSelectionClass === 'string') {
            addClass(cell, additionalSelectionClass);
          }
          callback(cell);
        }
      });
    }

    /**
     * Scans the table (only rendered cells) and collects all cells (TR) that match the coordinates
     * passed in the Selection instance but only for the X axis (rows).
     *
     * @param {function(HTMLTableElement): void} callback The callback function to trigger.
     */
  }, {
    key: "scanRowsInCellsRange",
    value: function scanRowsInCellsRange(callback) {
      var _this2 = this;
      // eslint-disable-next-line comma-spacing
      var _classPrivateFieldGet11 = _classPrivateFieldGet(this, _selection).getCorners(),
        _classPrivateFieldGet12 = _slicedToArray(_classPrivateFieldGet11, 3),
        topRow = _classPrivateFieldGet12[0],
        bottomRow = _classPrivateFieldGet12[2];
      var _classPrivateFieldGet13 = _classPrivateFieldGet(this, _activeOverlaysWot),
        wtTable = _classPrivateFieldGet13.wtTable;
      _classPrivateMethodGet(this, _scanCellsRange, _scanCellsRange2).call(this, function (sourceRow, sourceColumn) {
        if (sourceRow >= topRow && sourceRow <= bottomRow) {
          var cell = wtTable.getCell(_classPrivateFieldGet(_this2, _activeOverlaysWot).createCellCoords(sourceRow, sourceColumn));
          callback(cell);
        }
      });
    }

    /**
     * Scans the table (only rendered cells) and collects all cells (TR) that match the coordinates
     * passed in the Selection instance but only for the Y axis (columns).
     *
     * @param {function(HTMLTableElement): void} callback The callback function to trigger.
     */
  }, {
    key: "scanColumnsInCellsRange",
    value: function scanColumnsInCellsRange(callback) {
      var _this3 = this;
      var _classPrivateFieldGet14 = _classPrivateFieldGet(this, _selection).getCorners(),
        _classPrivateFieldGet15 = _slicedToArray(_classPrivateFieldGet14, 4),
        topColumn = _classPrivateFieldGet15[1],
        bottomColumn = _classPrivateFieldGet15[3];
      var _classPrivateFieldGet16 = _classPrivateFieldGet(this, _activeOverlaysWot),
        wtTable = _classPrivateFieldGet16.wtTable;
      _classPrivateMethodGet(this, _scanCellsRange, _scanCellsRange2).call(this, function (sourceRow, sourceColumn) {
        if (sourceColumn >= topColumn && sourceColumn <= bottomColumn) {
          var cell = wtTable.getCell(_classPrivateFieldGet(_this3, _activeOverlaysWot).createCellCoords(sourceRow, sourceColumn));
          callback(cell);
        }
      });
    }
  }]);
  return SelectionScanner;
}();
function _scanCellsRange2(callback) {
  var _classPrivateFieldGet17 = _classPrivateFieldGet(this, _activeOverlaysWot),
    wtTable = _classPrivateFieldGet17.wtTable;
  var renderedRowsCount = wtTable.getRenderedRowsCount();
  var renderedColumnsCount = wtTable.getRenderedColumnsCount();
  for (var row = 0; row < renderedRowsCount; row += 1) {
    var sourceRow = wtTable.rowFilter.renderedToSource(row);
    for (var column = 0; column < renderedColumnsCount; column += 1) {
      callback(sourceRow, wtTable.columnFilter.renderedToSource(column));
    }
  }
}