import { defineComponent, shallowRef, computed, watchEffect, createVNode, Fragment } from 'vue';
import '../../../../utils/index.mjs';
import { getColumnsKey } from '../utils/valueUtil.mjs';
import Cell from '../Cell/index.mjs';
import { useInjectTable } from '../context/TableContext.mjs';
import { useInjectBody } from '../context/BodyContext.mjs';
import ExpandedRow from './ExpandedRow.mjs';
import { classNames } from '../../../../utils/dom/class.mjs';

var BodyRow = /* @__PURE__ */ defineComponent({
  name: "BodyRow",
  inheritAttrs: false,
  props: ["record", "index", "renderIndex", "recordKey", "expandedKeys", "rowComponent", "cellComponent", "customRow", "rowExpandable", "indent", "rowKey", "getRowKey", "childrenColumnName"],
  setup(props, {
    attrs
  }) {
    const tableContext = useInjectTable();
    const bodyContext = useInjectBody();
    const expandRended = shallowRef(false);
    const expanded = computed(() => props.expandedKeys && props.expandedKeys.has(props.recordKey));
    watchEffect(() => {
      if (expanded.value)
        expandRended.value = true;
    });
    const rowSupportExpand = computed(() => bodyContext.expandableType === "row" && (!props.rowExpandable || props.rowExpandable(props.record)));
    const nestExpandable = computed(() => bodyContext.expandableType === "nest");
    const hasNestChildren = computed(() => props.childrenColumnName && props.record && props.record[props.childrenColumnName]);
    const mergedExpandable = computed(() => rowSupportExpand.value || nestExpandable.value);
    const onInternalTriggerExpand = (record, event) => {
      bodyContext.onTriggerExpand(record, event);
    };
    const additionalProps = computed(() => {
      var _a;
      return ((_a = props.customRow) == null ? void 0 : _a.call(props, props.record, props.index)) || {};
    });
    const onClick = (event, ...args) => {
      var _a, _b;
      if (bodyContext.expandRowByClick && mergedExpandable.value)
        onInternalTriggerExpand(props.record, event);
      (_b = (_a = additionalProps.value) == null ? void 0 : _a.onClick) == null ? void 0 : _b.call(_a, event, ...args);
    };
    const computeRowClassName = computed(() => {
      const {
        record,
        index,
        indent
      } = props;
      const {
        rowClassName
      } = bodyContext;
      if (typeof rowClassName === "string")
        return rowClassName;
      else if (typeof rowClassName === "function")
        return rowClassName(record, index, indent);
      return "";
    });
    const columnsKey = computed(() => getColumnsKey(bodyContext.flattenColumns));
    return () => {
      const {
        class: className,
        style
      } = attrs;
      const {
        record,
        index,
        rowKey,
        indent = 0,
        rowComponent: RowComponent,
        cellComponent
      } = props;
      const {
        prefixCls,
        fixedInfoList,
        transformCellText
      } = tableContext;
      const {
        flattenColumns,
        expandedRowClassName,
        indentSize,
        expandIcon,
        expandedRowRender,
        expandIconColumnIndex
      } = bodyContext;
      const baseRowNode = createVNode(RowComponent, {
        ...additionalProps.value,
        "data-row-key": rowKey,
        "class": classNames(className, `${prefixCls}-row`, `${prefixCls}-row-level-${indent}`, computeRowClassName.value, additionalProps.value.class),
        "style": [style, additionalProps.value.style],
        "onClick": onClick
      }, {
        default: () => [flattenColumns.map((column, colIndex) => {
          const {
            customRender,
            dataIndex,
            className: columnClassName
          } = column;
          const key = columnsKey[colIndex];
          const fixedInfo = fixedInfoList[colIndex];
          let additionalCellProps;
          if (column.customCell)
            additionalCellProps = column.customCell(record, index, column);
          const appendNode = colIndex === (expandIconColumnIndex || 0) && nestExpandable.value ? createVNode(Fragment, null, [createVNode("span", {
            "style": {
              paddingLeft: `${indentSize * indent}px`
            },
            "class": `${prefixCls}-row-indent indent-level-${indent}`
          }, null), expandIcon({
            prefixCls,
            expanded: expanded.value,
            expandable: hasNestChildren.value,
            record,
            onExpand: onInternalTriggerExpand
          })]) : null;
          return createVNode(Cell, {
            "cellType": "body",
            "class": columnClassName,
            "ellipsis": column.ellipsis,
            "align": column.align,
            "component": cellComponent,
            "prefixCls": prefixCls,
            "key": key,
            "record": record,
            "index": index,
            "renderIndex": props.renderIndex,
            "dataIndex": dataIndex,
            "customRender": customRender,
            ...fixedInfo,
            "additionalProps": additionalCellProps,
            "column": column,
            "transformCellText": transformCellText,
            "appendNode": appendNode
          }, null);
        })]
      });
      let expandRowNode;
      if (rowSupportExpand.value && (expandRended.value || expanded.value)) {
        const expandContent = expandedRowRender({
          record,
          index,
          indent: indent + 1,
          expanded: expanded.value
        });
        const computedExpandedRowClassName = expandedRowClassName && expandedRowClassName(record, index, indent);
        expandRowNode = createVNode(ExpandedRow, {
          "expanded": expanded.value,
          "class": classNames(`${prefixCls}-expanded-row`, `${prefixCls}-expanded-row-level-${indent + 1}`, computedExpandedRowClassName),
          "prefixCls": prefixCls,
          "component": RowComponent,
          "cellComponent": cellComponent,
          "colSpan": flattenColumns.length,
          "isEmpty": false
        }, {
          default: () => [expandContent]
        });
      }
      return createVNode(Fragment, null, [baseRowNode, expandRowNode]);
    };
  }
});

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