"use client";
'use strict';

var jsxRuntime = require('react/jsx-runtime');
var reactIcons_esm = require('../../../node_modules/.pnpm/@radix-ui_react-icons@1.3.2_react@19.1.1/node_modules/@radix-ui/react-icons/dist/react-icons.esm.cjs');
var index$1 = require('../../../node_modules/.pnpm/@tanstack_react-table@8.9.2_react-dom@19.1.1_react@19.1.1__react@19.1.1/node_modules/@tanstack/react-table/build/lib/index.cjs');
var index = require('../../../node_modules/.pnpm/class-variance-authority@0.7.1/node_modules/class-variance-authority/dist/index.cjs');
var React = require('react');
var badge = require('../../badge/badge.cjs');
var emptyState = require('../../empty-state/empty-state.cjs');
var flex = require('../../flex/flex.cjs');
var skeleton = require('../../skeleton/skeleton.cjs');
var table = require('../../table/table.cjs');
var dataTable_module = require('../data-table.module.css.cjs');
var useDataTable = require('../hooks/useDataTable.cjs');

function Headers({ headerGroups = [], className = '' }) {
    return (jsxRuntime.jsx(table.Table.Header, { className: className, children: headerGroups?.map(headerGroup => (jsxRuntime.jsx(table.Table.Row, { children: headerGroup?.headers?.map(header => {
                const columnDef = header.column.columnDef;
                return (jsxRuntime.jsx(table.Table.Head, { colSpan: header.colSpan, className: columnDef.classNames?.header, style: columnDef.styles?.header, children: index$1.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.jsx(table.Table.Row, { children: columns.map((_, colIndex) => (jsxRuntime.jsx(table.Table.Cell, { children: jsxRuntime.jsx(skeleton.Skeleton, {}) }, 'loading-column-' + colIndex))) }, 'loading-row-' + rowIndex));
    });
}
function GroupHeader({ colSpan, data }) {
    return (jsxRuntime.jsx(table.Table.SectionHeader, { colSpan: colSpan, children: jsxRuntime.jsxs(flex.Flex, { gap: 3, align: 'center', children: [data?.label, data.showGroupCount ? (jsxRuntime.jsx(badge.Badge, { variant: 'neutral', children: data?.count })) : null] }) }));
}
const Rows = React.forwardRef((props, lastRowRef) => {
    const { rows = [], onRowClick, classNames } = props;
    return (jsxRuntime.jsx(jsxRuntime.Fragment, { children: rows?.map((row, index$2) => {
            const isSelected = row.getIsSelected();
            const cells = row.getVisibleCells() || [];
            const isSectionHeadingRow = row.depth === 0 && row.getIsExpanded();
            const isLastRow = index$2 === rows.length - 1;
            const rowKey = row.id + '-' + index$2;
            return isSectionHeadingRow ? (jsxRuntime.jsx(GroupHeader, { colSpan: cells.length, data: row.original }, rowKey)) : (jsxRuntime.jsx(table.Table.Row, { className: index.cx(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.jsx(table.Table.Cell, { className: index.cx(dataTable_module.default['cell'], columnDef.classNames?.cell), style: columnDef.styles?.cell, children: index$1.flexRender(columnDef.cell, cell.getContext()) }, cell.id));
                }) }, rowKey));
        }) }));
});
const DefaultEmptyComponent = () => (jsxRuntime.jsx(emptyState.EmptyState, { icon: jsxRuntime.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 rowModel = table$1?.getRowModel();
    const { rows = [] } = rowModel || {};
    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.jsx("div", { className: classNames.root, children: jsxRuntime.jsxs(table.Table, { className: classNames.table, children: [jsxRuntime.jsx(Headers, { headerGroups: headerGroups, className: classNames.header }), jsxRuntime.jsx(table.Table.Body, { className: classNames.body, children: rows?.length || isLoading ? (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsx(Rows, { rows: rows, ref: lastRowRef, onRowClick: onRowClick, classNames: {
                                    row: classNames.row
                                } }), isLoading ? (jsxRuntime.jsx(LoaderRows, { rowCount: loadingRowCount, columnCount: visibleColumnsLength })) : null] })) : (jsxRuntime.jsx(table.Table.Row, { children: jsxRuntime.jsx(table.Table.Cell, { colSpan: visibleColumnsLength, children: emptyState || jsxRuntime.jsx(DefaultEmptyComponent, {}) }) })) })] }) }));
}

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