import { focusElement as e } from "../utils/focus.js";
import { clickOutsideElementBounds as t, generateId as n, getAttributeListChanges as r } from "../utils/dom.js";
import { attachInternals as i } from "../utils/a11y.js";
import { getHostAnchor as a, getHostTrigger as o, hasOpenPopover as s } from "./type-native-popover.utils.js";
//#region src/internal/controllers/type-native-popover.controller.ts
var c = class {
	get #e() {
		let e = this.host.getRootNode(), t = Array.from(e.querySelectorAll(`[popovertarget="${CSS.escape ? CSS.escape(this.host.id) : this.host.id}"]`)), n = Array.from(e.querySelectorAll(`[commandfor="${CSS.escape ? CSS.escape(this.host.id) : this.host.id}"]`)), r = Array.from(e.querySelectorAll(`[interestfor="${CSS.escape ? CSS.escape(this.host.id) : this.host.id}"]`));
		return [
			...t,
			...n,
			...r
		];
	}
	constructor(e) {
		this.host = e, this.host.addController(this);
		let t = this.host.showPopover;
		this.host.showPopover = (e) => {
			if (this.host.isConnected) {
				let n = e?.source ?? null;
				n ||= this.host.anchor ? a(this.host) : this.host.trigger ? o(this.host, this.host.trigger) : globalThis.document.activeElement, t.call(this.host, { source: n });
			}
		};
	}
	async hostConnected() {
		i(this.host), this.host.popover = this.host.popoverType ?? null, await this.host.updateComplete, this.host.setAttribute("nve-popover", ""), this.#u(), this.#l(), this.#s(), this.host.inert = this.host.matches(":not(:popover-open)") && !!this.#e.length, this.host.addEventListener("beforetoggle", (e) => {
			e.newState === "open" && this.host._internals.states.add("transition-start");
		}), this.host.addEventListener("toggle", (e) => {
			this.host.behaviorTrigger && (this.host.hidden = e.newState === "closed"), e.newState === "open" && this.host.closeTimeout && setTimeout(() => this.host.hidePopover(), this.host.closeTimeout), e.newState === "closed" && this.#i(), this.host.inert = this.host.matches(":not(:popover-open)"), this.host.modal && this.#d(e.newState === "open", e.target), this.host.dispatchEvent(new CustomEvent(e.newState === "open" && e.oldState !== "open" ? "open" : "close", {
				bubbles: !0,
				composed: !0,
				detail: { trigger: e.source }
			}));
		}), this.host.addEventListener("command", ((e) => {
			e.command === "toggle-popover" && this.host.togglePopover({ source: e.source }), e.command === "hide-popover" && (this.host.hidePopover(), this.#i()), e.command === "show-popover" && this.host.showPopover({ source: e.source });
		})), this.host.addEventListener("interest", ((e) => {
			if (e.source?.localName.includes("-")) {
				let t = this.host.openDelay ?? this.#a();
				t ? this.#t = setTimeout(() => {
					this.host.isConnected && this.host.showPopover({ source: e.source });
				}, t) : this.host.showPopover({ source: e.source });
			}
		})), this.host.addEventListener("loseinterest", ((e) => {
			e.source?.localName.includes("-") && this.host.hidePopover(), this.#t &&= (clearTimeout(this.#t), null);
		}));
	}
	#t = null;
	#n = [];
	#r = null;
	async hostUpdated() {
		this.host.popover = this.host.popoverType ?? null, this.#u();
	}
	hostDisconnected() {
		this.#n.forEach((e) => e.disconnect()), this.#i();
	}
	#i() {
		this.#t &&= (clearTimeout(this.#t), null);
	}
	#a() {
		let e = getComputedStyle(this.host).interestDelayStart;
		if (!e) return 0;
		let t = parseFloat(e);
		return e.endsWith("ms") ? t : t * 1e3;
	}
	#o = !1;
	#s() {
		this.host.addEventListener("pointerdown", (e) => {
			this.host.modal && this.host.matches(":popover-open") && (this.#o = t(e, this.host));
		}), this.host.addEventListener("pointerup", (e) => {
			this.#o && this.host.popoverDismissible && this.host.modal && this.host.matches(":popover-open") && !s(this.host) && t(e, this.host) && this.host.hidePopover();
		});
	}
	get #c() {
		return this.host.trigger ? o(this.host, this.host.trigger) : null;
	}
	async #l() {
		await new Promise((e) => requestAnimationFrame(e)), this.#n.push(r(this.host, ["hidden"], () => {
			this.host.isConnected && !this.host.hidden && !this.host.matches(":popover-open") && this.host.showPopover(), this.host.isConnected && this.host.hidden && this.host.matches(":popover-open") && this.host.hidePopover();
		})), this.host.isConnected && !this.#e.length && !this.host.hidden && !this.host.matches(":popover-open") && (this.host.popover = "manual", this.host.showPopover());
	}
	#u() {
		let e = this.#c;
		this.#r && this.#r !== e && (this.#r.popoverTargetElement = null, this.#r.removeAttribute("popovertarget")), e && (this.host.popoverType === "hint" ? (e.addEventListener("mouseenter", () => this.host.showPopover({ source: e })), e.addEventListener("mouseleave", () => this.host.hidePopover()), e.addEventListener("focusout", () => this.host.hidePopover())) : (this.host.id = this.host.id ? this.host.id : n(), e.popoverTargetElement = this.host, e.setAttribute("popovertarget", this.host.id)), this.#r = e);
	}
	#d(t, n) {
		t ? this.host.getRootNode().activeElement !== this.host && !this.host.shadowRoot.activeElement && e(this.host) : e(n);
	}
};
//#endregion
export { c as TypeNativePopoverController };

//# sourceMappingURL=type-native-popover.controller.js.map