"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useScrollToFocusItem = void 0;
var react_1 = require("react");
var SCROLL_COUNT = 3;
var useScrollToFocusItem = function (_a) {
    var focusIndex = _a.focusIndex, listRef = _a.listRef, items = _a.items;
    var visibleStartIndexRef = (0, react_1.useRef)(null);
    var visibleStopIndexRef = (0, react_1.useRef)(null);
    var handleOnItemsRendered = (0, react_1.useCallback)(function (_a) {
        var visibleStartIndex = _a.visibleStartIndex, visibleStopIndex = _a.visibleStopIndex;
        visibleStartIndexRef.current = visibleStartIndex;
        visibleStopIndexRef.current = visibleStopIndex;
    }, []);
    (0, react_1.useEffect)(function () {
        if (!listRef.current || focusIndex === null || items.length === 0) {
            return;
        }
        if (focusIndex < Math.max(visibleStartIndexRef.current + SCROLL_COUNT, 0)) {
            listRef.current.scrollToItem(Math.max(focusIndex - SCROLL_COUNT, 0), 'start');
        }
        else if (focusIndex > Math.min(visibleStopIndexRef.current - SCROLL_COUNT, items.length - 1)) {
            listRef.current.scrollToItem(Math.min(focusIndex + SCROLL_COUNT, items.length - 1), 'end');
        }
    }, [focusIndex, items.length, listRef]);
    return { handleOnItemsRendered: handleOnItemsRendered };
};
exports.useScrollToFocusItem = useScrollToFocusItem;
