import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2025 GoodData Corporation
import { useCallback, useMemo } from "react";
import { IntlWrapper } from "@gooddata/sdk-ui";
import { CHECKBOX_COLUMN_WIDTH, ROW_HEIGHT_LARGE, ROW_HEIGHT_NORMAL, SCROLLBAR_WIDTH } from "./constants.js";
import { UiAsyncTableBody } from "./UiAsyncTableBody.js";
import { UiAsyncTableEmptyState } from "./UiAsyncTableEmptyState.js";
import { UiAsyncTableHeader } from "./UiAsyncTableHeader.js";
import { UiAsyncTableRow } from "./UiAsyncTableRow.js";
import { UiAsyncTableToolbar } from "./UiAsyncTableToolbar.js";
import { getColumnWidth } from "./utils.js";
import { b } from "../asyncTableBem.js";
function AsyncTableCore(props) {
    const { width, itemHeight, isLargeRow, renderHeader, renderItem, renderEmptyState, shouldLoadNextPage } = useAsyncTable(props);
    const { filters, bulkActions, selectedItemIds, totalItemsCount, items, isLoading, hasNextPage, maxHeight = 400, skeletonItemsCount = 0, columns, scrollToIndex, isSmall, isFiltersTooLarge, loadNextPage, setSelectedItemIds, onSearch, } = props;
    return (_jsxs("div", { className: b(), style: { width }, children: [_jsx(UiAsyncTableToolbar, { filters: filters, isFiltersTooLarge: isFiltersTooLarge, bulkActions: bulkActions, selectedItemIds: selectedItemIds, setSelectedItemIds: setSelectedItemIds, totalItemsCount: totalItemsCount, items: items, isSmall: isSmall, onSearch: onSearch }), _jsxs("div", { role: "grid", children: [renderHeader(), items.length === 0 && !isLoading && renderEmptyState()] }), _jsx(UiAsyncTableBody, { items: items, maxHeight: maxHeight, itemHeight: itemHeight, skeletonItemsCount: skeletonItemsCount, hasNextPage: hasNextPage, isLoading: isLoading, onItemClick: props.onItemClick, loadNextPage: loadNextPage, columns: columns, bulkActions: bulkActions, scrollToIndex: scrollToIndex, shouldLoadNextPage: shouldLoadNextPage, renderItem: renderItem, isLargeRow: isLargeRow })] }));
}
const useAsyncTable = ({ columns, renderItem: renderItemProp, renderHeader: renderHeaderProp, renderEmptyState: renderEmptyStateProp, bulkActions, onSort, width: widthProp, sortBy, sortDirection, selectedItemIds, setSelectedItemIds, isSmall, onItemClick, }) => {
    const handleColumnClick = useCallback((key) => {
        onSort?.(key);
    }, [onSort]);
    const onItemSelect = useCallback((item) => {
        const filteredItemIds = selectedItemIds?.filter((id) => id !== item.id) ?? [];
        setSelectedItemIds(filteredItemIds.length === selectedItemIds?.length
            ? [...selectedItemIds, item.id]
            : filteredItemIds);
    }, [selectedItemIds, setSelectedItemIds]);
    const isItemSelected = useCallback((item) => {
        return selectedItemIds?.includes(item.id);
    }, [selectedItemIds]);
    const isLargeRow = useMemo(() => columns.some((column) => column.getMultiLineTextContent), [columns]);
    const itemHeight = useMemo(() => {
        return isLargeRow ? ROW_HEIGHT_LARGE : ROW_HEIGHT_NORMAL;
    }, [isLargeRow]);
    const width = useMemo(() => {
        return (widthProp ??
            columns.reduce((acc, column) => {
                const columnWidth = getColumnWidth(!!column.renderMenu, isLargeRow, column.width);
                return acc + columnWidth;
            }, SCROLLBAR_WIDTH + (!!bulkActions && CHECKBOX_COLUMN_WIDTH)));
    }, [columns, bulkActions, widthProp, isLargeRow]);
    const renderItem = useCallback((item) => {
        return renderItemProp ? (renderItemProp(item)) : (_jsx(UiAsyncTableRow, { item: item, columns: columns, onSelect: onItemSelect, isSelected: isItemSelected(item), hasCheckbox: !!bulkActions, isLarge: isLargeRow, onClick: onItemClick }));
    }, [columns, renderItemProp, onItemSelect, isItemSelected, onItemClick, bulkActions, isLargeRow]);
    const renderHeader = useCallback(() => {
        return renderHeaderProp ? (renderHeaderProp()) : (_jsx(UiAsyncTableHeader, { columns: columns, handleColumnClick: handleColumnClick, sortBy: sortBy, sortDirection: sortDirection, hasCheckbox: !!bulkActions, width: width, small: isSmall, largeRow: isLargeRow }));
    }, [
        columns,
        renderHeaderProp,
        handleColumnClick,
        bulkActions,
        width,
        sortBy,
        sortDirection,
        isLargeRow,
        isSmall,
    ]);
    const renderEmptyState = useCallback(() => {
        return renderEmptyStateProp ? renderEmptyStateProp() : _jsx(UiAsyncTableEmptyState, {});
    }, [renderEmptyStateProp]);
    const shouldLoadNextPage = useCallback((lastItemIndex, itemsCount) => {
        return lastItemIndex >= itemsCount - 1;
    }, []);
    return {
        width,
        itemHeight,
        isLargeRow,
        renderHeader,
        renderItem,
        renderEmptyState,
        shouldLoadNextPage,
        onItemSelect,
        isItemSelected,
    };
};
/**
 * @internal
 */
export function UiAsyncTable(props) {
    return (_jsx(IntlWrapper, { locale: props.locale, children: _jsx(AsyncTableCore, { ...props }) }));
}
//# sourceMappingURL=UiAsyncTable.js.map