/**
* A class for managing defined Shortcuts.
*
* @memberof module:libresignage/misc
*/
class ShortcutController {
	/**
	* Construct a new ShortcutController instance.
	*
	* @params {Shortcut[]} shortcuts An array of Shortcuts.
	*/
	constructor(shortcuts) {
		this.shortcuts = shortcuts;
		this.down = [];

		document.addEventListener('keydown', e => {
			this.down.push(e.key);
			for (let s of this.shortcuts) {
				if (s.matches(this.down)) {
					e.preventDefault();
					s.exec();
				}
			}
		});

		document.addEventListener('keyup', e => {
			this.down.splice(this.down.indexOf(e.key), 1);
		});
	}

	/**
	* Register a new Shortcut.
	*
	* @param {Shortcut} shortcut The new Shortcut ot register.
	*/
	reg(shortcut) {
		this.shortcuts.push(shortcut);
	}
}
module.exports = ShortcutController;
