/**
 * 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 Vue = require('vue');
var get = require('lodash/get');
var utils_event = require('../../utils/event.js');
var config = require('../../config.js');
var table_baseTableProps = require('../base-table-props.js');
var table_primaryTableProps = require('../primary-table-props.js');
var table_baseTable_tableRow = require('./table-row.js');
var utils_renderTnode = require('../../utils/render-tnode.js');
require('../../utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@babel/runtime/helpers/slicedToArray');
require('lodash/camelCase');
require('./table-cell.js');
require('@babel/runtime/helpers/toConsumableArray');
require('../../popup/index.js');
require('../../popup/popup.js');
require('@babel/runtime/helpers/typeof');
require('@popperjs/core');
require('../../_chunks/dep-939cdc39.js');
require('../../utils/classnames.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('../../utils/withInstall.js');
require('lodash/capitalize');
require('../util/common.js');
require('lodash/isFunction');

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

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var Vue__default = /*#__PURE__*/_interopDefaultLegacy(Vue);
var get__default = /*#__PURE__*/_interopDefaultLegacy(get);

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 TableBody = Vue__default["default"].extend({
  name: "".concat(config.prefix, "-table-body"),
  props: {
    data: table_baseTableProps["default"].data,
    columns: table_baseTableProps["default"].columns,
    rowClassName: table_baseTableProps["default"].rowClassName,
    rowKey: table_baseTableProps["default"].rowKey,
    rowspanAndColspan: table_baseTableProps["default"].rowspanAndColspan,
    firstFullRow: table_baseTableProps["default"].firstFullRow,
    onRowHover: table_baseTableProps["default"].onRowHover,
    onRowMouseup: table_baseTableProps["default"].onRowMouseup,
    onRowMousedown: table_baseTableProps["default"].onRowMousedown,
    onRowClick: table_baseTableProps["default"].onRowClick,
    onRowDbClick: table_baseTableProps["default"].onRowDbClick,
    selectedRowKeys: table_primaryTableProps["default"].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 = utils_renderTnode.renderTNodeJSX(this, type);

      if (firstFullRowNode) {
        return h("tr", [h("td", {
          "attrs": {
            "colspan": this.columns.length
          },
          "class": "".concat(config.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__default["default"](row, rowKey) : index + _this.current;
        var disabled = typeof selectColumn.disabled === "function" ? selectColumn.disabled({
          row: row,
          rowIndex: index
        }) : selectColumn.disabled;

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

        if ((selectedRowKeys === null || selectedRowKeys === void 0 ? void 0 : selectedRowKeys.indexOf(key)) > -1) {
          rowClass.push("".concat(config.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(config.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() {
              utils_event.emitEvent(_this, "row-dragstart", {
                index: index,
                row: row
              });
            },
            "row-dragover": function rowDragover(_ref3) {
              var e = _ref3.e;
              e.preventDefault();
              utils_event.emitEvent(_this, "row-dragover", {
                index: index,
                row: row,
                targetElm: rowVnode.elm
              });
            }
          }),
          scopedSlots: scopedSlots
        };
        rowVnode = h(table_baseTable_tableRow["default"], helper.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(config.prefix, "-table__body ").concat(this.provider.dragging ? "dragging" : "");
      return h("transition-group", {
        "class": className,
        "attrs": {
          "tag": "tbody"
        }
      }, [this.renderBody()]);
    }

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

exports["default"] = TableBody;
//# sourceMappingURL=table-body.js.map
