import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2025-2026 GoodData Corporation
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { isEnterKey } from "../../../utils/events.js";
import { makeGridKeyboardNavigation } from "../../@utils/keyboardNavigation.js";
import { UiPagedVirtualList } from "../../UiPagedVirtualList/UiPagedVirtualList.js";
import { useSkeletonItem } from "./SkeletonItemFactory.js";
import { getCellId, getItemKey, getRowId } from "./utils.js";
export function UiAsyncTableBody({ items, maxHeight, itemHeight, skeletonItemsCount, hasNextPage, isLoading, onItemClick, loadNextPage, columns, bulkActions, scrollToIndex, isLargeRow, shouldLoadNextPage, renderItem, }) {
    const SkeletonItem = useSkeletonItem(columns, bulkActions, isLargeRow ?? false);
    const { handleKeyDown, handleFocus, focusedRowIndex, focusedColumnIndex, focusedItemRef } = useAsyncTableBodyKeyboardNavigation(items.length, columns.length, !!bulkActions, scrollToIndex);
    const activeDescendantId = useMemo(() => {
        if (focusedRowIndex === undefined || focusedRowIndex < 0 || focusedRowIndex >= items.length) {
            return undefined;
        }
        const itemId = getItemKey(items[focusedRowIndex]);
        if (focusedColumnIndex !== undefined) {
            return getCellId(itemId, focusedColumnIndex);
        }
        return getRowId(itemId);
    }, [focusedRowIndex, focusedColumnIndex, items]);
    return (_jsx(UiPagedVirtualList, { maxHeight: maxHeight, itemHeight: itemHeight, itemsGap: 0, itemPadding: 0, items: items, skeletonItemsCount: skeletonItemsCount, hasNextPage: hasNextPage, isLoading: isLoading, onKeyDownSelect: onItemClick, loadNextPage: loadNextPage, SkeletonItem: SkeletonItem, scrollbarHoverEffect: true, scrollToIndex: scrollToIndex ?? focusedRowIndex, shouldLoadNextPage: shouldLoadNextPage, tabIndex: items.length ? 0 : -1, customKeyboardNavigationHandler: handleKeyDown, onFocus: handleFocus, listboxProps: {
            "aria-activedescendant": activeDescendantId,
        }, children: (item, focusedIndex) => {
            const itemIndex = focusedIndex ?? 0;
            return renderItem(item, itemIndex, focusedItemRef, itemIndex === focusedRowIndex, focusedColumnIndex);
        } }));
}
const useAsyncTableBodyKeyboardNavigation = (rowsLength, definedColumnsLength, hasBulkActions, scrollToIndex) => {
    const [focusedRowIndex, setFocusedRowIndex] = useState(undefined);
    const [focusedColumnIndex, setFocusedColumnIndex] = useState(undefined);
    const focusedItemRef = useRef(null);
    useEffect(() => {
        setFocusedRowIndex(undefined);
    }, [scrollToIndex]);
    const columnsLength = useMemo(() => {
        return definedColumnsLength + (hasBulkActions ? 1 : 0);
    }, [definedColumnsLength, hasBulkActions]);
    const isFirstRow = useMemo(() => {
        return focusedRowIndex === undefined || focusedRowIndex === 0;
    }, [focusedRowIndex]);
    const isLastRow = useMemo(() => {
        return focusedRowIndex === rowsLength - 1;
    }, [focusedRowIndex, rowsLength]);
    const isFirstColumn = useMemo(() => {
        return focusedColumnIndex === 0;
    }, [focusedColumnIndex]);
    const isLastColumn = useMemo(() => {
        return focusedColumnIndex === columnsLength - 1;
    }, [focusedColumnIndex, columnsLength]);
    const handleKeyDown = useMemo(() => {
        return makeGridKeyboardNavigation({
            onFocusDown: () => {
                setFocusedRowIndex(isLastRow ? 0 : (focusedRowIndex ?? 0) + 1);
            },
            onFocusUp: () => {
                setFocusedRowIndex(isFirstRow ? rowsLength - 1 : (focusedRowIndex ?? 0) - 1);
            },
            onFocusFirst: () => {
                setFocusedRowIndex(0);
            },
            onFocusLast: () => {
                setFocusedRowIndex(rowsLength - 1);
            },
            onFocusLeft: () => {
                if (focusedColumnIndex === undefined) {
                    setFocusedColumnIndex(columnsLength - 1);
                    return;
                }
                setFocusedColumnIndex(isFirstColumn ? undefined : focusedColumnIndex - 1);
            },
            onFocusRight: () => {
                if (focusedColumnIndex === undefined) {
                    setFocusedColumnIndex(0);
                    return;
                }
                setFocusedColumnIndex(isLastColumn ? undefined : focusedColumnIndex + 1);
            },
            onSelect: (e) => {
                const isCheckbox = focusedColumnIndex === 0 && hasBulkActions;
                if (isEnterKey(e) && isCheckbox) {
                    return;
                }
                focusedItemRef.current?.click();
            },
        });
    }, [
        rowsLength,
        columnsLength,
        focusedRowIndex,
        setFocusedRowIndex,
        focusedColumnIndex,
        setFocusedColumnIndex,
        hasBulkActions,
        isFirstRow,
        isLastRow,
        isFirstColumn,
        isLastColumn,
    ]);
    const handleFocus = useCallback((e) => {
        if (focusedRowIndex === undefined && e.target.matches(":focus-visible")) {
            setFocusedRowIndex(0);
        }
    }, [focusedRowIndex]);
    return {
        handleKeyDown,
        handleFocus,
        focusedRowIndex,
        focusedColumnIndex,
        focusedItemRef,
    };
};
