import { focusElement as e, initializeKeyListItems as t, setActiveKeyListItem as n } from "../utils/focus.js";
import { KeynavCode as r, validKeyNavigationCode as i } from "../utils/dom.js";
//#region src/internal/controllers/keynav-list.controller.ts
function a() {
	return (e) => e.addInitializer((e) => new o(e));
}
var o = class {
	get #e() {
		return {
			layout: "horizontal",
			manageFocus: !0,
			manageTabindex: !0,
			loop: !1,
			dir: this.host.getAttribute("rtl"),
			...this.host.keynavListConfig,
			items: Array.from(this.host.keynavListConfig.items).filter((e) => !e.disabled)
		};
	}
	constructor(e) {
		this.host = e, this.host.addController(this);
	}
	get #t() {
		return this.host.hasAttribute("nve-keynav-disabled");
	}
	async hostConnected() {
		await this.host.updateComplete, this.#n(), this.host.addEventListener("pointerup", (e) => this.#r(e)), this.host.addEventListener("keydown", (e) => this.#i(e));
	}
	#n() {
		let { manageFocus: e, manageTabindex: n, items: r } = this.#e;
		e && n && !this.#t && t(r);
	}
	#r(e) {
		let t = this.#a(e, this.#e.items);
		t && this.#o(e, t);
	}
	#i(e) {
		if (i(e) && !this.#t) {
			let { loop: t, layout: n, dir: r, items: i } = this.#e, a = this.#a(e, i);
			if (a) {
				let { next: o, previous: s } = u(a, Array.from(i), {
					loop: t,
					layout: n,
					dir: r,
					code: e.code
				});
				o !== s && this.#o(e, i[o], i[s]);
			}
		}
	}
	#a(e, t) {
		let n = e.composedPath()[0];
		return t.find((e) => e === n) ? n : null;
	}
	#o(t, r, i) {
		let { manageFocus: a, manageTabindex: o, items: s } = this.#e;
		a && (o && n(s, r), e(r), t.preventDefault());
		let c = {
			activeItem: r,
			previousItem: i,
			code: t instanceof KeyboardEvent ? t.code : null,
			metaKey: t.ctrlKey || t.metaKey,
			items: s
		};
		r.dispatchEvent(new CustomEvent("nve-key-change", {
			bubbles: !0,
			composed: !0,
			detail: c
		}));
	}
};
function s(e) {
	let { code: t, layout: n, dir: i } = e, a = i === "rtl" ? r.ArrowRight : r.ArrowLeft, o = i === "rtl" ? r.ArrowLeft : r.ArrowRight;
	if (n !== "horizontal") {
		if (t === r.ArrowUp) return "backward";
		if (t === r.ArrowDown) return "forward";
	}
	if (n !== "vertical") {
		if (t === a) return "backward";
		if (t === o) return "forward";
	}
	return null;
}
function c(e) {
	return e === r.End ? "end" : e === r.Home ? "home" : e === r.PageUp ? "pageup" : e === r.PageDown ? "pagedown" : null;
}
function l(e, t, n) {
	let { limit: r, loop: i } = n, a = e + t;
	return a >= 0 && a <= r ? a : i ? t < 0 ? r : 0 : e;
}
function u(e, t, n) {
	let r = t.indexOf(e), i = r, a = t.length - 1;
	switch (s(n) ?? c(n.code)) {
		case "backward":
			r = l(r, -1, {
				limit: a,
				loop: n.loop
			});
			break;
		case "forward":
			r = l(r, 1, {
				limit: a,
				loop: n.loop
			});
			break;
		case "end":
			r = a;
			break;
		case "home":
			r = 0;
			break;
		case "pageup":
			r = Math.max(0, r - 4);
			break;
		case "pagedown":
			r = Math.min(a, r + 4);
			break;
		case null: break;
	}
	return {
		next: r,
		previous: i
	};
}
//#endregion
export { o as KeyNavigationListController, u as getNextKeyListItem, a as keyNavigationList };

//# sourceMappingURL=keynav-list.controller.js.map