import { defineComponent, toRef, shallowRef, createVNode } from 'vue';
import useFlattenRecords from '../hooks/useFlattenRecords.mjs';
import { getColumnsKey } from '../utils/valueUtil.mjs';
import { useInjectResize } from '../context/ResizeContext.mjs';
import { useInjectTable } from '../context/TableContext.mjs';
import { useInjectBody } from '../context/BodyContext.mjs';
import { useProvideHover } from '../context/HoverContext.mjs';
import BodyRow from './BodyRow.mjs';
import MeasureCell from './MeasureCell.mjs';
import ExpandedRow from './ExpandedRow.mjs';

var Body = /* @__PURE__ */ defineComponent({
  name: "TableBody",
  props: ["data", "getRowKey", "measureColumnWidth", "expandedKeys", "customRow", "rowExpandable", "childrenColumnName"],
  setup(props, {
    slots
  }) {
    const resizeContext = useInjectResize();
    const tableContext = useInjectTable();
    const bodyContext = useInjectBody();
    const flattenData = useFlattenRecords(toRef(props, "data"), toRef(props, "childrenColumnName"), toRef(props, "expandedKeys"), toRef(props, "getRowKey"));
    const startRow = shallowRef(-1);
    const endRow = shallowRef(-1);
    let timeoutId;
    useProvideHover({
      startRow,
      endRow,
      onHover: (start, end) => {
        clearTimeout(timeoutId);
        timeoutId = setTimeout(() => {
          startRow.value = start;
          endRow.value = end;
        }, 100);
      }
    });
    return () => {
      const {
        data,
        getRowKey,
        measureColumnWidth,
        expandedKeys,
        customRow,
        rowExpandable,
        childrenColumnName
      } = props;
      const {
        onColumnResize
      } = resizeContext;
      const {
        prefixCls,
        getComponent
      } = tableContext;
      const {
        flattenColumns
      } = bodyContext;
      const WrapperComponent = getComponent(["body", "wrapper"], "tbody");
      const trComponent = getComponent(["body", "row"], "tr");
      const tdComponent = getComponent(["body", "cell"], "td");
      let rows;
      if (data.length) {
        rows = flattenData.value.map((item, idx) => {
          const {
            record,
            indent,
            index: renderIndex
          } = item;
          const key = getRowKey(record, idx);
          return createVNode(BodyRow, {
            "key": key,
            "rowKey": key,
            "record": record,
            "recordKey": key,
            "index": idx,
            "renderIndex": renderIndex,
            "rowComponent": trComponent,
            "cellComponent": tdComponent,
            "expandedKeys": expandedKeys,
            "customRow": customRow,
            "getRowKey": getRowKey,
            "rowExpandable": rowExpandable,
            "childrenColumnName": childrenColumnName,
            "indent": indent
          }, null);
        });
      } else {
        rows = createVNode(ExpandedRow, {
          "expanded": true,
          "class": `${prefixCls}-placeholder`,
          "prefixCls": prefixCls,
          "component": trComponent,
          "cellComponent": tdComponent,
          "colSpan": flattenColumns.length,
          "isEmpty": true
        }, {
          default: () => {
            var _a;
            return [(_a = slots.emptyNode) == null ? void 0 : _a.call(slots)];
          }
        });
      }
      const columnsKey = getColumnsKey(flattenColumns);
      return createVNode(WrapperComponent, {
        "class": `${prefixCls}-tbody`
      }, {
        default: () => [measureColumnWidth && createVNode("tr", {
          "aria-hidden": "true",
          "class": `${prefixCls}-measure-row`,
          "style": {
            height: 0,
            fontSize: 0
          }
        }, [columnsKey.map((columnKey) => createVNode(MeasureCell, {
          "key": columnKey,
          "columnKey": columnKey,
          "onColumnResize": onColumnResize
        }, null))]), rows]
      });
    };
  }
});

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