/**
 * tdesign v0.39.1-alpha.1
 * (c) 2022 tdesign
 * @license MIT
 */

'use strict';

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

var helper = require('../_chunks/dep-33ee4bce.js');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var VueCompositionAPI = require('@vue/composition-api');
var camelCase = require('lodash/camelCase');
var get = require('lodash/get');
var pick = require('lodash/pick');
var table_tr = require('./tr.js');
var configProvider_useConfig = require('../config-provider/useConfig.js');
var hooks_tnode = require('../hooks/tnode.js');
var table_hooks_useClassName = require('./hooks/useClassName.js');
var table_baseTableProps = require('./base-table-props.js');
require('@babel/runtime/helpers/typeof');
require('lodash/isFunction');
require('lodash/upperFirst');
require('lodash/isString');
require('./utils.js');
require('./hooks/useFixed.js');
require('@babel/runtime/helpers/slicedToArray');
require('../_common/js/log/log.js');
require('../_common/js/utils/getScrollbarWidth.js');
require('../utils/dom.js');
require('vue');
require('raf');
require('../utils/easing.js');
require('./ellipsis.js');
require('../utils/render-tnode.js');
require('lodash/isObject');
require('../popup/index.js');
require('../popup/popup.js');
require('@popperjs/core');
require('../config.js');
require('../utils/classnames.js');
require('../utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('../_common/js/utils/set-style.js');
require('../popup/props.js');
require('../popup/container.js');
require('../utils/map-props.js');
require('../utils/withInstall.js');
require('lodash/capitalize');
require('lodash/cloneDeep');
require('lodash/mergeWith');
require('../config-provider/context.js');
require('lodash/merge');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('lodash/isEmpty');
require('lodash/kebabCase');
require('../hooks/render-tnode.js');

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

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var camelCase__default = /*#__PURE__*/_interopDefaultLegacy(camelCase);
var get__default = /*#__PURE__*/_interopDefaultLegacy(get);
var pick__default = /*#__PURE__*/_interopDefaultLegacy(pick);

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 ROW_AND_TD_LISTENERS = table_tr.ROW_LISTENERS.concat("cell-click");
var extendTableProps = ["rowKey", "rowClassName", "rowAttributes", "loading", "empty", "fixedRows", "firstFullRow", "lastFullRow", "rowspanAndColspan", "onCellClick", "onPageChange", "onRowClick", "onRowDblclick", "onRowMouseover", "onRowMousedown", "onRowMouseenter", "onRowMouseleave", "onRowMouseup", "onScroll", "onScrollX", "onScrollY"];
var TBody = VueCompositionAPI.defineComponent({
  name: "TBody",
  props: _objectSpread({
    data: Array,
    columns: Array,
    rowAndColFixedPosition: Map,
    showColumnShadow: Object,
    tableElm: {},
    tableWidth: Number,
    isWidthOverflow: Boolean,
    translateY: Number,
    scrollType: String,
    isVirtual: Boolean,
    rowHeight: Number,
    trs: Map,
    bufferSize: Number,
    handleRowMounted: Function,
    renderExpandedRow: Function,
    firstFullRow: [String, Function],
    lastFullRow: [String, Function]
  }, pick__default["default"](table_baseTableProps["default"], extendTableProps)),
  setup: function setup(props, _ref) {
    var emit = _ref.emit;
    var renderTNode = hooks_tnode.useTNodeJSX();

    var _useConfig = configProvider_useConfig.useConfig("table"),
        t = _useConfig.t,
        global = _useConfig.global;

    var _useClassName = table_hooks_useClassName["default"](),
        tableFullRowClasses = _useClassName.tableFullRowClasses,
        tableBaseClass = _useClassName.tableBaseClass;

    var tbodyClasses = VueCompositionAPI.computed(function () {
      return [tableBaseClass.body];
    });
    var isFixedLeftColumn = VueCompositionAPI.computed(function () {
      return props.isWidthOverflow && !!props.columns.find(function (col) {
        return col.fixed === "left";
      });
    });

    var getTrListeners = function getTrListeners() {
      var trListeners = {};
      ROW_AND_TD_LISTENERS.forEach(function (eventName) {
        var name = ["cell-click"].includes(eventName) ? eventName : "row-".concat(eventName);

        trListeners[name] = function (context) {
          emit(name, context);
        };
      });
      return trListeners;
    };

    return {
      t: t,
      global: global,
      tableFullRowClasses: tableFullRowClasses,
      tbodyClasses: tbodyClasses,
      tableBaseClass: tableBaseClass,
      isFixedLeftColumn: isFixedLeftColumn,
      renderTNode: renderTNode,
      getTrListeners: getTrListeners
    };
  },
  render: function render(h) {
    var _this = this,
        _this$data,
        _this$data2;

    var renderEmpty = function renderEmpty(h2, columns) {
      return h("tr", {
        "class": [_this.tableBaseClass.emptyRow, _defineProperty__default["default"]({}, _this.tableFullRowClasses.base, _this.isWidthOverflow)]
      }, [h("td", {
        "attrs": {
          "colspan": columns.length
        }
      }, [h("div", {
        "class": [_this.tableBaseClass.empty, _defineProperty__default["default"]({}, _this.tableFullRowClasses.innerFullRow, _this.isWidthOverflow)],
        "style": _this.isWidthOverflow ? {
          width: "".concat(_this.tableWidth, "px")
        } : {}
      }, [_this.renderTNode("empty") || _this.t(_this.global.empty)])])]);
    };

    var getFullRow = function getFullRow(h2, columnLength2, type) {
      var tType = camelCase__default["default"](type);

      var fullRowNode = _this.renderTNode(tType);

      if (["", null, void 0, false].includes(fullRowNode)) return null;
      var classes = [_this.tableFullRowClasses.base, _this.tableFullRowClasses[tType]];
      return h("tr", {
        "class": classes
      }, [h("td", {
        "attrs": {
          "colspan": columnLength2
        }
      }, [h("div", {
        "class": _defineProperty__default["default"]({}, _this.tableFullRowClasses.innerFullRow, _this.isFixedToLeft),
        "style": _this.isFixedToLeft ? {
          width: "".concat(_this.tableWidth, "px")
        } : {}
      }, [h("div", {
        "class": _this.tableFullRowClasses.innerFullElement
      }, [fullRowNode])])])]);
    };

    var skipSpansMap = /* @__PURE__ */new Map();

    var onTrRowspanOrColspan = function onTrRowspanOrColspan(params, cellSpans) {
      var rowIndex = params.rowIndex,
          colIndex = params.colIndex;
      if (!cellSpans.rowspan && !cellSpans.colspan) return;
      var maxRowIndex = rowIndex + (cellSpans.rowspan || 1);
      var maxColIndex = colIndex + (cellSpans.colspan || 1);

      for (var i = rowIndex; i < maxRowIndex; i++) {
        for (var j = colIndex; j < maxColIndex; j++) {
          if (i !== rowIndex || j !== colIndex) {
            skipSpansMap.set([i, j].join(), true);
          }
        }
      }
    };

    var columnLength = this.columns.length;
    var dataLength = this.data.length;
    var trNodeList = [];
    skipSpansMap = /* @__PURE__ */new Map();
    (_this$data = this.data) === null || _this$data === void 0 ? void 0 : _this$data.forEach(function (row, rowIndex) {
      var trProps = _objectSpread(_objectSpread({}, pick__default["default"](_this.$props, table_tr.TABLE_PROPS)), {}, {
        row: row,
        columns: _this.columns,
        rowIndex: rowIndex,
        dataLength: dataLength,
        rowAndColFixedPosition: _this.rowAndColFixedPosition,
        skipSpansMap: skipSpansMap,
        onTrRowspanOrColspan: onTrRowspanOrColspan,
        isVirtual: _this.isVirtual,
        scrollType: _this.scrollType,
        rowHeight: _this.rowHeight,
        trs: _this.trs,
        bufferSize: _this.bufferSize,
        tableElm: _this.tableElm
      });

      if (_this.onCellClick) {
        trProps.onCellClick = _this.onCellClick;
      }

      var on = _this.getTrListeners();

      if (_this.handleRowMounted) {
        on.onRowMounted = _this.handleRowMounted;
      }

      var trNode = h(table_tr["default"], helper.helper([{
        "scopedSlots": _this.$scopedSlots,
        "key": get__default["default"](row, _this.rowKey || "id")
      }, {
        "on": on
      }, {}, {
        "props": trProps
      }]));
      trNodeList.push(trNode);

      if (_this.renderExpandedRow) {
        var p = {
          row: row,
          index: rowIndex,
          columns: _this.columns,
          tableWidth: _this.tableWidth,
          isWidthOverflow: _this.isWidthOverflow
        };

        var expandedContent = _this.renderExpandedRow(h, p);

        expandedContent && trNodeList.push(expandedContent);
      }
    });
    var list = [getFullRow(h, columnLength, "first-full-row"), trNodeList, getFullRow(h, columnLength, "last-full-row")];
    var isEmpty = !((_this$data2 = this.data) !== null && _this$data2 !== void 0 && _this$data2.length) && !this.loading;
    var translate = "translate(0, ".concat(this.translateY, "px)");
    var posStyle = {
      transform: translate,
      "-ms-transform": translate,
      "-moz-transform": translate,
      "-webkit-transform": translate
    };
    return h("tbody", {
      "class": this.tbodyClasses,
      "style": this.isVirtual && _objectSpread({}, posStyle)
    }, [isEmpty ? renderEmpty(h, this.columns) : list]);
  }
});

exports.ROW_AND_TD_LISTENERS = ROW_AND_TD_LISTENERS;
exports["default"] = TBody;
exports.extendTableProps = extendTableProps;
//# sourceMappingURL=tbody.js.map
