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

import { h as helper } from '../../../_chunks/dep-29915b8e.js';
import { defineComponent, ref, computed } from '@vue/composition-api';
import isFunction from 'lodash/isFunction';
import { getColumnFixedStyles } from '../hooks/useFixed.js';
import useClassName from '../hooks/useClassName.js';
import { renderTitle } from '../hooks/useTableHeader.js';
import { formatClassNames } from '../utils.js';
import TEllipsis from './ellipsis.js';
import 'lodash/get';
import 'lodash/debounce';
import '../../../_chunks/dep-70c789e5.js';
import '../../../_chunks/dep-5210251b.js';
import '../../../_chunks/dep-47d77fc8.js';
import '../../../utils/dom.js';
import 'vue';
import 'raf';
import 'lodash/isString';
import '../../../utils/easing.js';
import '../../config-provider/useConfig.js';
import '../../config-provider/context.js';
import 'lodash';
import '../../../_chunks/dep-def4c2ce.js';
import '../hooks/useMultiHeader.js';
import '../../../utils/render-node.js';
import '../../tooltip/index.js';
import '../../../utils/map-props.js';
import '../../../utils/with-install.js';
import '../../../config.js';
import '../../tooltip/tooltip.js';
import '../../popup/props.js';
import '../../popup/popup.js';
import '@popperjs/core';
import '../../../_chunks/dep-cf772670.js';
import '../../../utils/helper.js';
import 'lodash/camelCase';
import '../../config-provider/config-receiver.js';
import 'lodash/mergeWith';
import '../../../utils/mixins.js';
import '../../popup/container.js';
import '../../tooltip/props.js';
import 'lodash/isObject';

var THead = 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 = ref();
    const classnames = useClassName();
    const {
      tableHeaderClasses,
      tableBaseClass
    } = classnames;
    const theadClasses = computed(() => [tableHeaderClasses.header, {
      [tableHeaderClasses.fixed]: props.isFixedHeader,
      [tableBaseClass.bordered]: props.bordered && props.isMultipleHeader,
      [tableHeaderClasses.multipleHeader]: props.isMultipleHeader
    }]);
    const colspanSkipMap = 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 = getColumnFixedStyles(col, index, rowAndColFixedPosition, this.tableColFixedClasses);
          const colParams = {
            col,
            colIndex: index,
            row: {},
            rowIndex: -1
          };
          const customClasses = 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 = 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(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(col.attrs) ? col.attrs({
            ...colParams,
            type: "th"
          }) : col.attrs) || {};
          if (col.colspan > 1) attrs.colspan = col.colspan;
          return h("th", 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(TEllipsis, {
            "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)]);
  }
});

export { THead as default };
//# sourceMappingURL=thead.js.map
