"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get keys () {
        return keys;
    },
    get useKeyNavigation () {
        return useKeyNavigation;
    }
});
const keys = {
    Enter: 'Enter',
    Space: 'Space',
    Tab: 'Tab',
    Escape: 'Escape',
    ArrowLeft: 'ArrowLeft',
    ArrowRight: 'ArrowRight',
    ArrowUp: 'ArrowUp',
    ArrowDown: 'ArrowDown',
    Home: 'Home',
    End: 'End',
    PageUp: 'PageUp',
    PageDown: 'PageDown'
};
const useKeyNavigation = ({ isOpen, setIsOpen, focused, dispatchFocused, finalResults, handleItemClick })=>{
    const onKeyDown = (event)=>{
        switch(event.code){
            case keys.ArrowDown:
                {
                    if (!isOpen) {
                        setIsOpen(true);
                        dispatchFocused({
                            type: 'setFirstItem'
                        });
                        break;
                    }
                    dispatchFocused({
                        type: 'setNextItem',
                        listLength: finalResults.length
                    });
                    break;
                }
            case keys.ArrowUp:
                {
                    if (!isOpen) {
                        setIsOpen(true);
                        dispatchFocused({
                            type: 'setLastItem',
                            listLength: finalResults.length
                        });
                        break;
                    }
                    dispatchFocused({
                        type: 'setPrevItem',
                        listLength: finalResults.length
                    });
                    break;
                }
            case keys.Home:
                {
                    if (!isOpen) {
                        return;
                    }
                    dispatchFocused({
                        type: 'setFirstItem'
                    });
                    break;
                }
            case keys.End:
                {
                    if (!isOpen) {
                        return;
                    }
                    dispatchFocused({
                        type: 'setLastItem',
                        listLength: finalResults.length
                    });
                    break;
                }
            case keys.Enter:
                {
                    if (!isOpen || focused === null) {
                        return;
                    }
                    dispatchFocused({
                        type: 'reset'
                    });
                    handleItemClick(finalResults[focused]);
                    break;
                }
            case keys.Tab:
            case keys.Escape:
                {
                    if (!isOpen) {
                        return;
                    }
                    setIsOpen(false);
                    dispatchFocused({
                        type: 'reset'
                    });
                    break;
                }
            default:
                {
                    break;
                }
        }
    };
    return {
        onKeyDown
    };
};
