"use strict";

var _typeof = require("@babel/runtime-corejs3/helpers/typeof");

var _WeakMap = require("@babel/runtime-corejs3/core-js-stable/weak-map");

var _Object$getOwnPropertyDescriptor = require("@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor");

var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = getTableHeaderRenderTemplate;

var _extends2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/extends"));

var _map = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/map"));

var _flat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/flat"));

var _slice = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/slice"));

var _concat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/concat"));

var _react = _interopRequireWildcard(require("react"));

var _reactDom = _interopRequireDefault(require("react-dom"));

var _classnames = _interopRequireDefault(require("classnames"));

var _htmlTable = require("./html-table");

var _styles = require("./styles");

var _utils = require("./utils");

var _utils2 = require("../utils");

var _header = _interopRequireDefault(require("./header"));

function _getRequireWildcardCache(nodeInterop) { if (typeof _WeakMap !== "function") return null; var cacheBabelInterop = new _WeakMap(); var cacheNodeInterop = new _WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && _Object$getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? _Object$getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

/*
 * @Author: wqhui
 * @Date: 2022-03-14 11:21:12
 * @Description: 不同情况的渲染模板，比如IE下的
 */
var TEMPLATES = new _map.default();

function renderTableHeaderInIE(info, props) {
  var stickyTop = props.stickyTop,
      hasHeader = props.hasHeader;
  var flat = (0, _flat.default)(info),
      nested = info.nested,
      visible = info.visible,
      hasLockColumn = info.hasLockColumn;
  var left = flat.left,
      right = flat.right;
  var leftNested = nested.left,
      rightNested = nested.right,
      full = nested.full;
  var rowCount = (0, _utils2.getTreeDepth)(full) + 1;
  return /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _classnames.default)(_styles.Classes.tableHeader)
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _classnames.default)(_styles.Classes.tableHeaderMain, 'no-scrollbar'),
    style: {
      top: stickyTop === 0 ? undefined : stickyTop,
      display: hasHeader ? undefined : 'none'
    }
  }, /*#__PURE__*/_react.default.createElement(_header.default, {
    info: info,
    theaderPosition: hasLockColumn ? 'center' : undefined
  })), left.length > 0 ? /*#__PURE__*/_react.default.createElement("div", {
    className: _styles.Classes.fixedLeft
  }, /*#__PURE__*/_react.default.createElement(_header.default, {
    info: (0, _extends2.default)((0, _extends2.default)({}, info), {
      flat: {
        center: left,
        full: left,
        left: [],
        right: []
      },
      nested: {
        center: leftNested,
        full: leftNested,
        left: [],
        right: []
      },
      visible: (0, _slice.default)(visible).call(visible, 0, left.length),
      horizontalRenderRange: {
        leftIndex: 0,
        leftBlank: 0,
        rightIndex: left.length,
        rightBlank: 0
      }
    }),
    theaderPosition: "left",
    rowCount: rowCount
  })) : null, right.length > 0 ? /*#__PURE__*/_react.default.createElement("div", {
    className: _styles.Classes.fixedRight
  }, /*#__PURE__*/_react.default.createElement(_header.default, {
    info: (0, _extends2.default)((0, _extends2.default)({}, info), {
      flat: {
        center: right,
        full: right,
        left: [],
        right: []
      },
      nested: {
        center: rightNested,
        full: rightNested,
        left: [],
        right: []
      },
      visible: (0, _slice.default)(visible).call(visible, visible.length - right.length),
      horizontalRenderRange: {
        leftIndex: 0,
        leftBlank: 0,
        rightIndex: right.length,
        rightBlank: 0
      }
    }),
    theaderPosition: 'right',
    rowCount: rowCount
  })) : null);
}

function renderTableBodyInIE(info, props, extra) {
  var dataSource = props.dataSource,
      getRowProps = props.getRowProps,
      primaryKey = props.primaryKey;
  var _info$verticalRenderR = info.verticalRenderRange,
      topIndex = _info$verticalRenderR.topIndex,
      bottomBlank = _info$verticalRenderR.bottomBlank,
      topBlank = _info$verticalRenderR.topBlank,
      bottomIndex = _info$verticalRenderR.bottomIndex;
  var flat = (0, _flat.default)(info),
      visible = info.visible,
      hasLockColumn = info.hasLockColumn;
  var left = flat.left,
      right = flat.right;
  var verticalRenderInfo = {
    first: 0,
    offset: topIndex,
    limit: bottomIndex,
    last: dataSource.length - 1
  };
  var commonProps = {
    getRowProps: (0, _utils.composeRowPropsGetter)(getRowProps, extra.rowProps),
    primaryKey: primaryKey,
    data: (0, _slice.default)(dataSource).call(dataSource, topIndex, bottomIndex)
  };
  var fixedRightTableStyle = {
    right: -extra.stickyRightOffset
  };
  return /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _classnames.default)(_styles.Classes.tableBody, _styles.Classes.horizontalScrollContainer)
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: _styles.Classes.virtual,
    tabIndex: -1
  }, topBlank > 0 && /*#__PURE__*/_react.default.createElement("div", {
    key: "top-blank",
    className: (0, _classnames.default)(_styles.Classes.virtualBlank, 'top'),
    style: {
      height: topBlank
    }
  }), /*#__PURE__*/_react.default.createElement(_htmlTable.HtmlTable, (0, _extends2.default)({
    tbodyHtmlTag: "tbody"
  }, commonProps, {
    tbodyPosition: hasLockColumn ? 'center' : undefined,
    horizontalRenderInfo: info,
    verticalRenderInfo: verticalRenderInfo
  })), bottomBlank > 0 && /*#__PURE__*/_react.default.createElement("div", {
    key: "bottom-blank",
    className: (0, _classnames.default)(_styles.Classes.virtualBlank, 'bottom'),
    style: {
      height: bottomBlank
    }
  })), left.length > 0 ? /*#__PURE__*/_react.default.createElement("div", {
    className: _styles.Classes.fixedLeft
  }, topBlank > 0 && /*#__PURE__*/_react.default.createElement("div", {
    key: "top-blank",
    className: (0, _classnames.default)(_styles.Classes.virtualBlank, 'top'),
    style: {
      height: topBlank
    }
  }), /*#__PURE__*/_react.default.createElement(_htmlTable.HtmlTable, (0, _extends2.default)({
    tbodyHtmlTag: "tbody"
  }, commonProps, {
    tbodyPosition: "left",
    horizontalRenderInfo: (0, _extends2.default)((0, _extends2.default)({}, info), {
      flat: {
        center: left,
        full: left,
        left: [],
        right: []
      },
      visible: (0, _slice.default)(visible).call(visible, 0, left.length)
    }),
    verticalRenderInfo: verticalRenderInfo
  })), bottomBlank > 0 && /*#__PURE__*/_react.default.createElement("div", {
    key: "bottom-blank",
    className: (0, _classnames.default)(_styles.Classes.virtualBlank, 'bottom'),
    style: {
      height: bottomBlank
    }
  })) : null, right.length > 0 ? /*#__PURE__*/_react.default.createElement("div", {
    className: _styles.Classes.fixedRight,
    style: fixedRightTableStyle
  }, topBlank > 0 && /*#__PURE__*/_react.default.createElement("div", {
    key: "top-blank",
    className: (0, _classnames.default)(_styles.Classes.virtualBlank, 'top'),
    style: {
      height: topBlank
    }
  }), /*#__PURE__*/_react.default.createElement(_htmlTable.HtmlTable, (0, _extends2.default)({
    tbodyHtmlTag: "tbody"
  }, commonProps, {
    tbodyPosition: "right",
    stickyRightOffset: extra.stickyRightOffset,
    horizontalRenderInfo: (0, _extends2.default)((0, _extends2.default)({}, info), {
      flat: {
        center: right,
        full: right,
        left: [],
        right: []
      },
      visible: (0, _slice.default)(visible).call(visible, visible.length - right.length)
    }),
    verticalRenderInfo: verticalRenderInfo
  })), bottomBlank > 0 && /*#__PURE__*/_react.default.createElement("div", {
    key: "bottom-blank",
    className: (0, _classnames.default)(_styles.Classes.virtualBlank, 'bottom'),
    style: {
      height: bottomBlank
    }
  })) : null, /*#__PURE__*/_react.default.createElement("div", {
    className: _styles.Classes.rowDetailContainer
  }));
}

function renderTableFooterInIE(info, props, extra) {
  var _props$footerDataSour = props.footerDataSource,
      footerDataSource = _props$footerDataSour === void 0 ? [] : _props$footerDataSour,
      getRowProps = props.getRowProps,
      primaryKey = props.primaryKey,
      stickyBottom = props.stickyBottom;

  var _getRowProps = (0, _utils.composeRowPropsGetter)(getRowProps, extra.rowProps);

  var flat = (0, _flat.default)(info),
      visible = info.visible,
      hasLockColumn = info.hasLockColumn;
  var left = flat.left,
      right = flat.right;
  var verticalRenderInfo = {
    offset: 0,
    first: 0,
    last: footerDataSource.length - 1,
    limit: Infinity
  };
  var commonProps = {
    data: footerDataSource,
    getRowProps: _getRowProps,
    primaryKey: primaryKey,
    verticalRenderInfo: verticalRenderInfo
  };
  return /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _classnames.default)(_styles.Classes.tableFooter),
    style: {
      bottom: stickyBottom === 0 ? undefined : stickyBottom
    }
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _classnames.default)(_styles.Classes.tableFooterMain, _styles.Classes.horizontalScrollContainer)
  }, /*#__PURE__*/_react.default.createElement(_htmlTable.HtmlTable, (0, _extends2.default)({
    tbodyHtmlTag: "tfoot"
  }, commonProps, {
    tbodyPosition: hasLockColumn ? 'center' : undefined,
    horizontalRenderInfo: info
  }))), left.length > 0 ? /*#__PURE__*/_react.default.createElement("div", {
    className: _styles.Classes.fixedLeft
  }, /*#__PURE__*/_react.default.createElement(_htmlTable.HtmlTable, (0, _extends2.default)({
    tbodyHtmlTag: "tfoot"
  }, commonProps, {
    tbodyPosition: "left",
    horizontalRenderInfo: (0, _extends2.default)((0, _extends2.default)({}, info), {
      flat: {
        center: left,
        full: left,
        left: [],
        right: []
      },
      visible: (0, _slice.default)(visible).call(visible, 0, left.length)
    })
  }))) : null, right.length > 0 ? /*#__PURE__*/_react.default.createElement("div", {
    className: _styles.Classes.fixedRight
  }, /*#__PURE__*/_react.default.createElement(_htmlTable.HtmlTable, (0, _extends2.default)({
    tbodyHtmlTag: "tfoot"
  }, commonProps, {
    tbodyPosition: "right",
    horizontalRenderInfo: (0, _extends2.default)((0, _extends2.default)({}, info), {
      flat: {
        center: right,
        full: right,
        left: [],
        right: []
      },
      visible: (0, _slice.default)(visible).call(visible, visible.length - right.length)
    })
  }))) : null);
}

function renderRowDetailInIE(params) {
  var domHelper = params.domHelper;
  if (!domHelper) return;
  var artTable = domHelper.artTable;
  var rowDetailContainer = artTable && artTable.querySelector(".".concat(_styles.Classes.rowDetailContainer));
  return /*#__PURE__*/_reactDom.default.createPortal( /*#__PURE__*/_react.default.createElement(RowDetail, (0, _extends2.default)({}, params)), rowDetailContainer);
}

function RowDetail(props) {
  var detailRef = (0, _react.useRef)(null);
  var row = props.row,
      rowIndex = props.rowIndex,
      domHelper = props.domHelper,
      renderDetail = props.renderDetail;
  var artTable = domHelper.artTable;
  (0, _react.useEffect)(function () {
    var selector = function selector(position) {
      var _context, _context2;

      return (0, _concat.default)(_context = (0, _concat.default)(_context2 = ".".concat(position, " .")).call(_context2, _styles.Classes.tableRow, "[data-rowindex=\"")).call(_context, rowIndex, "\"]");
    };

    var itemRect = detailRef.current && detailRef.current.getBoundingClientRect();
    var targetRow = artTable.querySelector(selector(_styles.Classes.tableBody));
    var targetRowLeft = artTable.querySelector(selector(_styles.Classes.fixedLeft));
    var targetRowRight = artTable.querySelector(selector(_styles.Classes.fixedRight));

    if (itemRect.height) {
      targetRow && (targetRow.style.height = itemRect.height + 'px');
      targetRowLeft && (targetRowLeft.style.height = itemRect.height + 'px');
      targetRowRight && (targetRowRight.style.height = itemRect.height + 'px');
    }
  }, []);
  (0, _react.useEffect)(function () {
    // 这时候行才渲染完，只能在这里设置偏移量
    var offsetTop = domHelper.getRowTop(rowIndex) || 0;
    detailRef.current.style.transform = "translateY(".concat(offsetTop + 'px', ")");
  });
  return /*#__PURE__*/_react.default.createElement("div", {
    ref: detailRef,
    className: _styles.Classes.rowDetailItem
  }, renderDetail(row, rowIndex));
}

if (_utils2.browserType.isIE) {
  TEMPLATES.set('header', renderTableHeaderInIE);
  TEMPLATES.set('body', renderTableBodyInIE);
  TEMPLATES.set('footer', renderTableFooterInIE);
  TEMPLATES.set('rowDetail', renderRowDetailInIE);
}

function getTableHeaderRenderTemplate(type) {
  return TEMPLATES.get(type);
}