"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const raf = require("../../../../utils/raf.js");
const style = require("../../../../utils/dom/style.js");
const index = require("../../../../hooks/use-namespace/index.js");
const util = require("../util.js");
const layoutObserver = require("../layout-observer.js");
const tokens = require("../tokens.js");
const renderHelper = require("./render-helper.js");
const defaults = require("./defaults.js");
const Vue = require("vue");
const index$1 = require("../../../../../node_modules/@vueuse/shared/index.js");
var table_body_default = Vue.defineComponent({
  name: "ElTableBody",
  props: defaults.default,
  setup(props) {
    var _a;
    const instance = Vue.getCurrentInstance();
    const parent = Vue.inject(tokens.TABLE_INJECTION_KEY);
    const ns = index.useNamespace("table");
    const { wrappedRowRender, tooltipContent, tooltipTrigger } = renderHelper.default(props);
    const { onColumnsChange, onScrollableChange } = layoutObserver.default(parent);
    const hoveredCellList = [];
    Vue.watch((_a = props.store) == null ? void 0 : _a.states.hoverRow, (newVal, oldVal) => {
      var _a2, _b;
      const el = instance == null ? void 0 : instance.vnode.el;
      const rows = Array.from((el == null ? void 0 : el.children) || []).filter((e) => e == null ? void 0 : e.classList.contains(`${ns.e("row")}`));
      let rowNum = newVal;
      const childNodes = (_a2 = rows[rowNum]) == null ? void 0 : _a2.childNodes;
      if (childNodes == null ? void 0 : childNodes.length) {
        let control = 0;
        Array.from(childNodes).reduce((acc, item, index2) => {
          var _a3, _b2;
          if (((_a3 = childNodes[index2]) == null ? void 0 : _a3.colSpan) > 1) control = (_b2 = childNodes[index2]) == null ? void 0 : _b2.colSpan;
          if (item.nodeName !== "TD" && control === 0) acc.push(index2);
          control > 0 && control--;
          return acc;
        }, []).forEach((rowIndex) => {
          var _a3;
          rowNum = newVal;
          while (rowNum > 0) {
            const preChildNodes = (_a3 = rows[rowNum - 1]) == null ? void 0 : _a3.childNodes;
            if (preChildNodes[rowIndex] && preChildNodes[rowIndex].nodeName === "TD" && preChildNodes[rowIndex].rowSpan > 1) {
              style.addClass(preChildNodes[rowIndex], "hover-cell");
              hoveredCellList.push(preChildNodes[rowIndex]);
              break;
            }
            rowNum--;
          }
        });
      } else {
        hoveredCellList.forEach((item) => style.removeClass(item, "hover-cell"));
        hoveredCellList.length = 0;
      }
      if (!((_b = props.store) == null ? void 0 : _b.states.isComplex.value) || !index$1.isClient) return;
      raf.rAF(() => {
        const oldRow = rows[oldVal];
        const newRow = rows[newVal];
        if (oldRow && !oldRow.classList.contains("hover-fixed-row")) style.removeClass(oldRow, "hover-row");
        if (newRow) style.addClass(newRow, "hover-row");
      });
    });
    Vue.onUnmounted(() => {
      var _a2;
      (_a2 = util.removePopper) == null ? void 0 : _a2.call(util);
    });
    return {
      ns,
      onColumnsChange,
      onScrollableChange,
      wrappedRowRender,
      tooltipContent,
      tooltipTrigger
    };
  },
  render() {
    const { wrappedRowRender, store } = this;
    return Vue.h("tbody", { tabIndex: -1 }, [((store == null ? void 0 : store.states.data.value) || []).reduce((acc, row) => {
      return acc.concat(wrappedRowRender(row, acc.length));
    }, [])]);
  }
});
exports.default = table_body_default;
