'use strict';

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

var vue = require('vue');
var useFlattenRecords = require('../hooks/useFlattenRecords.cjs');
var valueUtil = require('../utils/valueUtil.cjs');
var ResizeContext = require('../context/ResizeContext.cjs');
var TableContext = require('../context/TableContext.cjs');
var BodyContext = require('../context/BodyContext.cjs');
var HoverContext = require('../context/HoverContext.cjs');
var BodyRow = require('./BodyRow.cjs');
var MeasureCell = require('./MeasureCell.cjs');
var ExpandedRow = require('./ExpandedRow.cjs');

var Body = /* @__PURE__ */ vue.defineComponent({
  name: "TableBody",
  props: ["data", "getRowKey", "measureColumnWidth", "expandedKeys", "customRow", "rowExpandable", "childrenColumnName"],
  setup(props, {
    slots
  }) {
    const resizeContext = ResizeContext.useInjectResize();
    const tableContext = TableContext.useInjectTable();
    const bodyContext = BodyContext.useInjectBody();
    const flattenData = useFlattenRecords.default(vue.toRef(props, "data"), vue.toRef(props, "childrenColumnName"), vue.toRef(props, "expandedKeys"), vue.toRef(props, "getRowKey"));
    const startRow = vue.shallowRef(-1);
    const endRow = vue.shallowRef(-1);
    let timeoutId;
    HoverContext.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 vue.createVNode(BodyRow.default, {
            "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 = vue.createVNode(ExpandedRow.default, {
          "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 = valueUtil.getColumnsKey(flattenColumns);
      return vue.createVNode(WrapperComponent, {
        "class": `${prefixCls}-tbody`
      }, {
        default: () => [measureColumnWidth && vue.createVNode("tr", {
          "aria-hidden": "true",
          "class": `${prefixCls}-measure-row`,
          "style": {
            height: 0,
            fontSize: 0
          }
        }, [columnsKey.map((columnKey) => vue.createVNode(MeasureCell.default, {
          "key": columnKey,
          "columnKey": columnKey,
          "onColumnResize": onColumnResize
        }, null))]), rows]
      });
    };
  }
});

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