'use strict';

var jsxRuntime = require('../../../node_modules/.pnpm/react@18.2.0/node_modules/react/jsx-runtime.cjs');
var reactIcons_esm = require('../../../node_modules/.pnpm/@radix-ui_react-icons@1.3.0_react@18.2.0/node_modules/@radix-ui/react-icons/dist/react-icons.esm.cjs');
var clsx = require('../../../node_modules/.pnpm/clsx@2.1.1/node_modules/clsx/dist/clsx.cjs');
var index = require('../../../node_modules/.pnpm/@tanstack_react-table@8.9.2_react-dom@18.3.1_react@18.2.0__react@18.2.0/node_modules/@tanstack/react-table/build/lib/index.cjs');
var React = require('react');
var index$1 = require('../../../node_modules/.pnpm/react-loading-skeleton@3.4.0_react@18.2.0/node_modules/react-loading-skeleton/dist/index.cjs');
var badge = require('../../badge/badge.cjs');
var emptyState = require('../../empty-state/empty-state.cjs');
var flex = require('../../flex/flex.cjs');
var table = require('../../table/table.cjs');
var useDataTable = require('../hooks/useDataTable.cjs');
var dataTable_module = require('../data-table.module.css.cjs');

function Headers({ headerGroups = [], className = "", }) {
    return (jsxRuntime.jsxRuntimeExports.jsx(table.Table.Header, { className: className, children: headerGroups?.map((headerGroup) => (jsxRuntime.jsxRuntimeExports.jsx(table.Table.Row, { children: headerGroup?.headers?.map((header) => {
                const columnDef = header.column.columnDef;
                return (jsxRuntime.jsxRuntimeExports.jsx(table.Table.Head, { colSpan: header.colSpan, className: columnDef.classNames?.header, style: columnDef.styles?.header, children: index.flexRender(columnDef.header, header.getContext()) }, header.id));
            }) }, headerGroup?.id))) }));
}
function LoaderRows({ rowCount, columnCount, }) {
    const rows = Array.from({ length: rowCount });
    return rows.map((_, rowIndex) => {
        const columns = Array.from({ length: columnCount });
        return (jsxRuntime.jsxRuntimeExports.jsx(table.Table.Row, { children: columns.map((_, colIndex) => (jsxRuntime.jsxRuntimeExports.jsx(table.Table.Cell, { children: jsxRuntime.jsxRuntimeExports.jsx(index$1.default, { highlightColor: "var(--rs-color-background-base-primary)", baseColor: "var(--rs-color-background-base-primary-hover)" }) }, "loading-column-" + colIndex))) }, "loading-row-" + rowIndex));
    });
}
function GroupHeader({ colSpan, data, }) {
    return (jsxRuntime.jsxRuntimeExports.jsx(table.Table.SectionHeader, { colSpan: colSpan, children: jsxRuntime.jsxRuntimeExports.jsxs(flex.Flex, { gap: 3, align: "center", children: [data?.label, data.showGroupCount ? (jsxRuntime.jsxRuntimeExports.jsx(badge.Badge, { variant: "neutral", children: data?.count })) : null] }) }));
}
const Rows = React.forwardRef((props, lastRowRef) => {
    const { rows = [], onRowClick, classNames } = props;
    return (jsxRuntime.jsxRuntimeExports.jsx(jsxRuntime.jsxRuntimeExports.Fragment, { children: rows?.map((row, index$1) => {
            const isSelected = row.getIsSelected();
            const cells = row.getVisibleCells() || [];
            const isSectionHeadingRow = row.depth === 0 && row.getIsExpanded();
            const isLastRow = index$1 === rows.length - 1;
            const rowKey = row.id + "-" + index$1;
            return isSectionHeadingRow ? (jsxRuntime.jsxRuntimeExports.jsx(GroupHeader, { colSpan: cells.length, data: row.original }, rowKey)) : (jsxRuntime.jsxRuntimeExports.jsx(table.Table.Row, { className: clsx.clsx(dataTable_module.default["row"], onRowClick ? dataTable_module.default["clickable"] : "", classNames?.row), "data-state": isSelected && "selected", ref: isLastRow ? lastRowRef : undefined, onClick: () => onRowClick?.(row.original), children: cells.map((cell) => {
                    const columnDef = cell.column.columnDef;
                    return (jsxRuntime.jsxRuntimeExports.jsx(table.Table.Cell, { className: clsx.clsx(dataTable_module.default["cell"], columnDef.classNames?.cell), style: columnDef.styles?.cell, children: index.flexRender(columnDef.cell, cell.getContext()) }, cell.id));
                }) }, rowKey));
        }) }));
});
const DefaultEmptyComponent = () => (jsxRuntime.jsxRuntimeExports.jsx(emptyState.EmptyState, { icon: jsxRuntime.jsxRuntimeExports.jsx(reactIcons_esm.TableIcon, {}), heading: "No Data" }));
function Content({ emptyState, classNames = {}, }) {
    const { onRowClick, table: table$1, mode, isLoading, loadMoreData, loadingRowCount = 3, } = useDataTable.useDataTable();
    const headerGroups = table$1?.getHeaderGroups();
    const { rows = [] } = table$1?.getRowModel();
    const lastRowRef = React.useRef(null);
    const observerRef = React.useRef(null);
    const handleObserver = React.useCallback((entries) => {
        const target = entries[0];
        if (target.isIntersecting && !isLoading) {
            loadMoreData();
        }
    }, [loadMoreData, isLoading]);
    React.useEffect(() => {
        if (mode !== "server")
            return;
        if (observerRef.current) {
            observerRef.current.disconnect();
        }
        observerRef.current = new IntersectionObserver(handleObserver, {
            threshold: 0.1,
        });
        const lastRow = lastRowRef.current;
        if (lastRow) {
            observerRef.current.observe(lastRow);
        }
        return () => {
            if (observerRef.current && lastRow) {
                observerRef.current.unobserve(lastRow);
                observerRef.current.disconnect();
            }
        };
    }, [mode, rows.length, handleObserver]);
    const visibleColumnsLength = table$1.getVisibleLeafColumns().length;
    return (jsxRuntime.jsxRuntimeExports.jsx("div", { className: classNames.root, children: jsxRuntime.jsxRuntimeExports.jsxs(table.Table, { className: classNames.table, children: [jsxRuntime.jsxRuntimeExports.jsx(Headers, { headerGroups: headerGroups, className: classNames.header }), jsxRuntime.jsxRuntimeExports.jsx(table.Table.Body, { className: classNames.body, children: rows?.length || isLoading ? (jsxRuntime.jsxRuntimeExports.jsxs(jsxRuntime.jsxRuntimeExports.Fragment, { children: [jsxRuntime.jsxRuntimeExports.jsx(Rows, { rows: rows, ref: lastRowRef, onRowClick: onRowClick, classNames: {
                                    row: classNames.row,
                                } }), isLoading ? (jsxRuntime.jsxRuntimeExports.jsx(LoaderRows, { rowCount: loadingRowCount, columnCount: visibleColumnsLength })) : null] })) : (jsxRuntime.jsxRuntimeExports.jsx(table.Table.Row, { children: jsxRuntime.jsxRuntimeExports.jsx(table.Table.Cell, { colSpan: visibleColumnsLength, children: emptyState || jsxRuntime.jsxRuntimeExports.jsx(DefaultEmptyComponent, {}) }) })) })] }) }));
}

exports.Content = Content;
//# sourceMappingURL=content.cjs.map
