/**
 * tdesign v0.32.0
 * (c) 2021 tdesign
 * @license MIT
 */

import { h as helper } from '../../_chunks/dep-42d5c87e.js';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import throttle from 'lodash/throttle';
import mixins from '../../utils/mixins.js';
import getConfigReceiverMixins from '../../config-provider/config-receiver.js';
import { prefix } from '../../config.js';
import flatColumns from '../util/props-util.js';
import baseTableProps from '../base-table-props.js';
import TableBody from './table-body.js';
import TableHeader from './table-header.js';
import TableColGroup from './col-group.js';
import { Pagination } from '../../pagination/index.js';
import { Loading } from '../../loading/index.js';
import { debounce, getScrollDirection, SCROLL_DIRECTION } from '../util/common.js';
import { renderTNodeJSX } from '../../utils/render-tnode.js';
import { emitEvent } from '../../utils/event.js';
import { EventNameWithKebab } from '../util/interface.js';
import { SIZE_CLASSNAMES } from '../../utils/classnames.js';
import primaryTableProps from '../primary-table-props.js';
import 'vue';
import '../../config-provider/zh_CN_config.js';
import '../../config-provider/type.js';
import '@babel/runtime/helpers/toConsumableArray';
import 'lodash/get';
import './table-row.js';
import './table-cell.js';
import '../../popup/index.js';
import '../../popup/popup.js';
import '@babel/runtime/helpers/typeof';
import '@popperjs/core';
import '../../_chunks/dep-1db5c91c.js';
import '../../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../../utils/easing.js';
import '../../popup/props.js';
import '../../utils/set-style.js';
import '../../utils/map-props.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '../../utils/withInstall.js';
import 'lodash/capitalize';
import './style/index.js';
import '../../popup/type.js';
import '../../pagination/pagination.js';
import 'tdesign-icons-vue';
import '../../input-number/index.js';
import '../../input-number/input-number.js';
import '@babel/runtime/helpers/asyncToGenerator';
import '@babel/runtime/regenerator';
import '../../button/index.js';
import '../../button/button.js';
import '../../button/props.js';
import '../../utils/ripple.js';
import '../../loading/loading.js';
import '../../loading/icon/gradient.js';
import '../../utils/transfer-dom.js';
import '../../loading/props.js';
import '../../loading/type.js';
import '../../loading/plugin.js';
import '../../button/type.js';
import '../../input-number/props.js';
import '../../input-number/type.js';
import '../../utils/helper.js';
import '@babel/runtime/helpers/slicedToArray';
import 'lodash/camelCase';
import '../../select/index.js';
import '../../select/select.js';
import 'lodash/isFunction';
import 'lodash/debounce';
import 'lodash/set';
import '../../input/index.js';
import '../../input/addon.js';
import '../../input/input.js';
import '../../input/props.js';
import '../../input/input-group.js';
import '../../input/type.js';
import '../../tag/index.js';
import '../../tag/tag.js';
import '../../tag/props.js';
import '../../tag/check-tag.js';
import '../../tag/check-tag-props.js';
import '../../tag/type.js';
import '../../common-components/fake-arrow.js';
import '../../select/option.js';
import '../../select/option-props.js';
import '../../checkbox/index.js';
import '../../checkbox/group.js';
import 'lodash/intersection';
import '../../checkbox/checkbox.js';
import '../../checkbox/props.js';
import '../../checkbox/checkbox-group-props.js';
import '../../checkbox/type.js';
import '../../select/props.js';
import '../../select/optionGroup.js';
import '../../select/option-group-props.js';
import '../../select/type.js';
import '../../pagination/props.js';
import '../../pagination/type.js';

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var _BaseTable = mixins(getConfigReceiverMixins("table")).extend({
  name: "TBaseTable",
  model: {
    prop: "value",
    event: "change"
  },
  props: _objectSpread(_objectSpread({}, baseTableProps), {}, {
    selectedRowKeys: primaryTableProps.selectedRowKeys,
    provider: {
      type: Object,
      "default": function _default() {
        return {
          renderRows: function renderRows() {}
        };
      }
    }
  }),
  data: function data() {
    return {
      scrollableToLeft: false,
      scrollableToRight: false,
      scrollBarWidth: 0,
      defaultCurrent: 0,
      defaultPageSize: 0,
      useFixedHeader: false
    };
  },
  computed: {
    current: function current() {
      var _this$pagination, _this$pagination2;

      return ((_this$pagination = this.pagination) === null || _this$pagination === void 0 ? void 0 : _this$pagination.current) || this.defaultCurrent || ((_this$pagination2 = this.pagination) === null || _this$pagination2 === void 0 ? void 0 : _this$pagination2.defaultCurrent);
    },
    pageSize: function pageSize() {
      var _this$pagination3, _this$pagination4;

      return ((_this$pagination3 = this.pagination) === null || _this$pagination3 === void 0 ? void 0 : _this$pagination3.pageSize) || this.defaultPageSize || ((_this$pagination4 = this.pagination) === null || _this$pagination4 === void 0 ? void 0 : _this$pagination4.defaultPageSize);
    },
    dataSource: function dataSource() {
      if (!this.hasPagination) return this.data.slice(0);
      var current = this.current,
          pageSize = this.pageSize;

      if (this.data.length > pageSize) {
        return this.data.slice((current - 1) * pageSize, current * pageSize);
      }

      return this.data;
    },
    flattedColumns: function flattedColumns() {
      return flatColumns(this.columns);
    },
    isEmpty: function isEmpty() {
      return (!this.dataSource || this.dataSource.length === 0) && !this.loading;
    },
    hasFixedColumns: function hasFixedColumns() {
      var columns = this.columns;
      return columns.some(function (item) {
        return item.fixed === "right" || item.fixed === "left";
      });
    },
    hasPagination: function hasPagination() {
      return !!this.pagination;
    },
    isLoading: function isLoading() {
      return !!this.loading;
    },
    tableHeight: function tableHeight() {
      var height = this.height,
          maxHeight = this.maxHeight,
          useFixedHeader = this.useFixedHeader,
          isEmpty = this.isEmpty;

      if (isEmpty) {
        return "auto";
      }

      if (height !== "auto" && height) {
        return height;
      }

      if (maxHeight && useFixedHeader) {
        return maxHeight;
      }

      return "auto";
    },
    fixedHeader: function fixedHeader() {
      var tableHeight = this.tableHeight;
      return tableHeight !== "auto";
    },
    commonClass: function commonClass() {
      var _ref;

      var classes = ["".concat(prefix, "-table"), (_ref = {}, _defineProperty(_ref, SIZE_CLASSNAMES.small, this.size === "small"), _defineProperty(_ref, SIZE_CLASSNAMES.large, this.size === "large"), _defineProperty(_ref, "".concat(prefix, "-table--bordered"), this.bordered), _defineProperty(_ref, "".concat(prefix, "-table--striped"), this.stripe), _defineProperty(_ref, "".concat(prefix, "-table--hoverable"), this.hover), _defineProperty(_ref, "".concat(prefix, "-table__row--draggable"), this.provider.sortOnRowDraggable), _defineProperty(_ref, "".concat(prefix, "-table-table--align-top"), this.verticalAlign === "top"), _defineProperty(_ref, "".concat(prefix, "-table-table--align-bottom"), this.verticalAlign === "bottom"), _defineProperty(_ref, "".concat(prefix, "-table__cell--fixed"), this.hasFixedColumns), _defineProperty(_ref, "".concat(prefix, "-table--has-fixed"), this.hasFixedColumns), _defineProperty(_ref, "".concat(prefix, "-table__header--fixed"), this.fixedHeader), _ref)];
      return classes;
    },
    usePadding: function usePadding() {
      return this.scrollableToRight || this.scrollableToLeft;
    }
  },
  methods: {
    checkScrollableToLeftOrRight: function checkScrollableToLeftOrRight() {
      var scrollContainer = this.$refs[this.fixedHeader ? "scrollBody" : "tableContent"];
      var scrollLeft = scrollContainer.scrollLeft,
          scrollWidth = scrollContainer.scrollWidth,
          clientWidth = scrollContainer.clientWidth;
      this.scrollableToLeft = scrollLeft > 0;
      this.scrollableToRight = scrollLeft + clientWidth < scrollWidth;
    },
    addWindowResizeEventListener: function addWindowResizeEventListener() {
      var checkScrollableToLeftOrRight = debounce(this.checkScrollableToLeftOrRight);
      window.addEventListener("resize", checkScrollableToLeftOrRight);
      this.$once("hook:beforeDestroy", function () {
        return window.removeEventListener("resize", checkScrollableToLeftOrRight);
      });
    },
    renderHeader: function renderHeader() {
      var h = this.$createElement;
      var columns = this.columns,
          scopedSlots = this.$scopedSlots,
          bordered = this.bordered;
      return h(TableHeader, {
        "scopedSlots": scopedSlots,
        "attrs": {
          "columns": columns,
          "bordered": bordered
        }
      });
    },
    registerRowEvents: function registerRowEvents() {
      var _this = this;

      var events = {};
      EventNameWithKebab.forEach(function (eventName) {
        events[eventName] = function (params) {
          emitEvent(_this, eventName, params);
        };
      });
      return events;
    },
    renderBody: function renderBody() {
      var h = this.$createElement;
      var listener = this.$listeners,
          scopedSlots = this.$scopedSlots;
      var rowEvents = this.registerRowEvents();
      var props = {
        props: {
          rowKey: this.rowKey,
          data: this.dataSource,
          provider: this.provider,
          columns: this.flattedColumns,
          rowClassName: this.rowClassName,
          current: this.current,
          selectedRowKeys: this.selectedRowKeys,
          rowspanAndColspan: this.rowspanAndColspan,
          firstFullRow: this.firstFullRow
        },
        scopedSlots: scopedSlots,
        on: _objectSpread(_objectSpread({}, listener), rowEvents)
      };
      return h(TableBody, helper([{}, props]));
    },
    renderEmptyTable: function renderEmptyTable() {
      var h = this.$createElement;
      if (this.empty === null) return null;
      var useLocale = !this.empty && !this.$scopedSlots.empty;
      return h("div", {
        "class": "".concat(prefix, "-table__empty")
      }, [useLocale ? this.global.empty : renderTNodeJSX(this, "empty")]);
    },
    renderPagination: function renderPagination() {
      var _this2 = this;

      var h = this.$createElement;
      var paginationProps = this.pagination;
      return h("div", {
        "class": "".concat(prefix, "-table__pagination")
      }, [h(Pagination, helper([{}, {
        "props": _objectSpread({}, paginationProps)
      }, {
        "on": _objectSpread({}, {
          change: function change(pageInfo) {
            var current = pageInfo.current,
                pageSize = pageInfo.pageSize;
            emitEvent(_this2, "page-change", pageInfo, _this2.dataSource);
            _this2.defaultCurrent = current;
            _this2.defaultPageSize = pageSize;
          }
        })
      }]))]);
    },
    renderTableWithFixedHeader: function renderTableWithFixedHeader() {
      var _this3 = this;

      var h = this.$createElement;
      var fixedTable = [];
      var columns = this.columns,
          asyncLoadingProps = this.provider.asyncLoadingProps,
          tableLayout = this.tableLayout,
          scrollBarWidth = this.scrollBarWidth,
          hasFixedColumns = this.hasFixedColumns,
          tableHeight = this.tableHeight,
          usePadding = this.usePadding;
      var handleScroll = throttle(function (e) {
        var target = e.target;
        var scrollLeft = target.scrollLeft;
        _this3.$refs.scrollHeader.scrollLeft = scrollLeft;

        _this3.handleScroll(e);
      }, 10);
      var paddingRight = "".concat(scrollBarWidth, "px");
      var headerContainerStyle = columns.length > 1 && usePadding ? {
        paddingRight: paddingRight
      } : {};
      fixedTable.push(h("div", {
        "class": "".concat(prefix, "-table__header"),
        "style": headerContainerStyle,
        "ref": "scrollHeader"
      }, [h("table", {
        "style": {
          tableLayout: tableLayout,
          paddingRight: paddingRight
        }
      }, [h(TableColGroup, {
        "attrs": {
          "columns": columns
        }
      }), this.renderHeader()])]));
      var containerStyle = {
        height: isNaN(Number(tableHeight)) ? tableHeight : "".concat(Number(tableHeight), "px"),
        width: hasFixedColumns ? "100%" : void 0
      };
      fixedTable.push(h("div", helper([{
        "class": "".concat(prefix, "-table__body"),
        "style": containerStyle
      }, asyncLoadingProps, {
        "ref": "scrollBody",
        "on": {
          "scroll": handleScroll
        }
      }]), [h("table", {
        "ref": "table",
        "style": {
          tableLayout: tableLayout
        }
      }, [h(TableColGroup, {
        "attrs": {
          "columns": columns
        }
      }), this.renderBody(), this.renderFooter()])]));
      return fixedTable;
    },
    renderLoadingContent: function renderLoadingContent() {
      var h = this.$createElement;
      return renderTNodeJSX(this, "loading", {
        defaultNode: h("div")
      });
    },
    renderFooter: function renderFooter() {
      var h = this.$createElement;
      var colspan = this.flattedColumns.length;
      var footerContent = renderTNodeJSX(this, "footer");
      return footerContent ? h("tfoot", [h("tr", [h("td", {
        "attrs": {
          "colspan": colspan
        }
      }, [footerContent])])]) : null;
    },
    handleScroll: function handleScroll(e) {
      this.checkScrollableToLeftOrRight();
      var _e$target = e.target,
          scrollLeft = _e$target.scrollLeft,
          scrollTop = _e$target.scrollTop;
      var direction = getScrollDirection(scrollLeft, scrollTop);

      if (direction !== SCROLL_DIRECTION.UNKNOWN) {
        var scrollListenerName = direction === SCROLL_DIRECTION.X ? "scroll-x" : "scroll-y";
        var scrollParams = {
          e: e
        };
        emitEvent(this, scrollListenerName, scrollParams);
      }
    }
  },
  render: function render() {
    var _ref2;

    var h = arguments[0];
    var hasPagination = this.hasPagination,
        commonClass = this.commonClass,
        fixedHeader = this.fixedHeader,
        columns = this.columns,
        tableLayout = this.tableLayout,
        isLoading = this.isLoading,
        isEmpty = this.isEmpty,
        useFixedHeader = this.useFixedHeader,
        hasFixedColumns = this.hasFixedColumns;
    var body = [];
    var tableColGroup = h(TableColGroup, {
      "attrs": {
        "columns": columns
      }
    });
    var tableHeader = this.renderHeader();
    var tableContent = [tableColGroup, tableHeader];
    var fixedTableContent;

    if (fixedHeader || useFixedHeader) {
      fixedTableContent = this.renderTableWithFixedHeader();
    } else {
      tableContent.push(this.renderBody());
      tableContent.push(this.renderFooter());
    }

    if (isEmpty) {
      var empty = this.renderEmptyTable();
      empty && body.push(empty);
    }

    if (hasPagination) {
      body.push(this.renderPagination());
    }

    var handleScroll = throttle(this.handleScroll, 100);
    var tableContentClass = ["".concat(prefix, "-table__content"), (_ref2 = {}, _defineProperty(_ref2, "".concat(prefix, "-table__content--scrollable-to-right"), this.scrollableToRight), _defineProperty(_ref2, "".concat(prefix, "-table__content--scrollable-to-left"), this.scrollableToLeft), _ref2)];
    var width;
    var _this$$refs = this.$refs,
        tableContentEl = _this$$refs.tableContent,
        tableEl = _this$$refs.table;

    if (!hasFixedColumns && tableContentEl && tableContentEl.clientWidth < tableEl.clientWidth) {
      width = "".concat(tableEl.clientWidth, "px");
    }

    return h("div", {
      "class": commonClass,
      "style": {
        width: width
      }
    }, [renderTNodeJSX(this, "topContent"), h(Loading, {
      "attrs": {
        "loading": isLoading,
        "showOverlay": true,
        "text": this.renderLoadingContent
      }
    }, [h("div", {
      "ref": "tableContent",
      "class": tableContentClass,
      "on": {
        "scroll": handleScroll
      }
    }, [fixedTableContent || h("table", {
      "ref": "table",
      "style": {
        tableLayout: tableLayout
      }
    }, [tableContent])]), body])]);
  },
  mounted: function mounted() {
    var _this4 = this;

    if (this.hasFixedColumns) {
      var timer = setTimeout(function () {
        _this4.checkScrollableToLeftOrRight();

        clearTimeout(timer);
        timer = null;
      }, 0);
      this.addWindowResizeEventListener();
    }

    var scrollDiv = document.createElement("div");
    scrollDiv.style.cssText = "\n      width: 99px;\n      height: 99px;\n      overflow: scroll;\n      position: absolute;\n      top: -9999px;";
    scrollDiv.classList.add("scrollbar");
    document.body.appendChild(scrollDiv);
    this.scrollBarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth;
    document.body.removeChild(scrollDiv);
    var maxHeight = this.maxHeight;

    if (maxHeight && this.$refs.tableContent.clientHeight > maxHeight) {
      this.useFixedHeader = true;
    }
  }
});

export { _BaseTable as default };
//# sourceMappingURL=index.js.map
