"use strict";

var __assign = void 0 && (void 0).__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 __createBinding = void 0 && (void 0).__createBinding || (Object.create ? function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  Object.defineProperty(o, k2, {
    enumerable: true,
    get: function get() {
      return m[k];
    }
  });
} : function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  o[k2] = m[k];
});

var __setModuleDefault = void 0 && (void 0).__setModuleDefault || (Object.create ? function (o, v) {
  Object.defineProperty(o, "default", {
    enumerable: true,
    value: v
  });
} : function (o, v) {
  o["default"] = v;
});

var __importStar = void 0 && (void 0).__importStar || function (mod) {
  if (mod && mod.__esModule) return mod;
  var result = {};
  if (mod != null) for (var k in mod) {
    if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  }

  __setModuleDefault(result, mod);

  return result;
};

var __exportStar = void 0 && (void 0).__exportStar || function (m, exports) {
  for (var p in m) {
    if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
  }
};

var __rest = void 0 && (void 0).__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;
};

var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
  return mod && mod.__esModule ? mod : {
    "default": mod
  };
};

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.CrossTreeTable = exports.CrossTable = exports.VirtualizedTableMemo = void 0;

var jsx_runtime_1 = require("react/jsx-runtime");

var React = __importStar(require("react"));

var ali_react_table_fork_1 = require("ali-react-table-fork");

var useMeasure_1 = __importDefault(require("react-use/lib/useMeasure"));

var classnames_1 = __importDefault(require("classnames"));

require("./style/virtual-table.css");

var ali_react_table_pivot_1 = require("ali-react-table-fork/dist/ali-react-table-pivot");

var spin_1 = __importDefault(require("../spin"));

var scrollbar_1 = require("../scrollbar");

var uuid_1 = require("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 = uuid_1.v4();
  var psRef = React.useRef();
  var cls = classnames_1["default"](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 = scrollbar_1.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_1["default"](),
      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_1["default"](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_runtime_1.jsx("div", __assign({
    className: prefixCls + "table-container",
    ref: ref
  }, {
    children: jsx_runtime_1.jsx(ali_react_table_fork_1.BaseTable, __assign({}, restProps, {
      style: commonStyle,
      prefixCls: prefixCls,
      loadingIcon: jsx_runtime_1.jsx(spin_1["default"], {}, 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_runtime_1.jsx(ali_react_table_pivot_1.CrossTable, __assign({}, restProps, commonProps), void 0);
};

exports.CrossTable = CrossTable;
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_runtime_1.jsx(ali_react_table_pivot_1.CrossTreeTable, __assign({}, restProps, commonProps), void 0);
};

exports.CrossTreeTable = CrossTreeTable;
CrossTreeTable.defaultProps = defaultProps;

__exportStar(require("ali-react-table-fork"), exports);

VirtualizedTable.displayName = 'VirtualizedTable';
exports["default"] = VirtualizedTable;
var VirtualizedTableMemo = React.memo(VirtualizedTable);
exports.VirtualizedTableMemo = VirtualizedTableMemo;
VirtualizedTableMemo.displayName = 'VirtualizedTableMemo';

