const { useEffect } = require('react');

const ESCAPE_CODE = "Escape";
const _dependencies = [];

export default function useEscapeEffect(callback, dependencies = []) {
    useEffect(() => {
        if (!callback) {
            return;
        }
        if (!Array.isArray(dependencies)) {
            dependencies = _dependencies;
            console.log('Dependencies must be an array!');
        }

        function onKeyDown(event) {
            if (event.code === ESCAPE_CODE) {
                callback(event);
            }
        }
        document.addEventListener('keydown', onKeyDown);
        return () => {
            document.removeEventListener('keydown', onKeyDown);
        };
    }, dependencies);
};

