/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

'use strict';

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

var helper = require('../../../chunks/helper.js');
var VueCompositionAPI = require('@vue/composition-api');
var isFunction = require('lodash/isFunction');
var components_table_hooks_useFixed = require('../hooks/useFixed.js');
var components_table_hooks_useClassName = require('../hooks/useClassName.js');
var components_table_hooks_useTableHeader = require('../hooks/useTableHeader.js');
var components_table_utils = require('../utils.js');
var components_table_core_ellipsis = require('./ellipsis.js');
require('lodash/get');
require('lodash/debounce');
require('../../../chunks/log.js');
require('../../../chunks/_commonjsHelpers-2e6815bd.js');
require('../../../chunks/helper2.js');
require('../../../utils/dom.js');
require('vue');
require('raf');
require('lodash/isString');
require('../../../utils/easing.js');
require('../../config-provider/useConfig.js');
require('../../config-provider/context.js');
require('lodash');
require('../../../chunks/zh_CN.js');
require('../hooks/useMultiHeader.js');
require('../../../utils/render-node.js');
require('../../tooltip/index.js');
require('../../../utils/map-props.js');
require('../../../utils/with-install.js');
require('../../../config.js');
require('../../tooltip/tooltip.js');
require('../../popup/props.js');
require('../../popup/popup.js');
require('@popperjs/core');
require('../../../chunks/set-style.js');
require('../../../utils/helper.js');
require('lodash/camelCase');
require('../../config-provider/config-receiver.js');
require('lodash/mergeWith');
require('../../../utils/mixins.js');
require('../../popup/container.js');
require('../../tooltip/props.js');
require('lodash/isObject');

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

var isFunction__default = /*#__PURE__*/_interopDefaultLegacy(isFunction);

var THead = VueCompositionAPI.defineComponent({
  name: "THead",
  props: {
    ellipsisOverlayClassName: String,
    isFixedHeader: Boolean,
    rowAndColFixedPosition: Map,
    thWidthList: Object,
    bordered: Boolean,
    isMultipleHeader: Boolean,
    resizable: Boolean,
    spansAndLeafNodes: Object,
    thList: Array,
    columnResizeParams: Object
  },
  setup(props, _ref) {
    let {
      slots
    } = _ref;
    const theadRef = VueCompositionAPI.ref();
    const classnames = components_table_hooks_useClassName["default"]();
    const {
      tableHeaderClasses,
      tableBaseClass
    } = classnames;
    const theadClasses = VueCompositionAPI.computed(() => [tableHeaderClasses.header, {
      [tableHeaderClasses.fixed]: props.isFixedHeader,
      [tableBaseClass.bordered]: props.bordered && props.isMultipleHeader,
      [tableHeaderClasses.multipleHeader]: props.isMultipleHeader
    }]);
    const colspanSkipMap = VueCompositionAPI.computed(() => {
      const map = {};
      const list = props.thList[0];
      for (let i = 0, len = list.length; i < len; i++) {
        const item = list[i];
        if (item.colspan > 1) {
          for (let j = i + 1; j < i + item.colspan; j++) {
            if (list[j]) map[list[j].colKey] = true;
          }
        }
      }
      return map;
    });
    const getTableNode = thead => {
      let parent = thead;
      while (parent) {
        var _parent, _parent$classList;
        parent = parent.parentNode;
        if ((_parent = parent) !== null && _parent !== void 0 && (_parent$classList = _parent.classList) !== null && _parent$classList !== void 0 && _parent$classList.contains(`${props.classPrefix}-table`)) break;
      }
      return parent;
    };
    return {
      ...classnames,
      colspanSkipMap,
      theadRef,
      theadClasses,
      slots,
      getTableNode
    };
  },
  render(h) {
    const renderThNodeList = (h2, rowAndColFixedPosition, thWidthList) => {
      const thBorderMap = /* @__PURE__ */new Map();
      const thRowspanAndColspan = this.spansAndLeafNodes.rowspanAndColspanMap;
      return this.thList.map((row, rowIndex) => {
        const thRow = row.map((col, index) => {
          var _col$children;
          if (this.colspanSkipMap[col.colKey]) return null;
          const rowspanAndColspan = thRowspanAndColspan.get(col);
          if (index === 0 && rowspanAndColspan.rowspan > 1) {
            for (let j = rowIndex + 1; j < rowIndex + rowspanAndColspan.rowspan; j++) thBorderMap.set(this.thList[j][0], true);
          }
          const thStyles = components_table_hooks_useFixed.getColumnFixedStyles(col, index, rowAndColFixedPosition, this.tableColFixedClasses);
          const colParams = {
            col,
            colIndex: index,
            row: {},
            rowIndex: -1
          };
          const customClasses = components_table_utils.formatClassNames(col.className, {
            ...colParams,
            type: "th"
          });
          const thClasses = [thStyles.classes, customClasses, {
            [this.tableHeaderClasses.thBordered]: thBorderMap.get(col),
            [`${this.classPrefix}-table__th-${col.colKey}`]: col.colKey,
            [this.tdAlignClasses[col.align]]: col.align && col.align !== "left"
          }];
          const withoutChildren = !((_col$children = col.children) !== null && _col$children !== void 0 && _col$children.length);
          const width = withoutChildren && thWidthList !== null && thWidthList !== void 0 && thWidthList[col.colKey] ? `${thWidthList === null || thWidthList === void 0 ? void 0 : thWidthList[col.colKey]}px` : void 0;
          const styles = {
            ...(thStyles.style || {}),
            width
          };
          const innerTh = components_table_hooks_useTableHeader.renderTitle(h2, this.slots, col, index);
          const resizeColumnListener = this.resizable ? {
            mousedown: e => {
              var _this$columnResizePar, _this$columnResizePar2;
              return (_this$columnResizePar = this.columnResizeParams) === null || _this$columnResizePar === void 0 ? void 0 : (_this$columnResizePar2 = _this$columnResizePar.onColumnMousedown) === null || _this$columnResizePar2 === void 0 ? void 0 : _this$columnResizePar2.call(_this$columnResizePar, e, col);
            },
            mousemove: e => {
              var _this$columnResizePar3, _this$columnResizePar4;
              return (_this$columnResizePar3 = this.columnResizeParams) === null || _this$columnResizePar3 === void 0 ? void 0 : (_this$columnResizePar4 = _this$columnResizePar3.onColumnMouseover) === null || _this$columnResizePar4 === void 0 ? void 0 : _this$columnResizePar4.call(_this$columnResizePar3, e, col);
            }
          } : {};
          const content = isFunction__default["default"](col.ellipsisTitle) ? col.ellipsisTitle(h2, {
            col,
            colIndex: index
          }) : void 0;
          const isEllipsis = col.ellipsisTitle !== void 0 ? Boolean(col.ellipsisTitle) : Boolean(col.ellipsis);
          const attrs = (isFunction__default["default"](col.attrs) ? col.attrs({
            ...colParams,
            type: "th"
          }) : col.attrs) || {};
          if (col.colspan > 1) attrs.colspan = col.colspan;
          return h("th", helper.helper([{
            "key": col.colKey,
            "attrs": {
              "data-colkey": col.colKey
            },
            "class": thClasses,
            "style": styles
          }, {
            "attrs": {
              ...attrs,
              ...rowspanAndColspan
            }
          }, {}, {
            "on": resizeColumnListener
          }]), [h("div", {
            "class": this.tableBaseClass.thCellInner
          }, [isEllipsis ? h(components_table_core_ellipsis["default"], {
            "attrs": {
              "placement": "bottom",
              "attach": this.theadRef ? () => this.getTableNode(this.theadRef) : void 0,
              "tooltipContent": content && (() => content),
              "tooltipProps": typeof col.ellipsisTitle === "object" ? col.ellipsisTitle : void 0,
              "overlayClassName": this.ellipsisOverlayClassName,
              "classPrefix": this.classPrefix
            }
          }, [innerTh]) : innerTh])]);
        });
        return h("tr", {
          "key": rowIndex
        }, [thRow]);
      });
    };
    return h("thead", {
      "ref": "theadRef",
      "class": this.theadClasses
    }, [renderThNodeList(h, this.rowAndColFixedPosition, this.thWidthList)]);
  }
});

exports["default"] = THead;
//# sourceMappingURL=thead.js.map
