var __assign = this && this.__assign || function () {
  __assign = Object.assign || function (t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];

      for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
      }
    }

    return t;
  };

  return __assign.apply(this, arguments);
};

var __rest = this && this.__rest || function (s, e) {
  var t = {};

  for (var p in s) {
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  }

  if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
    if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  }
  return t;
};

import { jsx as _jsx } from "react/jsx-runtime";
import * as React from 'react';
import { BaseTable } from 'ali-react-table-fork';
import useMeasure from 'react-use/lib/useMeasure';
import classNames from 'classnames';
import "./style/virtual-table.css";
import { CrossTable as BaseCrossTable, CrossTreeTable as BaseCrossTreeTable } from 'ali-react-table-fork/dist/ali-react-table-pivot';
import Spin from "../spin";
import { createScrollbar } from "../scrollbar";
import { v4 as uuidV4 } from 'uuid';
var prefixCls = 'dmc-virtual-';
var PAGE_BG_HEADER_HEIGHT = 50;
var DEFAULT_HEADER_HEIGHT = 42;
var defaultProps = {
  withVerticalBorder: true,
  withHorizontalBorder: true,
  noCellPadding: true
};
var defaultBorderColor = 'var(--dmc-table-bg-border-color)';
var defaultHeaderRowHeight = '42px';
var defaultRowHeight = '40px';
var defaultHeadBgcolor = 'var(--dmc-table-bg-head-bg-color)';
var defaultHeaderColor = 'var(--dmc-table-bg-head-text-color)';
var defaultBgcolor = 'var(--dmc-table-bg-color)';
var defaultTextColor = 'var(--dmc-table-bg-body-text-color)';
var defaultRowBgHoverColor = 'var(--dmc-table-bg-body-bg-hover-color)';
var pageBgBorderColor = 'var(--dmc-table-pb-border-color)';
var pageBgHeaderRowHeight = '50px';
var pageBgRowHeight = '48px';
var pageBgHeadBgcolor = 'var(--dmc-table-pb-head-bg-color)';
var pageBgBgcolor = 'var(--dmc-table-pb-bg-color)';
var pageBgHeaderColor = 'var(--dmc-table-pb-head-text-color)';
var pageBgTextColor = 'var(--dmc-table-pb-head-text-color)';
var pageBgRowBgHoverColor = 'var(--dmc-table-pb-body-bg-hover-color)';
var assistBgBorderColor = 'var(--dmc-table-ab-border-color)';
var assistBgHeaderRowHeight = '42px';
var assistBgRowHeight = '40px';
var assistBgHeadBgcolor = 'var(--dmc-table-ab-head-bg-color)';
var assistBgBgcolor = 'var(--dmc-table-ab-bg-color)';
var assistBgHeaderColor = 'var(--dmc-table-ab-head-text-color)';
var assistBgTextColor = 'var(--dmc-table-ab-body-text-color)';
var assistBgRowBgHoverColor = 'var(--dmc-table-ab-body-bg-hover-color)';

var getStyle = function getStyle(type) {
  var defaultStyle = {
    '--border-color': defaultBorderColor,
    '--header-row-height': defaultHeaderRowHeight,
    '--row-height': defaultRowHeight,
    '--header-bgcolor': defaultHeadBgcolor,
    '--header-color': defaultHeaderColor,
    '--bgcolor': defaultBgcolor,
    '--color': defaultTextColor,
    '--row-hover-color': defaultRowBgHoverColor
  };
  var pageBgStyle = {
    '--border-color': pageBgBorderColor,
    '--header-row-height': pageBgHeaderRowHeight,
    '--row-height': pageBgRowHeight,
    '--header-bgcolor': pageBgHeadBgcolor,
    '--header-color': pageBgHeaderColor,
    '--bgcolor': pageBgBgcolor,
    '--color': pageBgTextColor,
    '--row-hover-color': pageBgRowBgHoverColor
  };
  var assistBgStyle = {
    '--border-color': assistBgBorderColor,
    '--header-row-height': assistBgHeaderRowHeight,
    '--row-height': assistBgRowHeight,
    '--header-bgcolor': assistBgHeadBgcolor,
    '--header-color': assistBgHeaderColor,
    '--bgcolor': assistBgBgcolor,
    '--color': assistBgTextColor,
    '--row-hover-color': assistBgRowBgHoverColor
  };
  return type === 'page-bg' ? pageBgStyle : type === 'assist-bg' ? assistBgStyle : defaultStyle;
};

var useCommonTabelProps = function useCommonTabelProps(style, type, className, noCellPadding) {
  var _a;

  var uuid = uuidV4();
  var psRef = React.useRef();
  var cls = classNames(className, prefixCls + "table-" + uuid, (_a = {}, _a[prefixCls + "table-" + type] = type, _a[prefixCls + "table-noCellPadding"] = !noCellPadding, _a)); //修改样式使用style, BaseTableCSSVariables

  var mixStyle = __assign(__assign({}, getStyle(type)), style !== null && style !== void 0 ? style : {});

  React.useEffect(function () {
    var dom = document.querySelector("." + prefixCls + "table-" + uuid);

    if (dom) {
      var _a = createScrollbar(dom, type),
          ps = _a[0],
          destroy_1 = _a[1];

      psRef.current = ps;
      return function () {
        destroy_1();
        psRef.current = undefined;
      };
    }
  }, [type, uuid]);
  React.useEffect(function () {
    var _a;

    (_a = psRef.current) === null || _a === void 0 ? void 0 : _a.update();
  });
  return {
    className: cls,
    style: mixStyle
  };
};

var VirtualizedTable = function VirtualizedTable(_a) {
  var _b;

  var className = _a.className,
      withVerticalBorder = _a.withVerticalBorder,
      withHorizontalBorder = _a.withHorizontalBorder,
      type = _a.type,
      headerHeight = _a.headerHeight,
      style = _a.style,
      noCellPadding = _a.noCellPadding,
      restProps = __rest(_a, ["className", "withVerticalBorder", "withHorizontalBorder", "type", "headerHeight", "style", "noCellPadding"]);

  var _c = useMeasure(),
      ref = _c[0],
      eleHeight = _c[1].height;

  var mixStyle = React.useMemo(function () {
    return __assign({
      width: '100%',
      height: eleHeight,
      overflow: 'auto'
    }, style);
  }, [eleHeight, style]);

  var _d = useCommonTabelProps(mixStyle, type, className, noCellPadding),
      commonCls = _d.className,
      commonStyle = _d.style;

  var cls = classNames(className, commonCls, (_b = {}, _b[prefixCls + "table-without-vertical-border"] = !withVerticalBorder, _b[prefixCls + "table-without-horizontal-border"] = !withHorizontalBorder, _b[prefixCls + "table-" + type] = type, _b));
  return _jsx("div", __assign({
    className: prefixCls + "table-container",
    ref: ref
  }, {
    children: _jsx(BaseTable, __assign({}, restProps, {
      style: commonStyle,
      prefixCls: prefixCls,
      loadingIcon: _jsx(Spin, {}, void 0),
      className: cls,
      headerHeight: headerHeight || type === 'page-bg' ? PAGE_BG_HEADER_HEIGHT : DEFAULT_HEADER_HEIGHT
    }), void 0)
  }), void 0);
};

VirtualizedTable.defaultProps = defaultProps;

var CrossTable = function CrossTable(_a) {
  var type = _a.type,
      style = _a.style,
      className = _a.className,
      noCellPadding = _a.noCellPadding,
      restProps = __rest(_a, ["type", "style", "className", "noCellPadding"]);

  var commonProps = useCommonTabelProps(style, type, className, noCellPadding);
  return _jsx(BaseCrossTable, __assign({}, restProps, commonProps), void 0);
};

CrossTable.defaultProps = defaultProps;

var CrossTreeTable = function CrossTreeTable(_a) {
  var type = _a.type,
      style = _a.style,
      className = _a.className,
      noCellPadding = _a.noCellPadding,
      restProps = __rest(_a, ["type", "style", "className", "noCellPadding"]);

  var commonProps = useCommonTabelProps(style, type, className, noCellPadding);
  return _jsx(BaseCrossTreeTable, __assign({}, restProps, commonProps), void 0);
};

CrossTreeTable.defaultProps = defaultProps;
export * from 'ali-react-table-fork';
VirtualizedTable.displayName = 'VirtualizedTable';
export default VirtualizedTable;
var VirtualizedTableMemo = /*#__PURE__*/React.memo(VirtualizedTable);
VirtualizedTableMemo.displayName = 'VirtualizedTableMemo';
export { VirtualizedTableMemo, CrossTable, CrossTreeTable };

