import { isMacOS } from "@scalar/helpers/general/is-mac-os";
//#region src/libs/hot-keys.ts
/**
* These are unrelated to an input so they will still fire if we are in one,
* this is not for a textarea in which you should be able to use arrow keys, enter etc
*/
var inputHotkeys = [
	"Escape",
	"ArrowDown",
	"ArrowUp",
	"Enter",
	"F1",
	"F2",
	"F3",
	"F4",
	"F5",
	"F6",
	"F7",
	"F8",
	"F9",
	"F10",
	"F11",
	"F12"
];
/**
* Default set of keybindings
*
* Passing an empty object for hotkeys will disable them
*
* TODO we can add a merge or overwrite option
*
* The modifier can be set by the user but defaults to ctrl for windows/linux and meta for macos
*
* For the modifier key:
* - if you leave it blank it can be true or false
* - if you explicitly set it, the event must match ex: modifier false will not trigger if the modifier was pressed
*/
var DEFAULT_HOTKEYS = {
	Escape: { event: "closeModal" },
	Enter: {
		event: "executeRequest",
		modifiers: ["default"]
	},
	b: {
		event: "toggleSidebar",
		modifiers: ["default"]
	},
	k: {
		event: "openCommandPalette",
		modifiers: ["default"]
	},
	l: {
		event: "focusAddressBar",
		modifiers: ["default"]
	}
};
/** Checks if we are in an "input" */
var isInput = (ev) => {
	if (!(ev.target instanceof HTMLElement)) return false;
	const target = ev.target;
	if (target.tagName === "INPUT") return !inputHotkeys.includes(ev.key);
	if (target.tagName === "TEXTAREA") return true;
	if (target.getAttribute("contenteditable")) return true;
	if (target.contentEditable === "true") return true;
	return false;
};
var MODIFIER_DICT = {
	Alt: "altKey",
	Control: "ctrlKey",
	Shift: "shiftKey",
	Meta: "metaKey"
};
/** Converts our modifier config to the eventKey */
var getModifiers = (modifiers) => modifiers.map((modifier) => modifier === "default" ? isMacOS() ? "metaKey" : "ctrlKey" : MODIFIER_DICT[modifier]);
/**
* Global keydown handler for hotkeys
*
* This is the brain of the operation, we turn keybindings -> events
*/
var handleHotKeyDown = (ev, eventBus, { hotKeys = DEFAULT_HOTKEYS, modifiers = ["default"] } = {}) => {
	const key = ev.key === " " ? "Space" : ev.key;
	const hotKeyEvent = hotKeys[key];
	if (hotKeyEvent) {
		if (key === "Escape") eventBus.emit({ [hotKeyEvent.event]: ev });
		else if (getModifiers(hotKeyEvent.modifiers || modifiers).every((mod) => ev[mod] === true)) eventBus.emit({ [hotKeyEvent.event]: ev });
		else if (!isInput(ev) && hotKeyEvent.modifiers === void 0) eventBus.emit({ [hotKeyEvent.event]: ev });
	}
};
//#endregion
export { DEFAULT_HOTKEYS, getModifiers, handleHotKeyDown, isInput };

//# sourceMappingURL=hot-keys.js.map