//#region src/services/keybindings.ts
var e = new Set([
	"INPUT",
	"TEXTAREA",
	"SELECT"
]);
function t(t) {
	return t ? !!(e.has(t.tagName) || t.getAttribute("contenteditable") === "true" || t.classList.contains("cm-content")) : !1;
}
function n(e) {
	let t = e.toLowerCase().split("+"), n = {
		ctrl: !1,
		shift: !1,
		alt: !1,
		meta: !1,
		key: ""
	};
	for (let e of t) {
		let t = e.trim();
		t === "ctrl" ? n.ctrl = !0 : t === "shift" ? n.shift = !0 : t === "alt" ? n.alt = !0 : t === "meta" || t === "cmd" ? n.meta = !0 : n.key = t;
	}
	return n;
}
function r(e) {
	return e.split(" ").filter((e) => e.trim().length > 0).map(n);
}
function i(e, t) {
	if (e.ctrlKey !== t.ctrl || e.shiftKey !== t.shift || e.altKey !== t.alt || e.metaKey !== t.meta) return !1;
	let n = e.key.toLowerCase(), r = t.key;
	return r === "enter" ? n === "enter" : r === "escape" || r === "esc" ? n === "escape" : r === "space" ? n === " " : r === "tab" ? n === "tab" : r === "backspace" ? n === "backspace" : r === "delete" ? n === "delete" : n === r;
}
var a = 500, o = class {
	bindings = [];
	pendingChord = null;
	chordTimer = null;
	destroyed = !1;
	handleKeyDown = (e) => {
		if (this.destroyed) return;
		let n = t(document.activeElement);
		for (let t of this.bindings) {
			if (t.entry.ignoreWhenEditing === !0 && n) continue;
			let r = t.parsed, a = r[0];
			if (a) {
				if (r.length === 1) {
					if (this.pendingChord !== null) continue;
					if (i(e, a)) {
						e.preventDefault(), t.entry.handler();
						return;
					}
				} else if (r.length === 2) {
					let n = r[1];
					if (!n) continue;
					if (this.pendingChord !== null) {
						if (i(e, n) && s(this.pendingChord.parsed, a)) {
							this.clearChord(), e.preventDefault(), t.entry.handler();
							return;
						}
					} else if (i(e, a)) {
						this.startChord(a), e.preventDefault();
						return;
					}
				}
			}
		}
		this.pendingChord !== null && this.clearChord();
	};
	constructor() {
		document.addEventListener("keydown", this.handleKeyDown);
	}
	register(e, t, n, i) {
		let a = r(e), o = i?.ignoreWhenEditing, s = {
			parsed: a,
			entry: o === void 0 ? {
				keys: e,
				handler: t,
				description: n
			} : {
				keys: e,
				handler: t,
				description: n,
				ignoreWhenEditing: o
			}
		};
		return this.bindings.push(s), () => {
			let e = this.bindings.indexOf(s);
			e !== -1 && this.bindings.splice(e, 1);
		};
	}
	registerGroup(e, t) {
		for (let [n, i, a] of t) {
			let t = r(n), o = {
				keys: n,
				handler: i,
				description: a
			};
			this.bindings.push({
				parsed: t,
				entry: o,
				groupId: e
			});
		}
		return () => {
			this.unregisterGroup(e);
		};
	}
	unregisterGroup(e) {
		for (let t = this.bindings.length - 1; t >= 0; --t) {
			let n = this.bindings[t];
			n !== void 0 && n.groupId === e && this.bindings.splice(t, 1);
		}
	}
	getBindings() {
		return this.bindings.map((e) => ({ ...e.entry }));
	}
	destroy() {
		this.destroyed = !0, this.clearChord(), document.removeEventListener("keydown", this.handleKeyDown), this.bindings.length = 0;
	}
	startChord(e) {
		this.clearChord(), this.pendingChord = {
			parsed: e,
			time: Date.now()
		}, this.chordTimer = setTimeout(() => {
			this.pendingChord = null, this.chordTimer = null;
		}, a);
	}
	clearChord() {
		this.pendingChord = null, this.chordTimer !== null && (clearTimeout(this.chordTimer), this.chordTimer = null);
	}
};
function s(e, t) {
	return e.ctrl === t.ctrl && e.shift === t.shift && e.alt === t.alt && e.meta === t.meta && e.key === t.key;
}
//#endregion
export { o as KeybindingManager };

//# sourceMappingURL=keybindings.js.map