import _extends from "@babel/runtime-corejs3/helpers/extends";
import _Map from "@babel/runtime-corejs3/core-js-stable/map";
import _flatInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/flat";
import _sliceInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/slice";
import _concatInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/concat";
/*
 * @Author: wqhui
 * @Date: 2022-03-14 11:21:12
 * @Description: 不同情况的渲染模板，比如IE下的
 */
import React, { useEffect, useRef } from 'react';
import ReactDom from 'react-dom';
import cx from 'classnames';
import { HtmlTable } from './html-table';
import { Classes } from './styles';
import { composeRowPropsGetter } from './utils';
import { getTreeDepth, isStickyUIDegrade } from '../utils';
import TableHeader from './header';
var TEMPLATES = new _Map();
function renderTableHeaderInIE(info, props, extra) {
  var stickyTop = props.stickyTop,
    hasHeader = props.hasHeader;
  var flat = _flatInstanceProperty(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 = getTreeDepth(full) + 1;
  var fixedRightTableStyle = {
    right: (extra === null || extra === void 0 ? void 0 : extra.stickyRightOffset) || 0
  };
  return /*#__PURE__*/React.createElement("div", {
    className: cx(Classes.tableHeader)
  }, /*#__PURE__*/React.createElement("div", {
    className: cx(Classes.tableHeaderMain, 'no-scrollbar'),
    style: {
      top: stickyTop === 0 ? undefined : stickyTop,
      display: hasHeader ? undefined : 'none'
    }
  }, /*#__PURE__*/React.createElement(TableHeader, {
    info: info,
    theaderPosition: hasLockColumn ? 'center' : undefined
  }), /*#__PURE__*/React.createElement("div", {
    className: Classes.verticalScrollPlaceholder,
    style: typeof (extra === null || extra === void 0 ? void 0 : extra.stickyRightOffset) === 'number' ? {
      width: extra === null || extra === void 0 ? void 0 : extra.stickyRightOffset
    } : undefined
  })), left.length > 0 ? (/*#__PURE__*/React.createElement("div", {
    className: Classes.fixedLeft
  }, /*#__PURE__*/React.createElement(TableHeader, {
    info: _extends(_extends({}, info), {
      flat: {
        center: left,
        full: left,
        left: [],
        right: []
      },
      nested: {
        center: leftNested,
        full: leftNested,
        left: [],
        right: []
      },
      visible: _sliceInstanceProperty(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.createElement("div", {
    className: Classes.fixedRight,
    style: fixedRightTableStyle
  }, /*#__PURE__*/React.createElement(TableHeader, {
    info: _extends(_extends({}, info), {
      flat: {
        center: right,
        full: right,
        left: [],
        right: []
      },
      nested: {
        center: rightNested,
        full: rightNested,
        left: [],
        right: []
      },
      visible: _sliceInstanceProperty(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 = _flatInstanceProperty(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: composeRowPropsGetter(getRowProps, extra.rowProps),
    primaryKey: primaryKey,
    data: _sliceInstanceProperty(dataSource).call(dataSource, topIndex, bottomIndex)
  };
  return /*#__PURE__*/React.createElement("div", {
    className: cx(Classes.tableBody, Classes.horizontalScrollContainer)
  }, /*#__PURE__*/React.createElement("div", {
    className: Classes.virtual,
    tabIndex: -1
  }, topBlank > 0 && (/*#__PURE__*/React.createElement("div", {
    key: "top-blank",
    className: cx(Classes.virtualBlank, 'top'),
    style: {
      height: topBlank
    }
  })), /*#__PURE__*/React.createElement(HtmlTable, _extends({
    tbodyHtmlTag: "tbody"
  }, commonProps, {
    tbodyPosition: hasLockColumn ? 'center' : undefined,
    horizontalRenderInfo: info,
    verticalRenderInfo: verticalRenderInfo
  })), bottomBlank > 0 && (/*#__PURE__*/React.createElement("div", {
    key: "bottom-blank",
    className: cx(Classes.virtualBlank, 'bottom'),
    style: {
      height: bottomBlank
    }
  }))), left.length > 0 ? (/*#__PURE__*/React.createElement("div", {
    className: Classes.fixedLeft
  }, topBlank > 0 && (/*#__PURE__*/React.createElement("div", {
    key: "top-blank",
    className: cx(Classes.virtualBlank, 'top'),
    style: {
      height: topBlank
    }
  })), /*#__PURE__*/React.createElement(HtmlTable, _extends({
    tbodyHtmlTag: "tbody"
  }, commonProps, {
    tbodyPosition: "left",
    horizontalRenderInfo: _extends(_extends({}, info), {
      flat: {
        center: left,
        full: left,
        left: [],
        right: []
      },
      visible: _sliceInstanceProperty(visible).call(visible, 0, left.length)
    }),
    verticalRenderInfo: verticalRenderInfo
  })), bottomBlank > 0 && (/*#__PURE__*/React.createElement("div", {
    key: "bottom-blank",
    className: cx(Classes.virtualBlank, 'bottom'),
    style: {
      height: bottomBlank
    }
  })))) : null, right.length > 0 ? (/*#__PURE__*/React.createElement("div", {
    className: Classes.fixedRight
  }, topBlank > 0 && (/*#__PURE__*/React.createElement("div", {
    key: "top-blank",
    className: cx(Classes.virtualBlank, 'top'),
    style: {
      height: topBlank
    }
  })), /*#__PURE__*/React.createElement(HtmlTable, _extends({
    tbodyHtmlTag: "tbody"
  }, commonProps, {
    tbodyPosition: "right",
    horizontalRenderInfo: _extends(_extends({}, info), {
      flat: {
        center: right,
        full: right,
        left: [],
        right: []
      },
      visible: _sliceInstanceProperty(visible).call(visible, visible.length - right.length)
    }),
    verticalRenderInfo: verticalRenderInfo
  })), bottomBlank > 0 && (/*#__PURE__*/React.createElement("div", {
    key: "bottom-blank",
    className: cx(Classes.virtualBlank, 'bottom'),
    style: {
      height: bottomBlank
    }
  })))) : null, /*#__PURE__*/React.createElement("div", {
    className: 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 = composeRowPropsGetter(getRowProps, extra.rowProps);
  var flat = _flatInstanceProperty(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
  };
  var fixedRightTableStyle = {
    right: (extra === null || extra === void 0 ? void 0 : extra.stickyRightOffset) || 0
  };
  return /*#__PURE__*/React.createElement("div", {
    className: cx(Classes.tableFooter),
    style: {
      bottom: stickyBottom === 0 ? undefined : stickyBottom
    }
  }, /*#__PURE__*/React.createElement("div", {
    className: cx(Classes.tableFooterMain, Classes.horizontalScrollContainer)
  }, /*#__PURE__*/React.createElement(HtmlTable, _extends({
    tbodyHtmlTag: "tfoot"
  }, commonProps, {
    tbodyPosition: hasLockColumn ? 'center' : undefined,
    horizontalRenderInfo: info
  })), footerDataSource.length > 0 ? /*#__PURE__*/React.createElement("div", {
    className: Classes.verticalScrollPlaceholder,
    style: typeof (extra === null || extra === void 0 ? void 0 : extra.stickyRightOffset) === 'number' ? {
      width: extra === null || extra === void 0 ? void 0 : extra.stickyRightOffset,
      visibility: 'initial'
    } : undefined
  }) : null), left.length > 0 ? /*#__PURE__*/React.createElement("div", {
    className: Classes.fixedLeft
  }, /*#__PURE__*/React.createElement(HtmlTable, _extends({
    tbodyHtmlTag: "tfoot"
  }, commonProps, {
    tbodyPosition: "left",
    horizontalRenderInfo: _extends(_extends({}, info), {
      flat: {
        center: left,
        full: left,
        left: [],
        right: []
      },
      visible: _sliceInstanceProperty(visible).call(visible, 0, left.length)
    })
  }))) : null, right.length > 0 ? /*#__PURE__*/React.createElement("div", {
    className: Classes.fixedRight,
    style: fixedRightTableStyle
  }, /*#__PURE__*/React.createElement(HtmlTable, _extends({
    tbodyHtmlTag: "tfoot"
  }, commonProps, {
    tbodyPosition: "right",
    horizontalRenderInfo: _extends(_extends({}, info), {
      flat: {
        center: right,
        full: right,
        left: [],
        right: []
      },
      visible: _sliceInstanceProperty(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(Classes.rowDetailContainer));
  return /*#__PURE__*/ReactDom.createPortal(/*#__PURE__*/React.createElement(RowDetail, _extends({}, params)), rowDetailContainer);
}
function RowDetail(props) {
  var detailRef = useRef(null);
  var row = props.row,
    rowIndex = props.rowIndex,
    domHelper = props.domHelper,
    renderDetail = props.renderDetail;
  var artTable = domHelper.artTable;
  useEffect(function () {
    var selector = function selector(position) {
      var _context, _context2;
      return _concatInstanceProperty(_context = _concatInstanceProperty(_context2 = ".".concat(position, " .")).call(_context2, Classes.tableRow, "[data-rowindex=\"")).call(_context, rowIndex, "\"]");
    };
    var itemRect = detailRef.current && detailRef.current.getBoundingClientRect();
    var targetRow = artTable.querySelector(selector(Classes.tableBody));
    var targetRowLeft = artTable.querySelector(selector(Classes.fixedLeft));
    var targetRowRight = artTable.querySelector(selector(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');
    }
  }, []);
  useEffect(function () {
    // 这时候行才渲染完，只能在这里设置偏移量
    var offsetTop = domHelper.getRowTop(rowIndex) || 0;
    detailRef.current.style.transform = "translateY(".concat(offsetTop + 'px', ")");
  });
  return /*#__PURE__*/React.createElement("div", {
    ref: detailRef,
    className: Classes.rowDetailItem
  }, renderDetail(row, rowIndex));
}
if (isStickyUIDegrade()) {
  TEMPLATES.set('header', renderTableHeaderInIE);
  TEMPLATES.set('body', renderTableBodyInIE);
  TEMPLATES.set('footer', renderTableFooterInIE);
  TEMPLATES.set('rowDetail', renderRowDetailInIE);
}
export default function getTableHeaderRenderTemplate(type) {
  return TEMPLATES.get(type);
}