'use strict';

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

var vue = require('vue');
require('../../../../utils/index.cjs');
var context = require('../../../../components/table/src/context.cjs');
var valueUtil = require('../utils/valueUtil.cjs');
var legacyUtil = require('../utils/legacyUtil.cjs');
var HoverContext = require('../context/HoverContext.cjs');
var StickyContext = require('../context/StickyContext.cjs');
var eagerComputed = require('../../../../utils/vue/eagerComputed.cjs');
var props = require('../../../../utils/vue/props.cjs');
var _class = require('../../../../utils/dom/class.cjs');
var log = require('../../../../utils/log.cjs');
var vnode = require('../../../../utils/vue/vnode.cjs');
var is = require('../../../../utils/is.cjs');

function inHoverRange(cellStartRow, cellRowSpan, startRow, endRow) {
  const cellEndRow = cellStartRow + cellRowSpan - 1;
  return cellStartRow <= endRow && cellEndRow >= startRow;
}
function isRenderCell(data) {
  return data && typeof data === "object" && !Array.isArray(data) && !vue.isVNode(data);
}
var Cell = /* @__PURE__ */ vue.defineComponent({
  name: "Cell",
  props: ["prefixCls", "record", "index", "renderIndex", "dataIndex", "customRender", "component", "colSpan", "rowSpan", "fixLeft", "fixRight", "firstFixLeft", "lastFixLeft", "firstFixRight", "lastFixRight", "appendNode", "additionalProps", "ellipsis", "align", "rowType", "isSticky", "column", "cellType", "transformCellText"],
  setup(props$1, {
    slots
  }) {
    const contextSlots = context.useInjectSlots();
    const {
      onHover,
      startRow,
      endRow
    } = HoverContext.useInjectHover();
    const colSpan = vue.computed(() => {
      var _a, _b, _c, _d;
      return (_d = (_b = props$1.colSpan) != null ? _b : (_a = props$1.additionalProps) == null ? void 0 : _a.colSpan) != null ? _d : (_c = props$1.additionalProps) == null ? void 0 : _c.colspan;
    });
    const rowSpan = vue.computed(() => {
      var _a, _b, _c, _d;
      return (_d = (_b = props$1.rowSpan) != null ? _b : (_a = props$1.additionalProps) == null ? void 0 : _a.rowSpan) != null ? _d : (_c = props$1.additionalProps) == null ? void 0 : _c.rowspan;
    });
    const hovering = eagerComputed.eagerComputed(() => {
      const {
        index
      } = props$1;
      return inHoverRange(index, rowSpan.value || 1, startRow.value, endRow.value);
    });
    const supportSticky = StickyContext.useInjectSticky();
    const onMouseenter = (event, mergedRowSpan) => {
      var _a;
      const {
        record,
        index,
        additionalProps
      } = props$1;
      if (record)
        onHover(index, index + mergedRowSpan - 1);
      (_a = additionalProps == null ? void 0 : additionalProps.onMouseenter) == null ? void 0 : _a.call(additionalProps, event);
    };
    const onMouseleave = (event) => {
      var _a;
      const {
        record,
        additionalProps
      } = props$1;
      if (record)
        onHover(-1, -1);
      (_a = additionalProps == null ? void 0 : additionalProps.onMouseleave) == null ? void 0 : _a.call(additionalProps, event);
    };
    const getTitle = (vnodes) => {
      const vnode = props.filterEmpty(vnodes)[0];
      if (vue.isVNode(vnode)) {
        if (vnode.type === vue.Text)
          return vnode.children;
        else
          return Array.isArray(vnode.children) ? getTitle(vnode.children) : void 0;
      } else {
        return vnode;
      }
    };
    const hoverRef = vue.shallowRef(null);
    vue.watch([hovering, () => props$1.prefixCls, hoverRef], () => {
      const cellDom = props.findDOMNode(hoverRef.value);
      if (!cellDom)
        return;
      if (hovering.value)
        _class.addClass(cellDom, `${props$1.prefixCls}-cell-row-hover`);
      else
        _class.removeClass(cellDom, `${props$1.prefixCls}-cell-row-hover`);
    });
    return () => {
      var _a, _b, _c, _d, _e;
      const {
        prefixCls,
        record,
        index,
        renderIndex,
        dataIndex,
        customRender,
        component: Component = "td",
        fixLeft,
        fixRight,
        firstFixLeft,
        lastFixLeft,
        firstFixRight,
        lastFixRight,
        appendNode = (_a = slots.appendNode) == null ? void 0 : _a.call(slots),
        additionalProps = {},
        ellipsis,
        align,
        rowType,
        isSticky,
        column = {},
        cellType
      } = props$1;
      const cellPrefixCls = `${prefixCls}-cell`;
      let cellProps;
      let childNode;
      let componentPropsCommonClassName;
      const children = (_b = slots.default) == null ? void 0 : _b.call(slots);
      if (valueUtil.validateValue(children) || cellType === "header") {
        childNode = children;
      } else {
        const value = valueUtil.getPathValue(record, dataIndex);
        childNode = value;
        if (customRender) {
          const renderData = customRender({
            text: value,
            value,
            record,
            index,
            renderIndex,
            column: column.__originColumn__
          });
          if (isRenderCell(renderData)) {
            if (process.env.NODE_ENV !== "production") {
              log.warning(false, "`columns.customRender` return cell props is deprecated with perf issue, please use `customCell` instead.");
            }
            childNode = renderData.children;
            cellProps = renderData.props;
          } else {
            childNode = renderData;
          }
        }
        if (!(legacyUtil.INTERNAL_COL_DEFINE in column) && cellType === "body" && contextSlots.value.bodyCell && !((_c = column.slots) == null ? void 0 : _c.customRender)) {
          const child = vnode.customRenderSlot(contextSlots.value, "bodyCell", {
            text: value,
            value,
            record,
            index,
            column: column.__originColumn__
          }, () => {
            const fallback = childNode === void 0 ? value : childNode;
            return [typeof fallback === "object" && is.isValidElement(fallback) || typeof fallback !== "object" ? fallback : null];
          });
          childNode = props.flattenChildren(child);
        }
        if (props$1.transformCellText) {
          childNode = props$1.transformCellText({
            text: childNode,
            record,
            index,
            column: column.__originColumn__
          });
        }
      }
      if (typeof childNode === "object" && !Array.isArray(childNode) && !vue.isVNode(childNode))
        childNode = null;
      if (ellipsis && (lastFixLeft || firstFixRight)) {
        const _childNode = function() {
          return childNode;
        }();
        childNode = vue.createVNode("span", {
          "class": `${cellPrefixCls}-content`
        }, [childNode]);
      }
      if (Array.isArray(childNode) && childNode.length === 1)
        childNode = childNode[0];
      const {
        colSpan: cellColSpan,
        rowSpan: cellRowSpan,
        style: cellStyle,
        class: cellClassName,
        ...restCellProps
      } = cellProps || {};
      const mergedColSpan = (_d = cellColSpan !== void 0 ? cellColSpan : colSpan.value) != null ? _d : 1;
      const mergedRowSpan = (_e = cellRowSpan !== void 0 ? cellRowSpan : rowSpan.value) != null ? _e : 1;
      if (mergedColSpan === 0 || mergedRowSpan === 0)
        return null;
      const fixedStyle = {};
      const isFixLeft = typeof fixLeft === "number" && supportSticky.value;
      const isFixRight = typeof fixRight === "number" && supportSticky.value;
      if (isFixLeft) {
        fixedStyle.position = "sticky";
        fixedStyle.left = `${fixLeft}px`;
      }
      if (isFixRight) {
        fixedStyle.position = "sticky";
        fixedStyle.right = `${fixRight}px`;
      }
      const alignStyle = {};
      if (align)
        alignStyle.textAlign = align;
      let title;
      const ellipsisConfig = ellipsis === true ? {
        showTitle: true
      } : ellipsis;
      if (ellipsisConfig && (ellipsisConfig.showTitle || rowType === "header")) {
        if (typeof childNode === "string" || typeof childNode === "number")
          title = childNode.toString();
        else if (vue.isVNode(childNode))
          title = getTitle([childNode]);
      }
      vue.watch([rowSpan, startRow, endRow], () => {
        var _a2;
        (_a2 = hoverRef.value) == null ? void 0 : _a2.setAttribute("class", _class.classNames(cellPrefixCls, {
          ...componentPropsCommonClassName,
          [`${cellPrefixCls}-row-hover`]: !cellProps && hovering.value
        }, additionalProps.class, cellClassName));
      });
      componentPropsCommonClassName = {
        [`${cellPrefixCls}-fix-left`]: isFixLeft && supportSticky.value,
        [`${cellPrefixCls}-fix-left-first`]: firstFixLeft && supportSticky.value,
        [`${cellPrefixCls}-fix-left-last`]: lastFixLeft && supportSticky.value,
        [`${cellPrefixCls}-fix-right`]: isFixRight && supportSticky.value,
        [`${cellPrefixCls}-fix-right-first`]: firstFixRight && supportSticky.value,
        [`${cellPrefixCls}-fix-right-last`]: lastFixRight && supportSticky.value,
        [`${cellPrefixCls}-ellipsis`]: ellipsis,
        [`${cellPrefixCls}-with-append`]: appendNode,
        [`${cellPrefixCls}-fix-sticky`]: (isFixLeft || isFixRight) && isSticky && supportSticky.value
      };
      const componentProps = {
        title,
        ...restCellProps,
        ...additionalProps,
        colSpan: mergedColSpan !== 1 ? mergedColSpan : null,
        rowSpan: mergedRowSpan !== 1 ? mergedRowSpan : null,
        class: _class.classNames(cellPrefixCls, componentPropsCommonClassName, additionalProps.class, cellClassName),
        onMouseenter: (e) => {
          onMouseenter(e, mergedRowSpan);
        },
        onMouseleave,
        style: [additionalProps.style, alignStyle, fixedStyle, cellStyle]
      };
      return vue.createVNode(Component, {
        ...componentProps,
        "ref": hoverRef
      }, {
        default: () => {
          var _a2;
          return [appendNode, childNode, (_a2 = slots.dragHandle) == null ? void 0 : _a2.call(slots)];
        }
      });
    };
  }
});

exports.default = Cell;
//# sourceMappingURL=index.cjs.map
