import { jsx as _jsx } from "react/jsx-runtime";
/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { useRef, forwardRef, useImperativeHandle, useCallback, useMemo, } from 'react';
import { VirtualizedList, SCROLL_TO_ITEM_END, } from './VirtualizedList.js';
import { useScrollable } from '../../contexts/ScrollProvider.js';
import { Box } from 'ink';
import { useKeypress } from '../../hooks/useKeypress.js';
import { keyMatchers, Command } from '../../keyMatchers.js';
/**
 * @plan PLAN-20251215-OLDUI-SCROLL.P05
 * @requirement REQ-456.4
 */
function ScrollableList(props, ref) {
    const { hasFocus } = props;
    const virtualizedListRef = useRef(null);
    const containerRef = useRef(null);
    useImperativeHandle(ref, () => ({
        scrollBy: (delta) => virtualizedListRef.current?.scrollBy(delta),
        scrollTo: (offset) => virtualizedListRef.current?.scrollTo(offset),
        scrollToEnd: () => virtualizedListRef.current?.scrollToEnd(),
        scrollToIndex: (params) => virtualizedListRef.current?.scrollToIndex(params),
        scrollToItem: (params) => virtualizedListRef.current?.scrollToItem(params),
        getScrollIndex: () => virtualizedListRef.current?.getScrollIndex() ?? 0,
        getScrollState: () => virtualizedListRef.current?.getScrollState() ?? {
            scrollTop: 0,
            scrollHeight: 0,
            innerHeight: 0,
        },
    }), []);
    const getScrollState = useCallback(() => virtualizedListRef.current?.getScrollState() ?? {
        scrollTop: 0,
        scrollHeight: 0,
        innerHeight: 0,
    }, []);
    const scrollBy = useCallback((delta) => {
        virtualizedListRef.current?.scrollBy(delta);
    }, []);
    const flashScrollbar = useCallback(() => { }, []);
    useKeypress((key) => {
        if (keyMatchers[Command.SCROLL_UP](key)) {
            scrollBy(-1);
            flashScrollbar();
        }
        else if (keyMatchers[Command.SCROLL_DOWN](key)) {
            scrollBy(1);
            flashScrollbar();
        }
        else if (keyMatchers[Command.PAGE_UP](key) ||
            keyMatchers[Command.PAGE_DOWN](key)) {
            const direction = keyMatchers[Command.PAGE_UP](key) ? -1 : 1;
            const scrollState = getScrollState();
            const current = scrollState.scrollTop;
            const innerHeight = scrollState.innerHeight;
            virtualizedListRef.current?.scrollTo(current + direction * innerHeight);
            flashScrollbar();
        }
        else if (keyMatchers[Command.SCROLL_HOME](key)) {
            virtualizedListRef.current?.scrollTo(0);
            flashScrollbar();
        }
        else if (keyMatchers[Command.SCROLL_END](key)) {
            virtualizedListRef.current?.scrollTo(SCROLL_TO_ITEM_END);
            flashScrollbar();
        }
    }, { isActive: hasFocus });
    const hasFocusCallback = useCallback(() => hasFocus, [hasFocus]);
    const scrollableEntry = useMemo(() => ({
        ref: containerRef,
        getScrollState,
        scrollBy,
        scrollTo: (scrollTop) => virtualizedListRef.current?.scrollTo(scrollTop),
        hasFocus: hasFocusCallback,
        flashScrollbar,
    }), [getScrollState, hasFocusCallback, flashScrollbar, scrollBy]);
    useScrollable(scrollableEntry, hasFocus);
    const scrollbarColor = hasFocus ? 'gray' : 'darkgray';
    return (_jsx(Box, { ref: containerRef, flexGrow: 1, flexShrink: 1, flexDirection: "column", overflow: "hidden", children: _jsx(VirtualizedList, { ref: virtualizedListRef, ...props, scrollbarThumbColor: scrollbarColor }) }));
}
const ScrollableListWithForwardRef = forwardRef(ScrollableList);
export { ScrollableListWithForwardRef as ScrollableList };
//# sourceMappingURL=ScrollableList.js.map