/**
 * 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 upperFirst = require('lodash/upperFirst');
var isString = require('lodash/isString');
var pick = require('lodash/pick');
var get = require('lodash/get');
var hooks_useLazyLoad = require('../../../hooks/use-lazy-load.js');
var components_table_utils = require('../utils.js');
var components_table_hooks_useFixed = require('../hooks/useFixed.js');
var components_table_hooks_useClassName = require('../hooks/useClassName.js');
var components_table_props = require('../props.js');
var components_table_hooks_useRowspanAndColspan = require('../hooks/useRowspanAndColspan.js');
var components_table_core_ellipsis = require('./ellipsis.js');
require('../../../chunks/observe.js');
require('lodash/isObject');
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('../../../utils/easing.js');
require('../../config-provider/useConfig.js');
require('../../config-provider/context.js');
require('lodash');
require('../../../chunks/zh_CN.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');

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

var isFunction__default = /*#__PURE__*/_interopDefaultLegacy(isFunction);
var upperFirst__default = /*#__PURE__*/_interopDefaultLegacy(upperFirst);
var isString__default = /*#__PURE__*/_interopDefaultLegacy(isString);
var pick__default = /*#__PURE__*/_interopDefaultLegacy(pick);
var get__default = /*#__PURE__*/_interopDefaultLegacy(get);

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__default["default"](col.cell)) return col.cell(VueCompositionAPI.h, params);
  if (slots[col.colKey]) return slots[col.colKey](params);
  if (isString__default["default"](col.cell) && slots[col.cell]) return slots[col.cell](params);
  if (isFunction__default["default"](col.render)) return col.render(VueCompositionAPI.h, {
    ...params,
    type: "cell"
  });
  const r = get__default["default"](row, col.colKey);
  if (![void 0, "", null].includes(r)) return r;
  if (extra !== null && extra !== void 0 && extra.cellEmptyContent) return isFunction__default["default"](extra.cellEmptyContent) ? extra.cellEmptyContent(VueCompositionAPI.h, params) : extra.cellEmptyContent;
  if (slots.cellEmptyContent) return slots.cellEmptyContent(params);
  return r;
}
var TrElement = VueCompositionAPI.defineComponent({
  name: "TR",
  props: {
    row: Object,
    rowIndex: Number,
    ellipsisOverlayClassName: String,
    classPrefix: String,
    dataLength: Number,
    rowAndColFixedPosition: Map,
    skipSpansMap: Map,
    ...pick__default["default"](components_table_props.BaseTableProps, TABLE_PROPS),
    scrollType: String,
    rowHeight: Number,
    trs: Map,
    bufferSize: Number,
    isVirtual: Boolean,
    tableElm: {},
    tableContentElm: {}
  },
  setup(props, context) {
    const {
      tableContentElm
    } = VueCompositionAPI.toRefs(props);
    const trRef = VueCompositionAPI.ref(null);
    const {
      tdEllipsisClass,
      tableBaseClass,
      tableColFixedClasses,
      tableRowFixedClasses,
      tdAlignClasses,
      tableDraggableClasses
    } = components_table_hooks_useClassName["default"]();
    const trStyles = VueCompositionAPI.computed(() => components_table_hooks_useFixed.getRowFixedStyles(get__default["default"](props.row, props.rowKey || "id"), props.rowIndex, props.dataLength, props.fixedRows, props.rowAndColFixedPosition, tableRowFixedClasses));
    const trAttributes = VueCompositionAPI.computed(() => components_table_utils.formatRowAttributes(props.rowAttributes, {
      row: props.row,
      rowIndex: props.rowIndex,
      type: "body"
    }));
    const classes = VueCompositionAPI.computed(() => {
      var _trStyles$value;
      const customClasses = components_table_utils.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
    } = hooks_useLazyLoad["default"](tableContentElm, trRef, VueCompositionAPI.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__default["default"](eventName)}`]) === null || _props === void 0 ? void 0 : _props.call(props, p);
          context.emit(`row-${eventName}`, p);
        };
      });
      return trListeners;
    };
    VueCompositionAPI.onMounted(() => {
      const {
        trs,
        row: rowData,
        scrollType,
        isVirtual
      } = props;
      if (scrollType === "virtual") {
        if (isVirtual) {
          const {
            $index
          } = rowData;
          trs.set($index, trRef.value);
          context.emit("onRowMounted");
        }
      }
    });
    VueCompositionAPI.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__default["default"](col.ellipsis) ? col.ellipsis(h2, cellParams) : void 0;
      if (typeof col.ellipsis === "object" && isFunction__default["default"](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(components_table_core_ellipsis["default"], {
        "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 = components_table_hooks_useFixed.getColumnFixedStyles(col, colIndex, rowAndColFixedPosition, this.tableColFixedClasses);
      const customClasses = components_table_utils.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__default["default"](col.attrs) ? col.attrs({
        ...params,
        type: "td"
      }) : col.attrs;
      const attrs = {
        ...normalAttrs,
        ...cellSpans
      };
      return h("td", helper.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 = components_table_hooks_useRowspanAndColspan.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.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]);
  }
});

exports.ROW_LISTENERS = ROW_LISTENERS;
exports.TABLE_PROPS = TABLE_PROPS;
exports["default"] = TrElement;
exports.renderCell = renderCell;
//# sourceMappingURL=tr.js.map
