import _classCallCheck from "@babel/runtime-corejs3/helpers/classCallCheck";
import _createClass from "@babel/runtime-corejs3/helpers/createClass";
import _sliceInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/slice";
import _Array$from from "@babel/runtime-corejs3/core-js-stable/array/from";
import _concatInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/concat";
import { Classes } from '../styles'; // 表格 DOM 结构
// div.art-table-wrapper
// └── div.art-loading-wrapper
//     ├── div.art-loading-indicator-wrapper
//     │   └── div.art-loading-indicator
//     │
//     └── div.art-loading-content-wrapper
//         ├── div.art-table
//         │   │
//         │   ├── div.art-table-header
//         │   │  └── table
//         │   │      ├── colgroup
//         │   │      └── thead  注意这里会出现自定义内容，可能存在嵌套表格
//         │   │
//         │   ├── div.art-table-body
//         │   │   ├── div.art-virtual-blank.top
//         │   │   ├── table
//         │   │   │   ├── colgroup
//         │   │   │   └── tbody 注意这里会出现自定义内容，可能存在嵌套表格
//         │   │   └── div.art-virtual-blank.bottom
//         │   │
//         │   ├── div.art-table-footer
//         │   │  └── table
//         │   │      ├── colgroup
//         │   │      └── tfoot  注意这里会出现自定义内容，可能存在嵌套表格
//         │   │
//         │   ├── div.art-lock-shadow-mask
//         │   │   └── div.art-left-lock-shadow
//         │   └── div.art-lock-shadow-mask
//         │       └── div.art-right-lock-shadow
//         │
//         └── div.art-sticky-scroll
//             └── div.art-sticky-scroll-item
//
// 在「可能存在嵌套表格」的情况下，我们可以采用以下的方式来避免「querySelector 不小心获取到了的嵌套表格上的元素」：
//  artTable.querySelector('.art-lock-shadow-mask .art-left-lock-shadow')
// 表格 DOM 结构辅助工具

export var TableDOMHelper = /*#__PURE__*/function () {
  function TableDOMHelper(artTableWrapper) {
    var _this = this,
        _context2;

    _classCallCheck(this, TableDOMHelper);

    this.getRowNodeListByEvent = function (e) {
      var nodeList = null;
      var rowIndex = e.currentTarget.dataset.rowindex;

      if (rowIndex !== undefined) {
        var targetParent = _this.tableBody.contains(e.currentTarget) ? _this.tableBody : _this.tableFooter;
        nodeList = targetParent.querySelectorAll("tr[data-rowindex=\"".concat(rowIndex, "\"]"));
      }

      return nodeList;
    };

    this.getInRangeRowByCellEvent = function (e) {
      var _context;

      var _a;

      var getParentNode = function getParentNode(ele, target) {
        if (ele.parentNode.nodeName === target) {
          return ele.parentNode;
        }

        return getParentNode(ele.parentNode, target);
      };

      e = e instanceof Array ? e[0] : e;
      var curCell = e === null || e === void 0 ? void 0 : e.currentTarget;
      var curRow = getParentNode(curCell, 'TR');
      var curRowSpan = parseInt(e.currentTarget.getAttribute('rowspan')) || 1;
      var rows = (_a = getParentNode(curCell, 'TABLE')) === null || _a === void 0 ? void 0 : _a.rows;
      return _sliceInstanceProperty(_context = _Array$from(rows)).call(_context, curRow.rowIndex, curRow.rowIndex + curRowSpan);
    };

    this.artTableWrapper = artTableWrapper;
    this.artTable = artTableWrapper.querySelector(".".concat(Classes.artTable));
    this.tableHeader = this.artTable.querySelector(".".concat(Classes.tableHeader));
    this.tableHeaderMain = this.artTable.querySelector(".".concat(Classes.tableHeaderMain));
    this.tableBody = this.artTable.querySelector(".".concat(Classes.tableBody));
    this.virtual = this.artTable.querySelector(".".concat(Classes.virtual));
    this.tableElement = this.artTable.querySelector(".".concat(Classes.tableBody, " table"));
    this.tableFooter = this.artTable.querySelector(".".concat(Classes.tableFooter));
    this.tableFooterMain = this.artTable.querySelector(".".concat(Classes.tableFooterMain));

    var stickyScrollSelector = _concatInstanceProperty(_context2 = ".".concat(Classes.artTable, " + .")).call(_context2, Classes.stickyScroll);

    this.stickyScroll = artTableWrapper.querySelector(stickyScrollSelector);
    this.stickyScrollItem = this.stickyScroll.querySelector(".".concat(Classes.stickyScrollItem));
  }

  _createClass(TableDOMHelper, [{
    key: "getVirtualTop",
    value: function getVirtualTop() {
      return this.tableBody.querySelector(".".concat(Classes.virtualBlank, ".top"));
    }
  }, {
    key: "getTableRows",
    value: function getTableRows() {
      var _context3;

      var tbody = this.artTable.querySelector(_concatInstanceProperty(_context3 = ".".concat(Classes.tableBody, " .")).call(_context3, Classes.virtual, " table tbody"));
      return tbody.childNodes;
    }
  }, {
    key: "getTableBodyHtmlTable",
    value: function getTableBodyHtmlTable() {
      var _context4;

      return this.artTable.querySelector(_concatInstanceProperty(_context4 = ".".concat(Classes.tableBody, " .")).call(_context4, Classes.virtual, " table"));
    }
  }, {
    key: "getLeftLockShadow",
    value: function getLeftLockShadow() {
      var _context5;

      var selector = _concatInstanceProperty(_context5 = ".".concat(Classes.lockShadowMask, " .")).call(_context5, Classes.leftLockShadow);

      return this.artTable.querySelector(selector);
    }
  }, {
    key: "getRightLockShadow",
    value: function getRightLockShadow() {
      var _context6;

      var selector = _concatInstanceProperty(_context6 = ".".concat(Classes.lockShadowMask, " .")).call(_context6, Classes.rightLockShadow);

      return this.artTable.querySelector(selector);
    }
  }, {
    key: "getLoadingIndicator",
    value: function getLoadingIndicator() {
      return this.artTableWrapper.querySelector(".".concat(Classes.loadingIndicator));
    }
  }, {
    key: "getRowTop",
    value: function getRowTop(rowIndex) {
      var _context7, _context8;

      if (rowIndex === 0) return 0;

      var selector = _concatInstanceProperty(_context7 = _concatInstanceProperty(_context8 = ".".concat(Classes.tableBody, " .")).call(_context8, Classes.tableRow, "[data-rowindex=\"")).call(_context7, rowIndex, "\"]");

      var row = this.artTable.querySelector(selector);
      var rowOffsetTop = row && row.offsetTop || 0;
      var tableOffsetTop = this.tableElement.offsetTop || 0;
      return rowOffsetTop + tableOffsetTop;
    }
  }]);

  return TableDOMHelper;
}();