import routes from 'src/router/modules';
import { useRouter } from 'vue-router';

let isNotified = false;

export default {
    created: function () {
        const router = useRouter();
        const keyBindingMap = routes
            .filter((route) => route.meta.keyBinding)
            .reduce((map, route) => {
                map['Key' + route.meta.keyBinding.toUpperCase()] = route.path;
                return map;
            }, {});

        const handleKeyDown = (event) => {
            const { ctrlKey, altKey, code } = event;

            if (ctrlKey && altKey && keyBindingMap[code] && !isNotified) {
                event.preventDefault();
                router.push(keyBindingMap[code]);
                isNotified = true;
            }
        };

        const handleKeyUp = (event) => {
            const { ctrlKey, altKey } = event;

            // Resetea la bandera cuando se sueltan las teclas ctrl o alt
            if (!ctrlKey || !altKey) {
                isNotified = false;
            }
        };

        window.addEventListener('keydown', handleKeyDown);
        window.addEventListener('keyup', handleKeyUp);
    },
};
