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.isConnected && (this.host.setAttribute("nve-popover", ""), this.#m(), this.#p(), this.#d(), this.host.inert = this.host.matches(":not(:popover-open)") && !!this.#e.length, this.host.addEventListener("beforetoggle", this.#b), this.host.addEventListener("toggle", this.#x), this.host.addEventListener("command", this.#S), this.host.addEventListener("interest", this.#C), this.host.addEventListener("loseinterest", this.#w));
	}
	#t = null;
	#n = null;
	#r = [];
	#i = null;
	#a = null;
	async hostUpdated() {
		this.host.popover = this.host.popoverType ?? null, this.#m();
	}
	hostDisconnected() {
		this.#r.forEach((e) => e.disconnect()), this.#r.length = 0, this.host.removeEventListener("beforetoggle", this.#b), this.host.removeEventListener("toggle", this.#x), this.host.removeEventListener("command", this.#S), this.host.removeEventListener("interest", this.#C), this.host.removeEventListener("loseinterest", this.#w), this.host.removeEventListener("pointerdown", this.#T), this.host.removeEventListener("pointerup", this.#E), this.#y(), this.#c(), this.#s();
	}
	#o() {
		this.#s(), this.host.closeTimeout && (this.#n = setTimeout(() => this.host.hidePopover(), this.host.closeTimeout));
	}
	#s() {
		this.#n &&= (clearTimeout(this.#n), null);
	}
	#c() {
		this.#t &&= (clearTimeout(this.#t), null);
	}
	#l() {
		let e = getComputedStyle(this.host).interestDelayStart;
		if (!e) return 0;
		let t = parseFloat(e);
		return e.endsWith("ms") ? t : t * 1e3;
	}
	#u = !1;
	#d() {
		this.host.removeEventListener("pointerdown", this.#T), this.host.removeEventListener("pointerup", this.#E), this.host.addEventListener("pointerdown", this.#T), this.host.addEventListener("pointerup", this.#E);
	}
	get #f() {
		return this.host.trigger ? o(this.host, this.host.trigger) : null;
	}
	async #p() {
		await new Promise((e) => requestAnimationFrame(e)), this.#r.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());
	}
	#m() {
		let e = this.#f;
		if (this.#i && this.#i !== e && this.#v(this.#i), this.#a && this.#a !== e && this.#y(), !e) {
			this.#i = null;
			return;
		}
		this.host.popoverType === "hint" ? this.#h(e) : this.#g(e), this.#i = e;
	}
	#h(e) {
		this.#a !== e && (e.addEventListener("mouseenter", this.#D), e.addEventListener("mouseleave", this.#O), e.addEventListener("focusout", this.#O), this.#a = e);
	}
	#g(e) {
		this.#y(), this.host.id = this.host.id ? this.host.id : n(), e.popoverTargetElement = this.host, e.setAttribute("popovertarget", this.host.id);
	}
	#_(t, n) {
		t ? this.host.getRootNode().activeElement !== this.host && !this.host.shadowRoot.activeElement && e(this.host) : e(n);
	}
	#v(e) {
		e.popoverTargetElement = null, e.removeAttribute("popovertarget"), this.#a === e && this.#y();
	}
	#y() {
		this.#a?.removeEventListener("mouseenter", this.#D), this.#a?.removeEventListener("mouseleave", this.#O), this.#a?.removeEventListener("focusout", this.#O), this.#a = null;
	}
	#b = (e) => {
		e.newState === "open" && this.host._internals.states.add("transition-start");
	};
	#x = (e) => {
		this.host.behaviorTrigger && (this.host.hidden = e.newState === "closed"), e.newState === "open" && this.host.closeTimeout && this.#o(), e.newState === "closed" && (this.#c(), this.#s()), this.host.inert = this.host.matches(":not(:popover-open)"), this.host.modal && this.#_(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 }
		}));
	};
	#S = (e) => {
		e.command === "toggle-popover" && this.host.togglePopover({ source: e.source }), e.command === "hide-popover" && (this.host.hidePopover(), this.#c()), e.command === "show-popover" && this.host.showPopover({ source: e.source });
	};
	#C = (e) => {
		if (e.source?.localName.includes("-")) {
			let t = this.host.openDelay ?? this.#l();
			t ? this.#t = setTimeout(() => {
				this.host.isConnected && this.host.showPopover({ source: e.source });
			}, t) : this.host.showPopover({ source: e.source });
		}
	};
	#w = (e) => {
		e.source?.localName.includes("-") && this.host.hidePopover(), this.#c();
	};
	#T = (e) => {
		this.host.modal && this.host.matches(":popover-open") && (this.#u = t(e, this.host));
	};
	#E = (e) => {
		this.#u && this.host.popoverDismissible && this.host.modal && this.host.matches(":popover-open") && !s(this.host) && t(e, this.host) && this.host.hidePopover();
	};
	#D = (e) => {
		this.host.showPopover({ source: e.currentTarget });
	};
	#O = () => {
		this.host.hidePopover();
	};
};
//#endregion
export { c as TypeNativePopoverController };

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