import { useState, useEffect, useRef, useCallback } from 'react';
import { isNil } from 'ramda';
import { isTitleListItem } from '../../VirtualGroupedList';
export var useKeyboardNavigation = function (_a) {
    var items = _a.items, open = _a.open, onSelectFocusedItem = _a.onSelectFocusedItem, selectedItems = _a.selectedItems, onClose = _a.onClose;
    var _b = useState(null), focusIndex = _b[0], setFocusIndex = _b[1];
    var restoreFocusIndex = useRef(false);
    var increaseFocusIndex = useCallback(function (currentIndex) {
        var newIndex = currentIndex === null ? increaseFocusIndex(-1) : currentIndex;
        while (currentIndex !== null &&
            newIndex < items.length &&
            (newIndex <= currentIndex || (newIndex >= 0 && isTitleListItem(items[newIndex])))) {
            newIndex += 1;
        }
        return newIndex !== items.length ? newIndex : increaseFocusIndex(-1);
    }, [items]);
    var decreaseFocusIndex = useCallback(function (currentIndex) {
        var newIndex = currentIndex === null ? items.length - 1 : currentIndex;
        while (currentIndex !== null &&
            newIndex >= 0 &&
            (newIndex >= currentIndex || isTitleListItem(items[newIndex]))) {
            newIndex -= 1;
        }
        return newIndex < 0 ? items.length - 1 : newIndex;
    }, [items]);
    var handleSelectFocusedItem = useCallback(function () {
        if (!isNil(focusIndex)) {
            var item = items[focusIndex].item;
            if (!item.item.notSelectable)
                onSelectFocusedItem({ item: item.item, groupData: item });
        }
    }, [onSelectFocusedItem, items, focusIndex]);
    var handleKeyDown = useCallback(function (event) {
        if (items.length === 0) {
            return;
        }
        switch (event.key) {
            case 'Down': // IE/Edge specific value
            case 'ArrowDown':
                setFocusIndex(increaseFocusIndex);
                event.preventDefault();
                return false;
            case 'Up': // IE/Edge specific value
            case 'ArrowUp':
                setFocusIndex(decreaseFocusIndex);
                event.preventDefault();
                return false;
            case 'Enter':
                restoreFocusIndex.current = true;
                handleSelectFocusedItem();
                break;
            case 'Tab':
                onClose === null || onClose === void 0 ? void 0 : onClose(event);
                break;
            default:
                return;
        }
    }, [items, increaseFocusIndex, decreaseFocusIndex, handleSelectFocusedItem, onClose]);
    useEffect(function () {
        if (restoreFocusIndex.current) {
            restoreFocusIndex.current = false;
            var moveIndex = items.findIndex(function (listItem) { return listItem.item.moveTo; });
            if (moveIndex > -1)
                setFocusIndex(moveIndex);
        }
        else {
            setFocusIndex(null);
        }
    }, [items, open, selectedItems]);
    return { focusIndex: focusIndex, handleKeyDown: handleKeyDown };
};
