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

'use strict';

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

var Vue = require('vue');
var table_baseTable_tableCell = require('./table-cell.js');
var table_baseTableProps = require('../base-table-props.js');
var config = require('../../config.js');
require('../../_chunks/dep-44e6bdf6.js');
require('@babel/runtime/helpers/toConsumableArray');
require('@babel/runtime/helpers/defineProperty');
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/render-tnode.js');
require('../../utils/set-style.js');
require('../../utils/map-props.js');
require('@babel/runtime/helpers/objectWithoutProperties');
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 Vue__default = /*#__PURE__*/_interopDefaultLegacy(Vue);

var TableHeader = Vue__default["default"].extend({
  name: "".concat(config.prefix, "-table-header"),
  props: {
    columns: table_baseTableProps["default"].columns,
    bordered: table_baseTableProps["default"].bordered
  },
  methods: {
    renderHeader: function renderHeader() {
      var h = this.$createElement;
      var trContentList = [];
      this.renderTr(this.columns, 0, trContentList);
      var theadContent = trContentList.map(function (item) {
        return h("tr", [item]);
      });
      return theadContent;
    },
    renderTr: function renderTr(columns, currentRow, trContentList) {
      var _this = this,
          _trContentList$curren;

      var thContent = [];

      if (typeof trContentList[currentRow] === "undefined") {
        trContentList[currentRow] = [];
      }

      var currentColSpan = 0;
      columns.forEach(function (column, index) {
        var children = column.children;

        if (children !== null && children !== void 0 && children.length) {
          var colSpan = _this.renderTr(children, currentRow + 1, trContentList);

          currentColSpan += colSpan;
          thContent[index] = _this.renderCell(column, 1, colSpan, index);
        }
      });
      var rowspan = 1;

      if (trContentList.length >= 1) {
        rowspan = trContentList.length - currentRow;
      }

      columns.forEach(function (column, index) {
        var children = column.children;

        if (!children || (children === null || children === void 0 ? void 0 : children.length) === 0) {
          var withBorder = currentRow > 0 && _this.bordered && thContent.length === 0 && trContentList[currentRow].length === 0;
          thContent[index] = _this.renderCell(column, rowspan, 1, index, withBorder);
          currentColSpan += 1;
        }
      });

      (_trContentList$curren = trContentList[currentRow]).push.apply(_trContentList$curren, thContent);

      return currentColSpan;
    },
    renderCell: function renderCell(column, rowspan, colspan, index, withBorder) {
      var _this2 = this;

      var h = this.$createElement;
      var title = column.title,
          render = column.render,
          scopedSlots = column.scopedSlots;
      var scopedSlotsTitle = scopedSlots === null || scopedSlots === void 0 ? void 0 : scopedSlots.title;
      var customData = {
        type: "title",
        func: "title"
      };
      var customRender;

      if (typeof this.$scopedSlots[scopedSlotsTitle] === "function") {
        customRender = this.$scopedSlots[scopedSlotsTitle];
        console.error("TDesign Table Warn: scopedSlots will be deprecated, please use `cell` and `title` instead.");
      } else if (typeof title === "string") {
        if (typeof this.$scopedSlots[title] === "function") {
          customRender = function customRender(params) {
            return _this2.$scopedSlots[title](params);
          };
        } else {
          customRender = function customRender() {
            return title;
          };
        }
      } else if (typeof title === "function") {
        customRender = function customRender(h, params) {
          return title(h, {
            colIndex: params.colIndex,
            col: params.col
          });
        };
      } else if (typeof render === "function") {
        customRender = render;
        customData.func = "render";
      } else {
        customRender = function customRender() {
          return "";
        };
      }

      var cellData = {
        col: column,
        colIndex: index,
        customData: customData,
        customRender: customRender,
        type: "th",
        withBorder: withBorder
      };
      return h(table_baseTable_tableCell["default"], {
        "attrs": {
          "cellData": cellData,
          "colspan": colspan,
          "rowspan": rowspan
        }
      });
    }
  },
  render: function render() {
    var h = arguments[0];
    return h("thead", [this.renderHeader()]);
  }
});

exports["default"] = TableHeader;
//# sourceMappingURL=table-header.js.map
