import { isVNode, defineComponent, computed, Text, renderSlot, createVNode } from 'vue';
import '../../../../utils/index.mjs';
import { useInjectSlots } from '../../../../components/table/src/context.mjs';
import { validateValue, getPathValue } from '../utils/valueUtil.mjs';
import { INTERNAL_COL_DEFINE } from '../utils/legacyUtil.mjs';
import { useInjectHover } from '../context/HoverContext.mjs';
import { useInjectSticky } from '../context/StickyContext.mjs';
import { eagerComputed } from '../../../../utils/vue/eagerComputed.mjs';
import { filterEmpty, flattenChildren } from '../../../../utils/vue/props.mjs';
import { warning } from '../../../../utils/log.mjs';
import { isValidElement } from '../../../../utils/is.mjs';
import { classNames } from '../../../../utils/dom/class.mjs';

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) && !isVNode(data);
}
var Cell = /* @__PURE__ */ 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, {
    slots
  }) {
    const contextSlots = useInjectSlots();
    const {
      onHover,
      startRow,
      endRow
    } = useInjectHover();
    const colSpan = computed(() => {
      var _a, _b, _c, _d;
      return (_d = (_b = props.colSpan) != null ? _b : (_a = props.additionalProps) == null ? void 0 : _a.colSpan) != null ? _d : (_c = props.additionalProps) == null ? void 0 : _c.colspan;
    });
    const rowSpan = computed(() => {
      var _a, _b, _c, _d;
      return (_d = (_b = props.rowSpan) != null ? _b : (_a = props.additionalProps) == null ? void 0 : _a.rowSpan) != null ? _d : (_c = props.additionalProps) == null ? void 0 : _c.rowspan;
    });
    const hovering = eagerComputed(() => {
      const {
        index
      } = props;
      return inHoverRange(index, rowSpan.value || 1, startRow.value, endRow.value);
    });
    const supportSticky = useInjectSticky();
    const onMouseenter = (event, mergedRowSpan) => {
      var _a;
      const {
        record,
        index,
        additionalProps
      } = props;
      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;
      if (record)
        onHover(-1, -1);
      (_a = additionalProps == null ? void 0 : additionalProps.onMouseleave) == null ? void 0 : _a.call(additionalProps, event);
    };
    const getTitle = (vnodes) => {
      const vnode = filterEmpty(vnodes)[0];
      if (isVNode(vnode)) {
        if (vnode.type === Text)
          return vnode.children;
        else
          return Array.isArray(vnode.children) ? getTitle(vnode.children) : void 0;
      } else {
        return vnode;
      }
    };
    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;
      const cellPrefixCls = `${prefixCls}-cell`;
      let cellProps;
      let childNode;
      const children = (_b = slots.default) == null ? void 0 : _b.call(slots);
      if (validateValue(children) || cellType === "header") {
        childNode = children;
      } else {
        const value = 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") {
              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 (!(INTERNAL_COL_DEFINE in column) && cellType === "body" && contextSlots.value.bodyCell && !((_c = column.slots) == null ? void 0 : _c.customRender)) {
          const child = renderSlot(contextSlots.value, "bodyCell", {
            text: value,
            value,
            record,
            index,
            column: column.__originColumn__
          }, () => {
            const fallback = childNode === void 0 ? value : childNode;
            return [typeof fallback === "object" && isValidElement(fallback) || typeof fallback !== "object" ? fallback : null];
          });
          childNode = flattenChildren(child);
        }
        if (props.transformCellText) {
          childNode = props.transformCellText({
            text: childNode,
            record,
            index,
            column: column.__originColumn__
          });
        }
      }
      if (typeof childNode === "object" && !Array.isArray(childNode) && !isVNode(childNode))
        childNode = null;
      if (ellipsis && (lastFixLeft || firstFixRight)) {
        const _childNode = function() {
          return childNode;
        }();
        childNode = 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 (isVNode(childNode))
          title = getTitle([childNode]);
      }
      const componentProps = {
        title,
        ...restCellProps,
        ...additionalProps,
        colSpan: mergedColSpan !== 1 ? mergedColSpan : null,
        rowSpan: mergedRowSpan !== 1 ? mergedRowSpan : null,
        class: classNames(cellPrefixCls, {
          [`${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,
          [`${cellPrefixCls}-row-hover`]: !cellProps && hovering.value
        }, additionalProps.class, cellClassName),
        onMouseenter: (e) => {
          onMouseenter(e, mergedRowSpan);
        },
        onMouseleave,
        style: [additionalProps.style, alignStyle, fixedStyle, cellStyle]
      };
      return createVNode(Component, componentProps, {
        default: () => {
          var _a2;
          return [appendNode, childNode, (_a2 = slots.dragHandle) == null ? void 0 : _a2.call(slots)];
        }
      });
    };
  }
});

export { Cell as default };
//# sourceMappingURL=index.mjs.map
