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

import { h as helper } from '../../../chunks/helper.js';
import { h, defineComponent, toRefs, ref, computed, reactive, onMounted, onBeforeUnmount } 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/use-lazy-load.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/_commonjsHelpers.js';
import '../../../chunks/observe.js';
import 'lodash/isObject';
import 'lodash/debounce';
import '../../../chunks/index.js';
import '../../../chunks/log.js';
import '../../../chunks/_commonjsHelpers-2e6815bd.js';
import '../../../chunks/helper2.js';
import '../../../chunks/defineProperty-838e4192.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';
import '../../../chunks/zh_CN.js';
import '../../config-provider/type.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/set-style.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 '../../tooltip/style/css.js';
import '../../tooltip/type.js';

const TABLE_PROPS = ["rowKey", "rowClassName", "columns", "fixedRows", "footData", "rowAttributes", "rowspanAndColspan", "scroll", "cellEmptyContent", "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") 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 !== null && extra !== void 0 && 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(() => {
      var _trStyles$value;
      const customClasses = formatRowClassNames(props.rowClassName, {
        row: props.row,
        rowIndex: props.rowIndex,
        type: "body"
      }, props.rowKey || "id");
      return [(_trStyles$value = trStyles.value) === null || _trStyles$value === void 0 ? void 0 : _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 => {
          var _props;
          const p = {
            e,
            row,
            index: rowIndex
          };
          (_props = props[`onRow${upperFirst(eventName)}`]) === null || _props === void 0 ? void 0 : _props.call(props, 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
      });
      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 => {
        var _this$onCellClick;
        const p = {
          ...params,
          e
        };
        (_this$onCellClick = this.onCellClick) === null || _this$onCellClick === void 0 ? void 0 : _this$onCellClick.call(this, 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) {
    var _this$columns, _this$trStyles;
    const h = arguments[0];
    const {
      row,
      rowIndex,
      dataLength,
      rowAndColFixedPosition
    } = this;
    const columnVNodeList = (_this$columns = this.columns) === null || _this$columns === void 0 ? void 0 : _this$columns.map((col, colIndex) => {
      const cellSpans = {};
      const params = {
        row,
        col,
        rowIndex,
        colIndex
      };
      let spanState = null;
      if (this.skipSpansMap.size) {
        var _spanState, _spanState2;
        const cellKey = getCellKey(row, this.rowKey, col.colKey, colIndex);
        spanState = this.skipSpansMap.get(cellKey) || {};
        ((_spanState = spanState) === null || _spanState === void 0 ? void 0 : _spanState.rowspan) > 1 && (cellSpans.rowspan = spanState.rowspan);
        ((_spanState2 = spanState) === null || _spanState2 === void 0 ? void 0 : _spanState2.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 = this.trStyles) === null || _this$trStyles === void 0 ? void 0 : _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
