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

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var helper = require('../../_chunks/dep-44e6bdf6.js');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var throttle = require('lodash/throttle');
var utils_mixins = require('../../utils/mixins.js');
var configProvider_configReceiver = require('../../config-provider/config-receiver.js');
var config = require('../../config.js');
var table_util_propsUtil = require('../util/props-util.js');
var table_baseTableProps = require('../base-table-props.js');
var table_baseTable_tableBody = require('./table-body.js');
var table_baseTable_tableHeader = require('./table-header.js');
var table_baseTable_colGroup = require('./col-group.js');
var pagination_index = require('../../pagination/index.js');
var loading_index = require('../../loading/index.js');
var table_util_common = require('../util/common.js');
var utils_renderTnode = require('../../utils/render-tnode.js');
var utils_event = require('../../utils/event.js');
var table_util_interface = require('../util/interface.js');
var utils_classnames = require('../../utils/classnames.js');
var table_primaryTableProps = require('../primary-table-props.js');
require('vue');
require('../../config-provider/zh_CN_config.js');
require('@babel/runtime/helpers/toConsumableArray');
require('lodash/get');
require('./table-row.js');
require('./table-cell.js');
require('../../popup/index.js');
require('../../popup/popup.js');
require('@babel/runtime/helpers/typeof');
require('@popperjs/core');
require('../../_chunks/dep-939cdc39.js');
require('../../utils/dom.js');
require('raf');
require('lodash/isString');
require('../../utils/easing.js');
require('../../popup/props.js');
require('../../utils/set-style.js');
require('../../utils/map-props.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('../../utils/withInstall.js');
require('lodash/capitalize');
require('../../pagination/pagination.js');
require('tdesign-icons-vue');
require('../../input-number/index.js');
require('../../input-number/input-number.js');
require('@babel/runtime/helpers/asyncToGenerator');
require('@babel/runtime/regenerator');
require('../../button/index.js');
require('../../button/button.js');
require('../../button/props.js');
require('../../utils/ripple.js');
require('../../loading/loading.js');
require('../../loading/icon/gradient.js');
require('../../utils/transfer-dom.js');
require('../../loading/props.js');
require('../../loading/plugin.js');
require('../../input-number/props.js');
require('../../utils/helper.js');
require('@babel/runtime/helpers/slicedToArray');
require('lodash/camelCase');
require('../../select/index.js');
require('../../select/select.js');
require('lodash/isFunction');
require('lodash/debounce');
require('lodash/set');
require('../../input/index.js');
require('../../input/addon.js');
require('../../input/input.js');
require('../../input/props.js');
require('../../input/input-group.js');
require('../../tag/index.js');
require('../../tag/tag.js');
require('../../tag/props.js');
require('../../tag/check-tag.js');
require('../../tag/check-tag-props.js');
require('../../common-components/fake-arrow.js');
require('../../select/option.js');
require('../../select/option-props.js');
require('../../checkbox/index.js');
require('../../checkbox/group.js');
require('lodash/intersection');
require('../../checkbox/checkbox.js');
require('../../checkbox/props.js');
require('../../checkbox/checkbox-group-props.js');
require('../../select/props.js');
require('../../select/optionGroup.js');
require('../../select/option-group-props.js');
require('../../pagination/props.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var throttle__default = /*#__PURE__*/_interopDefaultLegacy(throttle);

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__default["default"](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 = utils_mixins["default"](configProvider_configReceiver["default"]("table")).extend({
  name: "TBaseTable",
  model: {
    prop: "value",
    event: "change"
  },
  props: _objectSpread(_objectSpread({}, table_baseTableProps["default"]), {}, {
    selectedRowKeys: table_primaryTableProps["default"].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 table_util_propsUtil["default"](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(config.prefix, "-table"), (_ref = {}, _defineProperty__default["default"](_ref, utils_classnames.SIZE_CLASSNAMES.small, this.size === "small"), _defineProperty__default["default"](_ref, utils_classnames.SIZE_CLASSNAMES.large, this.size === "large"), _defineProperty__default["default"](_ref, "".concat(config.prefix, "-table--bordered"), this.bordered), _defineProperty__default["default"](_ref, "".concat(config.prefix, "-table--striped"), this.stripe), _defineProperty__default["default"](_ref, "".concat(config.prefix, "-table--hoverable"), this.hover), _defineProperty__default["default"](_ref, "".concat(config.prefix, "-table__row--draggable"), this.provider.sortOnRowDraggable), _defineProperty__default["default"](_ref, "".concat(config.prefix, "-table-table--align-top"), this.verticalAlign === "top"), _defineProperty__default["default"](_ref, "".concat(config.prefix, "-table-table--align-bottom"), this.verticalAlign === "bottom"), _defineProperty__default["default"](_ref, "".concat(config.prefix, "-table__cell--fixed"), this.hasFixedColumns), _defineProperty__default["default"](_ref, "".concat(config.prefix, "-table--has-fixed"), this.hasFixedColumns), _defineProperty__default["default"](_ref, "".concat(config.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 = table_util_common.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(table_baseTable_tableHeader["default"], {
        "scopedSlots": scopedSlots,
        "attrs": {
          "columns": columns,
          "bordered": bordered
        }
      });
    },
    registerRowEvents: function registerRowEvents() {
      var _this = this;

      var events = {};
      table_util_interface.EventNameWithKebab.forEach(function (eventName) {
        events[eventName] = function (params) {
          utils_event.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(table_baseTable_tableBody["default"], helper.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(config.prefix, "-table__empty")
      }, [useLocale ? this.global.empty : utils_renderTnode.renderTNodeJSX(this, "empty")]);
    },
    renderPagination: function renderPagination() {
      var _this2 = this;

      var h = this.$createElement;
      var paginationProps = this.pagination;
      return h("div", {
        "class": "".concat(config.prefix, "-table__pagination")
      }, [h(pagination_index.Pagination, helper.helper([{}, {
        "props": _objectSpread({}, paginationProps)
      }, {
        "on": _objectSpread({}, {
          change: function change(pageInfo) {
            var current = pageInfo.current,
                pageSize = pageInfo.pageSize;
            utils_event.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__default["default"](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(config.prefix, "-table__header"),
        "style": headerContainerStyle,
        "ref": "scrollHeader"
      }, [h("table", {
        "style": {
          tableLayout: tableLayout,
          paddingRight: paddingRight
        }
      }, [h(table_baseTable_colGroup["default"], {
        "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.helper([{
        "class": "".concat(config.prefix, "-table__body"),
        "style": containerStyle
      }, asyncLoadingProps, {
        "ref": "scrollBody",
        "on": {
          "scroll": handleScroll
        }
      }]), [h("table", {
        "ref": "table",
        "style": {
          tableLayout: tableLayout
        }
      }, [h(table_baseTable_colGroup["default"], {
        "attrs": {
          "columns": columns
        }
      }), this.renderBody(), this.renderFooter()])]));
      return fixedTable;
    },
    renderLoadingContent: function renderLoadingContent() {
      var h = this.$createElement;
      return utils_renderTnode.renderTNodeJSX(this, "loading", {
        defaultNode: h("div")
      });
    },
    renderFooter: function renderFooter() {
      var h = this.$createElement;
      var colspan = this.flattedColumns.length;
      var footerContent = utils_renderTnode.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 = table_util_common.getScrollDirection(scrollLeft, scrollTop);

      if (direction !== table_util_common.SCROLL_DIRECTION.UNKNOWN) {
        var scrollListenerName = direction === table_util_common.SCROLL_DIRECTION.X ? "scroll-x" : "scroll-y";
        var scrollParams = {
          e: e
        };
        utils_event.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(table_baseTable_colGroup["default"], {
      "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__default["default"](this.handleScroll, 100);
    var tableContentClass = ["".concat(config.prefix, "-table__content"), (_ref2 = {}, _defineProperty__default["default"](_ref2, "".concat(config.prefix, "-table__content--scrollable-to-right"), this.scrollableToRight), _defineProperty__default["default"](_ref2, "".concat(config.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
      }
    }, [utils_renderTnode.renderTNodeJSX(this, "topContent"), h(loading_index.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;
    }
  }
});

exports["default"] = _BaseTable;
//# sourceMappingURL=index.js.map
