"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const types = require("../../../utils/types.js");
const index = require("../../../hooks/use-namespace/index.js");
const useColumns = require("./composables/use-columns.js");
const useScrollbar = require("./composables/use-scrollbar.js");
const useRow = require("./composables/use-row.js");
const useData = require("./composables/use-data.js");
const useStyles = require("./composables/use-styles.js");
const Vue = require("vue");
const shared_esmBundler = require("../../../../../@vue/shared/dist/shared.esm-bundler.js");
function useTable(props) {
  const mainTableRef = Vue.ref();
  const leftTableRef = Vue.ref();
  const rightTableRef = Vue.ref();
  const { columns, columnsStyles, columnsTotalWidth, fixedColumnsOnLeft, fixedColumnsOnRight, hasFixedColumns, mainColumns, onColumnSorted } = useColumns.useColumns(props, Vue.toRef(props, "columns"), Vue.toRef(props, "fixed"));
  const { scrollTo, scrollToLeft, scrollToTop, scrollToRow, onScroll, onVerticalScroll, scrollPos } = useScrollbar.useScrollbar(props, {
    mainTableRef,
    leftTableRef,
    rightTableRef,
    onMaybeEndReached
  });
  const ns = index.useNamespace("table-v2");
  const instance = Vue.getCurrentInstance();
  const isScrolling = Vue.shallowRef(false);
  const { expandedRowKeys, lastRenderedRowIndex, isDynamic, isResetting, rowHeights, resetAfterIndex, onRowExpanded, onRowHeightChange, onRowHovered, onRowsRendered } = useRow.useRow(props, {
    mainTableRef,
    leftTableRef,
    rightTableRef,
    tableInstance: instance,
    ns,
    isScrolling
  });
  const { data, depthMap } = useData.useData(props, {
    expandedRowKeys,
    lastRenderedRowIndex,
    resetAfterIndex
  });
  const rowsHeight = Vue.computed(() => {
    const { estimatedRowHeight, rowHeight } = props;
    const _data = Vue.unref(data);
    if (types.isNumber(estimatedRowHeight)) return Object.values(Vue.unref(rowHeights)).reduce((acc, curr) => acc + curr, 0);
    return _data.length * rowHeight;
  });
  const { bodyWidth, fixedTableHeight, mainTableHeight, leftTableWidth, rightTableWidth, windowHeight, footerHeight, emptyStyle, rootStyle, headerHeight } = useStyles.useStyles(props, {
    columnsTotalWidth,
    fixedColumnsOnLeft,
    fixedColumnsOnRight,
    rowsHeight
  });
  const containerRef = Vue.ref();
  const showEmpty = Vue.computed(() => {
    const noData = Vue.unref(data).length === 0;
    return shared_esmBundler.isArray(props.fixedData) ? props.fixedData.length === 0 && noData : noData;
  });
  function getRowHeight(rowIndex) {
    const { estimatedRowHeight, rowHeight, rowKey } = props;
    if (!estimatedRowHeight) return rowHeight;
    return Vue.unref(rowHeights)[Vue.unref(data)[rowIndex][rowKey]] || estimatedRowHeight;
  }
  const isEndReached = Vue.ref(false);
  function onMaybeEndReached() {
    const { onEndReached } = props;
    if (!onEndReached) return;
    const { scrollTop } = Vue.unref(scrollPos);
    const _totalHeight = Vue.unref(rowsHeight);
    const remainDistance = _totalHeight - (scrollTop + Vue.unref(windowHeight)) + props.hScrollbarSize;
    if (!isEndReached.value && Vue.unref(lastRenderedRowIndex) >= 0 && _totalHeight <= scrollTop + Vue.unref(mainTableHeight) - Vue.unref(headerHeight)) {
      isEndReached.value = true;
      onEndReached(remainDistance);
    } else isEndReached.value = false;
  }
  Vue.watch(() => Vue.unref(rowsHeight), () => isEndReached.value = false);
  Vue.watch(() => props.expandedRowKeys, (val) => expandedRowKeys.value = val, { deep: true });
  return {
    columns,
    containerRef,
    mainTableRef,
    leftTableRef,
    rightTableRef,
    isDynamic,
    isResetting,
    isScrolling,
    hasFixedColumns,
    columnsStyles,
    columnsTotalWidth,
    data,
    expandedRowKeys,
    depthMap,
    fixedColumnsOnLeft,
    fixedColumnsOnRight,
    mainColumns,
    bodyWidth,
    emptyStyle,
    rootStyle,
    footerHeight,
    mainTableHeight,
    fixedTableHeight,
    leftTableWidth,
    rightTableWidth,
    showEmpty,
    getRowHeight,
    onColumnSorted,
    onRowHovered,
    onRowExpanded,
    onRowsRendered,
    onRowHeightChange,
    scrollTo,
    scrollToLeft,
    scrollToTop,
    scrollToRow,
    onScroll,
    onVerticalScroll
  };
}
exports.useTable = useTable;
