/**
 * ldesign v0.2.22
 * (c) 2022/12/7 16:21 built by swimly
 * @license MIT
 */

import { h as helper } from '../../../_chunks/dep-b0f1ed22.js';
import { defineComponent, toRefs, ref, computed, reactive, onMounted, onBeforeUnmount, h } from '@vue/composition-api';
import isFunction from 'lodash/isFunction';
import upperFirst from 'lodash/upperFirst';
import isString from 'lodash/isString';
import pick from 'lodash/pick';
import get from 'lodash/get';
import useLazyLoad from '../../../hooks/useLazyLoad.js';
import { formatRowAttributes, formatRowClassNames, formatClassNames } from '../utils.js';
import { getRowFixedStyles, getColumnFixedStyles } from '../hooks/useFixed.js';
import useClassName from '../hooks/useClassName.js';
import { BaseTableProps } from '../props.js';
import { getCellKey } from '../hooks/useRowspanAndColspan.js';
import TEllipsis from './ellipsis.js';
import '../../../_chunks/dep-62ed000f.js';
import 'lodash/isObject';
import 'lodash/debounce';
import '../../../_chunks/dep-eaa95999.js';
import '../../../_chunks/dep-102361be.js';
import '../../../_chunks/dep-1a6bca30.js';
import '../../../utils/dom.js';
import 'vue';
import 'raf';
import '../../../utils/easing.js';
import '../../config-provider/useConfig.js';
import '../../config-provider/context.js';
import 'lodash/merge';
import 'lodash/mergeWith';
import '../../../_chunks/dep-b4fd1955.js';
import '../../../utils/render-node.js';
import 'lodash';
import '../../tooltip/index.js';
import '../../../utils/map-props.js';
import '../../../utils/withInstall.js';
import 'lodash/capitalize';
import '../../../config.js';
import '../../tooltip/tooltip.js';
import '../../popup/props.js';
import '../../popup/popup.js';
import '@popperjs/core';
import '../../../_chunks/dep-c68dc4b6.js';
import '../../../utils/helper.js';
import 'lodash/camelCase';
import '../../config-provider/config-receiver.js';
import '../../../utils/mixins.js';
import '../../popup/container.js';
import '../../tooltip/props.js';

const TABLE_PROPS = ["rowKey", "rowClassName", "columns", "fixedRows", "footData", "rowAttributes", "rowspanAndColspan", "scroll", "cellEmptyContent", "pagination", "onCellClick", "onRowClick", "onRowDblclick", "onRowMouseover", "onRowMousedown", "onRowMouseenter", "onRowMouseleave", "onRowMouseup"];
const ROW_LISTENERS = ["click", "dblclick", "mouseover", "mousedown", "mouseenter", "mouseleave", "mouseup"];
function renderCell(params, slots, extra) {
  const {
    col,
    row,
    rowIndex
  } = params;
  if (col.colKey === "serial-number") {
    const {
      current,
      pageSize,
      defaultCurrent,
      defaultPageSize
    } = extra?.pagination || {};
    const tCurrent = current || defaultCurrent;
    const tPageSize = pageSize || defaultPageSize;
    if (tPageSize && tCurrent) {
      return tPageSize * (tCurrent - 1) + rowIndex + 1;
    }
    return rowIndex + 1;
  }
  if (isFunction(col.cell)) return col.cell(h, params);
  if (slots[col.colKey]) return slots[col.colKey](params);
  if (isString(col.cell) && slots[col.cell]) return slots[col.cell](params);
  if (isFunction(col.render)) return col.render(h, {
    ...params,
    type: "cell"
  });
  const r = get(row, col.colKey);
  if (![void 0, "", null].includes(r)) return r;
  if (extra?.cellEmptyContent) return isFunction(extra.cellEmptyContent) ? extra.cellEmptyContent(h, params) : extra.cellEmptyContent;
  if (slots.cellEmptyContent) return slots.cellEmptyContent(params);
  return r;
}
var TrElement = defineComponent({
  name: "TR",
  props: {
    row: Object,
    rowIndex: Number,
    ellipsisOverlayClassName: String,
    classPrefix: String,
    dataLength: Number,
    rowAndColFixedPosition: Map,
    skipSpansMap: Map,
    ...pick(BaseTableProps, TABLE_PROPS),
    scrollType: String,
    rowHeight: Number,
    trs: Map,
    bufferSize: Number,
    isVirtual: Boolean,
    tableElm: {},
    tableContentElm: {}
  },
  setup(props, context) {
    const {
      tableContentElm
    } = toRefs(props);
    const trRef = ref(null);
    const {
      tdEllipsisClass,
      tableBaseClass,
      tableColFixedClasses,
      tableRowFixedClasses,
      tdAlignClasses,
      tableDraggableClasses
    } = useClassName();
    const trStyles = computed(() => getRowFixedStyles(get(props.row, props.rowKey || "id"), props.rowIndex, props.dataLength, props.fixedRows, props.rowAndColFixedPosition, tableRowFixedClasses));
    const trAttributes = computed(() => formatRowAttributes(props.rowAttributes, {
      row: props.row,
      rowIndex: props.rowIndex,
      type: "body"
    }));
    const classes = computed(() => {
      const customClasses = formatRowClassNames(props.rowClassName, {
        row: props.row,
        rowIndex: props.rowIndex,
        type: "body"
      }, props.rowKey || "id");
      return [trStyles.value?.classes, customClasses];
    });
    const {
      hasLazyLoadHolder,
      tRowHeight
    } = useLazyLoad(tableContentElm, trRef, reactive({
      ...props.scroll,
      rowIndex: props.rowIndex
    }));
    const getTrListeners = (row, rowIndex) => {
      const trListeners = {};
      ROW_LISTENERS.forEach(eventName => {
        trListeners[eventName] = e => {
          const p = {
            e,
            row,
            index: rowIndex
          };
          props[`onRow${upperFirst(eventName)}`]?.(p);
          context.emit(`row-${eventName}`, p);
        };
      });
      return trListeners;
    };
    onMounted(() => {
      const {
        trs,
        row: rowData,
        scrollType,
        isVirtual
      } = props;
      if (scrollType === "virtual") {
        if (isVirtual) {
          const {
            $index
          } = rowData;
          trs.set($index, trRef.value);
          context.emit("onRowMounted");
        }
      }
    });
    onBeforeUnmount(() => {
      if (props.isVirtual) {
        const {
          trs,
          row
        } = props;
        const {
          $index
        } = row;
        trs.delete($index);
      }
    });
    return {
      trRef,
      tableColFixedClasses,
      tableDraggableClasses,
      tSlots: context.slots,
      tdEllipsisClass,
      tableBaseClass,
      tdAlignClasses,
      trStyles,
      classes,
      trAttributes,
      tRowHeight,
      hasLazyLoadHolder,
      getTrListeners
    };
  },
  methods: {
    renderEllipsisCell(h2, cellParams, params) {
      const h = this.$createElement;
      const {
        cellNode
      } = params;
      const {
        col
      } = cellParams;
      let content = isFunction(col.ellipsis) ? col.ellipsis(h2, cellParams) : void 0;
      if (typeof col.ellipsis === "object" && isFunction(col.ellipsis.content)) content = col.ellipsis.content(h2, cellParams);
      let tooltipProps = {};
      if (typeof col.ellipsis === "object") tooltipProps = "props" in col.ellipsis ? col.ellipsis.props : col.ellipsis || void 0;
      return h(TEllipsis, {
        "attrs": {
          "placement": "top",
          "attach": this.tableElm ? () => this.tableElm : void 0,
          "tooltipContent": content && (() => content),
          "tooltipProps": tooltipProps,
          "overlayClassName": this.ellipsisOverlayClassName,
          "classPrefix": this.classPrefix
        }
      }, [cellNode]);
    },
    renderTd(h2, params, extra) {
      const h = this.$createElement;
      const {
        col,
        colIndex,
        rowIndex
      } = params;
      const {
        cellSpans,
        dataLength,
        rowAndColFixedPosition
      } = extra;
      const cellNode = renderCell(params, this.tSlots, {
        cellEmptyContent: extra.cellEmptyContent,
        pagination: this.pagination
      });
      const tdStyles = getColumnFixedStyles(col, colIndex, rowAndColFixedPosition, this.tableColFixedClasses);
      const customClasses = formatClassNames(col.className, {
        ...params,
        type: "td"
      });
      const classes = [tdStyles.classes, customClasses, {
        [this.tdEllipsisClass]: col.ellipsis,
        [this.tableBaseClass.tdLastRow]: rowIndex + cellSpans.rowspan === dataLength,
        [this.tableBaseClass.tdFirstCol]: colIndex === 0 && this.rowspanAndColspan,
        [this.tdAlignClasses[col.align]]: col.align && col.align !== "left",
        [this.tableDraggableClasses.handle]: col.colKey === "drag"
      }];
      const onClick = e => {
        const p = {
          ...params,
          e
        };
        this.onCellClick?.(p);
        this.$emit("cell-click", p);
      };
      const normalAttrs = isFunction(col.attrs) ? col.attrs({
        ...params,
        type: "td"
      }) : col.attrs;
      const attrs = {
        ...normalAttrs,
        ...cellSpans
      };
      return h("td", helper([{
        "class": classes
      }, {
        "attrs": attrs
      }, {
        "style": {
          ...tdStyles.style,
          ...attrs.style
        },
        "on": {
          "click": onClick
        }
      }]), [col.ellipsis ? this.renderEllipsisCell(h2, params, {
        cellNode
      }) : cellNode]);
    }
  },
  render(h2) {
    const h = arguments[0];
    const {
      row,
      rowIndex,
      dataLength,
      rowAndColFixedPosition
    } = this;
    const columnVNodeList = this.columns?.map((col, colIndex) => {
      const cellSpans = {};
      const params = {
        row,
        col,
        rowIndex,
        colIndex
      };
      let spanState = null;
      if (this.skipSpansMap.size) {
        const cellKey = getCellKey(row, this.rowKey, col.colKey, colIndex);
        spanState = this.skipSpansMap.get(cellKey) || {};
        spanState?.rowspan > 1 && (cellSpans.rowspan = spanState.rowspan);
        spanState?.colspan > 1 && (cellSpans.colspan = spanState.colspan);
        if (spanState.skipped) return null;
      }
      return this.renderTd(h2, params, {
        dataLength,
        rowAndColFixedPosition,
        columnLength: this.columns.length,
        cellSpans,
        cellEmptyContent: this.cellEmptyContent
      });
    });
    const attrs = this.trAttributes || {};
    return h("tr", helper([{
      "ref": "trRef"
    }, {
      "attrs": attrs
    }, {
      "style": this.trStyles?.style,
      "class": this.classes
    }, {
      "on": this.getTrListeners(row, rowIndex)
    }]), [this.hasLazyLoadHolder ? [h("td", {
      "style": {
        height: `${this.tRowHeight}px`,
        border: "none"
      }
    })] : columnVNodeList]);
  }
});

export { ROW_LISTENERS, TABLE_PROPS, TrElement as default, renderCell };
//# sourceMappingURL=tr.js.map
