import { GlobalStateService as e } from "../services/global.service.js";
import { onChildListMutation as t, throttle as n } from "../utils/events.js";
import { LogService as r } from "../services/log.service.js";
import { focusElement as i, getActiveElement as a, initializeKeyListItems as o, isSimpleFocusable as s, setActiveKeyListItem as c } from "../utils/focus.js";
import { KeynavCode as l, getFlattenedDOMTree as u, getFlattenedFocusableItems as d, hasInvalidDOMGrid as f, isContextMenuClick as p, validKeyNavigationCode as m } from "../utils/dom.js";
//#region src/internal/controllers/keynav-grid.controller.ts
function h() {
	return (e) => e.addInitializer((e) => new g(e));
}
var g = class {
	#e = [];
	get #t() {
		return {
			grid: this.host,
			...this.host.keynavGridConfig
		};
	}
	get #n() {
		let e = Array.from(this.#t.rows);
		return this.#t.columnRow && e.unshift(this.#t.columnRow), e;
	}
	get #r() {
		return [...Array.from(this.#t.columns ?? []), ...Array.from(this.#t.cells)];
	}
	constructor(e) {
		this.host = e, this.host.addController(this);
	}
	async hostConnected() {
		await this.host.updateComplete, o(this.#r), this.#t.grid.addEventListener("keyup", (e) => this.#s(e)), this.#t.grid.addEventListener("keydown", (e) => this.#a(e)), this.#t.grid.addEventListener("mouseup", (e) => this.#i(e)), this.#e.push(t(this.host, n(() => o(this.#r), 500)));
	}
	hostDisconnected() {
		this.#e.forEach((e) => e?.disconnect());
	}
	#i(e) {
		if (!p(e)) {
			let t = e.composedPath()[0], n = this.#r.find((e) => e === t) ? t : null;
			n && this.#o(e, n);
		}
	}
	#a(t) {
		let n = Array.from(this.#n), i = Array.from(this.#r);
		if (e.state.env !== "production" && f(n)) {
			r.warn("Invalid grid structure, all rows must have the same number of cells");
			return;
		}
		if (m(t) && s(a())) {
			let { x: e, y: r } = y(i, n, {
				code: t.code,
				ctrlKey: t.ctrlKey,
				dir: this.host.dir
			}), a = Array.from(u(n[r])).filter((e) => !!i.find((t) => t === e))[e];
			this.#o(t, a), t.preventDefault();
		}
	}
	#o(e, t) {
		c(this.#r, t);
		let n = d(t), r = n.filter((e) => s(e));
		r.length === 1 && n.length === 1 ? i(r[0]) : i(t);
		let a = {
			code: e instanceof KeyboardEvent ? e.code : null,
			shiftKey: e.shiftKey,
			activeItem: t
		};
		t.dispatchEvent(new CustomEvent("nve-key-change", {
			bubbles: !0,
			composed: !0,
			detail: a
		}));
	}
	#s(e) {
		let t = Array.from(this.#r).find((e) => e.tabIndex === 0);
		e.code === "Escape" && t?.focus(), e.code === "Enter" && t === e.composedPath()[0] && d(t)[0]?.focus();
	}
};
function _(e, t) {
	let n = t === "rtl" ? l.ArrowRight : l.ArrowLeft, r = t === "rtl" ? l.ArrowLeft : l.ArrowRight;
	return e === l.ArrowUp ? {
		dx: 0,
		dy: -1
	} : e === l.ArrowDown ? {
		dx: 0,
		dy: 1
	} : e === n ? {
		dx: -1,
		dy: 0
	} : e === r ? {
		dx: 1,
		dy: 0
	} : e === l.PageUp ? {
		dx: 0,
		dy: -4
	} : e === l.PageDown ? {
		dx: 0,
		dy: 4
	} : null;
}
function v(e, t, n) {
	let { x: r, y: i } = t, { columnCount: a, rowCount: o, ctrlKey: s } = n;
	return e === l.End ? {
		x: a,
		y: s ? o : i
	} : e === l.Home ? {
		x: 0,
		y: s ? 0 : i
	} : {
		x: r,
		y: i
	};
}
function y(e, t, n) {
	let r = e.find((e) => e.tabIndex === 0), i = t.find((e) => u(e).find((e) => e === r)), a = Array.from(u(i)).filter((t) => !!e.find((e) => e === t)), o = t.length - 1, s = a.length - 1, c = a.indexOf(r), l = t.indexOf(i), d = _(n.code, n.dir);
	return d ? (c = Math.max(0, Math.min(s, c + d.dx)), l = Math.max(0, Math.min(o, l + d.dy))) : {x: c, y: l} = v(n.code, {
		x: c,
		y: l
	}, {
		columnCount: s,
		rowCount: o,
		ctrlKey: n.ctrlKey
	}), {
		x: c,
		y: l
	};
}
//#endregion
export { g as KeyNavigationGridController, y as getNextKeyGridItem, h as keyNavigationGrid };

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