import { normalizeKeyValue } from 'lightning/utilsPrivate';

export function handleKeyDownOnDatePickerIcon(event, datepickerInterface) {
    switch (normalizeKeyValue(event.key)) {
        case 'Enter':
        case ' ':
            preventDefaultAndStopPropagation(event);
            datepickerInterface.showCalendar();
            break;
        case 'Escape':
            preventDefaultAndStopPropagation(event);
            datepickerInterface.hideCalendar();
            break;
        case 'Tab':
            if (!event.shiftKey && !datepickerInterface.isCalendarVisible()) {
                datepickerInterface.leave();
            }
            break;
        default:
    }
}

export function handleInputKeyDownBehaviour(event, datepickerInterface) {
    if (normalizeKeyValue(event.key) === 'Tab' && event.shiftKey) {
        datepickerInterface.leave();
        return;
    }

    handleBasicKeyDownBehaviour(event, datepickerInterface);
}

export function handleBasicKeyDownBehaviour(event, datepickerInterface) {
    if (!datepickerInterface.isCalendarVisible()) {
        return;
    }

    if (normalizeKeyValue(event.key) === 'Escape') {
        preventDefaultAndStopPropagation(event);
        datepickerInterface.hideCalendar();
    }
}

function preventDefaultAndStopPropagation(event) {
    event.preventDefault();
    event.stopPropagation();
}
