/**
 * 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 Vue from 'vue';
import get from 'lodash/get';
import { emitEvent } from '../../utils/event.js';
import { prefix } from '../../config.js';
import baseTableProps from '../base-table-props.js';
import primaryTableProps from '../primary-table-props.js';
import TableRow from './table-row.js';
import { renderTNodeJSX } from '../../utils/render-tnode.js';
import '../../utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import 'lodash/camelCase';
import './table-cell.js';
import '@babel/runtime/helpers/toConsumableArray';
import '../../popup/index.js';
import '../../popup/popup.js';
import '@babel/runtime/helpers/typeof';
import '@popperjs/core';
import '../../_chunks/dep-1db5c91c.js';
import '../../utils/classnames.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 '../../utils/withInstall.js';
import 'lodash/capitalize';
import './style/index.js';
import '../../popup/type.js';
import '../util/common.js';
import 'lodash/isFunction';

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 TableBody = Vue.extend({
  name: "".concat(prefix, "-table-body"),
  props: {
    data: baseTableProps.data,
    columns: baseTableProps.columns,
    rowClassName: baseTableProps.rowClassName,
    rowKey: baseTableProps.rowKey,
    rowspanAndColspan: baseTableProps.rowspanAndColspan,
    firstFullRow: baseTableProps.firstFullRow,
    onRowHover: baseTableProps.onRowHover,
    onRowMouseup: baseTableProps.onRowMouseup,
    onRowMousedown: baseTableProps.onRowMousedown,
    onRowClick: baseTableProps.onRowClick,
    onRowDbClick: baseTableProps.onRowDbClick,
    selectedRowKeys: primaryTableProps.selectedRowKeys,
    provider: {
      type: Object,
      "default": function _default() {
        return {
          renderRows: function renderRows() {}
        };
      }
    },
    current: {
      type: Number,
      "default": 1
    }
  },
  computed: {
    selectColumn: function selectColumn() {
      return this.columns.find(function (_ref) {
        var type = _ref.type;
        return ["multiple", "single"].includes(type);
      }) || {};
    }
  },
  methods: {
    getRowspanAndColspanProps: function getRowspanAndColspanProps() {
      var props = [];
      var data = this.data,
          columns = this.columns,
          rowspanAndColspan = this.rowspanAndColspan;
      var cacheFirstColumnLeftedRowspan = [];
      data.forEach(function (rowData, rowIndex) {
        if (props[rowIndex] === void 0) {
          props[rowIndex] = {};
        }

        columns.forEach(function (col, colIndex) {
          var colKey = col.colKey;

          var _ref2 = rowspanAndColspan({
            col: col,
            colIndex: colIndex,
            row: rowData,
            rowIndex: rowIndex
          }) || {},
              rowspan = _ref2.rowspan,
              colspan = _ref2.colspan;

          rowspan = rowspan || 1;
          colspan = colspan || 1;

          if (colIndex === 0 && rowspan > 1) {
            cacheFirstColumnLeftedRowspan[rowspan + rowIndex - 1] = 1;
          }

          var leftedRowspan = 0;

          if (rowIndex === 0 || rowspan > 1) {
            leftedRowspan = rowspan - 1;
          } else {
            var _props$preRowIndex, _props$preRowIndex$co;

            var preRowIndex = rowIndex - 1;
            leftedRowspan = ((_props$preRowIndex = props[preRowIndex]) === null || _props$preRowIndex === void 0 ? void 0 : (_props$preRowIndex$co = _props$preRowIndex[colKey]) === null || _props$preRowIndex$co === void 0 ? void 0 : _props$preRowIndex$co.leftedRowspan) || 0;

            if (leftedRowspan > 0) {
              leftedRowspan -= 1;
              rowspan = -1;
            }
          }

          var leftedColspan = 0;

          if (colIndex === 0 || colspan > 1) {
            leftedColspan = colspan - 1;

            if (colIndex === 0 && (leftedRowspan > 0 || cacheFirstColumnLeftedRowspan[rowIndex] > 0)) {
              leftedColspan = 1;
            }
          } else {
            if (rowIndex > 0) {
              var _props, _props$colKey, _props2, _props2$colKey;

              var preLeftedColspan = (_props = props[rowIndex - 1]) === null || _props === void 0 ? void 0 : (_props$colKey = _props[colKey]) === null || _props$colKey === void 0 ? void 0 : _props$colKey.leftedColspan;
              var preLeftedColRowspan = (_props2 = props[rowIndex - 1]) === null || _props2 === void 0 ? void 0 : (_props2$colKey = _props2[colKey]) === null || _props2$colKey === void 0 ? void 0 : _props2$colKey.leftedRowspan;

              if (preLeftedColspan > 0 && preLeftedColRowspan > 0) {
                leftedColspan = preLeftedColspan;
                colspan = -1;
              }
            }

            var preColKey = columns[colIndex - 1].colKey;

            if (leftedColspan === 0) {
              var _props$rowIndex, _props$rowIndex$preCo;

              leftedColspan = ((_props$rowIndex = props[rowIndex]) === null || _props$rowIndex === void 0 ? void 0 : (_props$rowIndex$preCo = _props$rowIndex[preColKey]) === null || _props$rowIndex$preCo === void 0 ? void 0 : _props$rowIndex$preCo.leftedColspan) || 0;

              if (leftedColspan > 0) {
                leftedColspan -= 1;
                colspan = -1;
              }
            }
          }

          props[rowIndex][colKey] = {
            leftedColspan: leftedColspan,
            leftedRowspan: leftedRowspan,
            rowspan: rowspan,
            colspan: colspan
          };
        });
      });
      return props;
    },
    renderFullRow: function renderFullRow(type) {
      var h = this.$createElement;
      var firstFullRowNode = renderTNodeJSX(this, type);

      if (firstFullRowNode) {
        return h("tr", [h("td", {
          "attrs": {
            "colspan": this.columns.length
          },
          "class": "".concat(prefix, "-table__row--full")
        }, [firstFullRowNode])]);
      }

      return null;
    },
    renderBody: function renderBody() {
      var _this = this;

      var h = this.$createElement;
      var data = this.data,
          rowClassName = this.rowClassName,
          rowKey = this.rowKey,
          provider = this.provider,
          scopedSlots = this.$scopedSlots,
          rowspanAndColspan = this.rowspanAndColspan,
          selectedRowKeys = this.selectedRowKeys,
          selectColumn = this.selectColumn;
      var body = [];
      var allRowspanAndColspanProps;

      if (typeof rowspanAndColspan === "function") {
        allRowspanAndColspanProps = this.getRowspanAndColspanProps();
      }

      data.forEach(function (row, index) {
        var _row$__t_table_inner_;

        var defaultRowClass = typeof rowClassName === "function" ? rowClassName({
          row: row,
          rowIndex: index
        }) : rowClassName;
        var rowClass = [];

        if (defaultRowClass) {
          rowClass = rowClass.concat(defaultRowClass);
        }

        var rowspanAndColspanProps = allRowspanAndColspanProps ? allRowspanAndColspanProps[index] : void 0;
        var rowVnode;
        var key = rowKey ? get(row, rowKey) : index + _this.current;
        var disabled = typeof selectColumn.disabled === "function" ? selectColumn.disabled({
          row: row,
          rowIndex: index
        }) : selectColumn.disabled;

        if (disabled) {
          rowClass.push("".concat(prefix, "-table__row--disabled"));
        }

        if ((selectedRowKeys === null || selectedRowKeys === void 0 ? void 0 : selectedRowKeys.indexOf(key)) > -1) {
          rowClass.push("".concat(prefix, "-table__row--selected"));
        }

        if ((_row$__t_table_inner_ = row.__t_table_inner_data__) !== null && _row$__t_table_inner_ !== void 0 && _row$__t_table_inner_.level) {
          var _row$__t_table_inner_2;

          rowClass.push("".concat(prefix, "-table__row--level-").concat(((_row$__t_table_inner_2 = row.__t_table_inner_data__) === null || _row$__t_table_inner_2 === void 0 ? void 0 : _row$__t_table_inner_2.level) || 0));
        }

        var props = {
          key: key,
          props: _objectSpread(_objectSpread({}, _this.$props), {}, {
            rowClass: rowClass.join(" "),
            rowData: row,
            index: index,
            rowspanAndColspanProps: rowspanAndColspanProps
          }),
          on: _objectSpread(_objectSpread({}, _this.$listeners), {}, {
            "row-dragstart": function rowDragstart() {
              emitEvent(_this, "row-dragstart", {
                index: index,
                row: row
              });
            },
            "row-dragover": function rowDragover(_ref3) {
              var e = _ref3.e;
              e.preventDefault();
              emitEvent(_this, "row-dragover", {
                index: index,
                row: row,
                targetElm: rowVnode.elm
              });
            }
          }),
          scopedSlots: scopedSlots
        };
        rowVnode = h(TableRow, helper([{
          "attrs": {
            "rowKey": _this.rowKey
          }
        }, props]));
        body.push(rowVnode);
        provider.renderRows({
          rows: body,
          row: row,
          rowIndex: index,
          columns: _this.columns
        });
      });
      var firstRow = this.renderFullRow("firstFullRow");

      if (firstRow) {
        body = [firstRow].concat(body);
      }

      var lastRow = this.renderFullRow("lastFullRow");

      if (lastRow) {
        body = body.concat(lastRow);
      }

      return body;
    }
  },
  render: function render() {
    var h = arguments[0];

    if (this.provider.sortOnRowDraggable) {
      var className = "".concat(prefix, "-table__body ").concat(this.provider.dragging ? "dragging" : "");
      return h("transition-group", {
        "class": className,
        "attrs": {
          "tag": "tbody"
        }
      }, [this.renderBody()]);
    }

    return h("tbody", {
      "class": "".concat(prefix, "-table__body")
    }, [this.renderBody()]);
  }
});

export { TableBody as default };
//# sourceMappingURL=table-body.js.map
