import { cx as e } from "../../../theme/tools.js";
import { container as t, content as n, contentPositioned as r } from "./dropdown.css.js";
import { cloneElement as i, createContext as a, isValidElement as o, useCallback as s, useContext as c, useEffect as l, useLayoutEffect as u, useRef as d, useState as f } from "react";
import { jsx as p, jsxs as m } from "react/jsx-runtime";
import { createPortal as h } from "react-dom";
//#region src/atomic/molecules/dropdown/Dropdown.tsx
var ee = new Set([
	"menu",
	"listbox",
	"tree",
	"grid",
	"dialog"
]), g = a(null), _ = () => {
	let e = c(g);
	if (e == null) throw Error("useDropdown must be used within a <Dropdown />");
	return e;
}, v = (e) => typeof e != "object" || !e ? !1 : e.$$typeof === Symbol.for("react.forward_ref"), y = ({ trigger: a, children: c, className: _, contentClassName: y, placement: b = "bottom-start", offset: x = 4, matchTriggerWidth: S = !0, open: C, defaultOpen: w = !1, onOpenChange: T, closeOnItemClick: E = !1, portalContainer: D, contentRole: O = "menu", contentAriaLabel: k, ariaLabel: A }) => {
	let j = C != null, [M, N] = f(w), P = M;
	j && (P = !!C);
	let F = d(null), I = d(null), L = d(null), [R, z] = f(null), B = s((e) => {
		j || N(e), T?.(e);
	}, [j, T]), V = s(() => {
		B(!P);
	}, [P, B]), H = s(() => {
		B(!1);
	}, [B]), U = s(() => {
		B(!0);
	}, [B]), W = s(() => {
		let e = I.current, t = L.current;
		if (e == null || t == null) return;
		let n = e.getBoundingClientRect(), r = 0, i = 0;
		switch (b) {
			case "bottom-start":
				r = n.bottom + x, i = n.left;
				break;
			case "bottom-end":
				r = n.bottom + x, i = n.right - t.offsetWidth;
				break;
			case "top-start":
				r = n.top - t.offsetHeight - x, i = n.left;
				break;
			case "top-end":
				r = n.top - t.offsetHeight - x, i = n.right - t.offsetWidth;
				break;
			default: r = n.bottom + x, i = n.left;
		}
		let a = window.innerWidth, o = window.innerHeight, s;
		S && (s = n.width);
		let c = s ?? t.offsetWidth;
		i + c > a - 4 && (i = Math.max(4, a - c - 4)), i < 4 && (i = 4);
		let l = t.offsetHeight;
		if (r + l > o - 4) if (b.startsWith("bottom")) {
			let e = n.top - l - x;
			r = e >= 4 ? e : Math.max(4, o - l - 4);
		} else r = Math.max(4, o - l - 4);
		r < 4 && (r = 4), z({
			top: r,
			left: i,
			width: s
		});
	}, [
		b,
		x,
		S
	]);
	u(() => {
		P && W();
	}, [
		P,
		W,
		c
	]), l(() => {
		if (!P) return;
		let e = () => {
			W();
		};
		return window.addEventListener("resize", e), window.addEventListener("scroll", e, !0), () => {
			window.removeEventListener("resize", e), window.removeEventListener("scroll", e, !0);
		};
	}, [P, W]), l(() => {
		if (!P) return;
		let e = (e) => {
			let t = e.target;
			F.current != null && L.current != null && !F.current.contains(t) && !L.current.contains(t) && H();
		}, t = (e) => {
			e.key === "Escape" && (H(), I.current?.focus());
		};
		return document.addEventListener("mousedown", e), document.addEventListener("keydown", t), () => {
			document.removeEventListener("mousedown", e), document.removeEventListener("keydown", t);
		};
	}, [P, H]), l(() => {
		P && (L.current?.querySelector("button, [href], input, select, textarea, [tabindex]:not([tabindex=\"-1\"])"))?.focus();
	}, [P]), l(() => {
		if (!P || !E) return;
		let e = (e) => {
			L.current?.contains(e.target) && H();
		};
		return document.addEventListener("click", e, { capture: !0 }), () => {
			document.removeEventListener("click", e, !0);
		};
	}, [
		P,
		E,
		H
	]);
	let G = a;
	if (typeof a == "function" && (G = a(P)), !o(G)) throw Error("Dropdown trigger must be a React element.");
	let K = typeof G.type == "string", q = K && G.type === "button";
	if (!(K || typeof G.type == "function" || v(G.type))) throw Error("Dropdown trigger must be a DOM element or component that accepts a ref.");
	let J = G.props.ref ?? G.ref, te = (e) => {
		I.current = e, typeof J == "function" ? J(e) : J != null && (J.current = e);
	}, Y = G.props, ne = (e) => {
		Y.onClick?.(e), !e.defaultPrevented && V();
	}, re = (e) => {
		Y.onKeyDown?.(e), !e.defaultPrevented && (e.key === "Enter" || e.key === " ") && (e.preventDefault(), V());
	}, X = !0;
	ee.has(O) && (X = O);
	let Z = {
		onClick: ne,
		"aria-haspopup": X,
		"aria-expanded": P
	};
	A != null && A !== "" && (Z["aria-label"] = A), q ? Y.type ?? (Z.type = "button") : (Z.onKeyDown = re, Y.role ?? (Z.role = "button"), Y.tabIndex ?? (Z.tabIndex = 0)), Z.ref = te;
	let Q = {};
	R != null && (Q = {
		top: R.top,
		left: R.left,
		width: R.width
	});
	let ie = D ?? document.body, ae = {
		close: H,
		open: U,
		isOpen: P
	}, $ = null;
	return P && ($ = h(/* @__PURE__ */ p("div", {
		ref: L,
		role: O,
		"aria-label": k,
		className: e(n, r, y),
		style: Q,
		children: c
	}), ie)), /* @__PURE__ */ m(g.Provider, {
		value: ae,
		children: [/* @__PURE__ */ p("div", {
			className: e(t, _),
			ref: F,
			children: i(G, Z)
		}), $]
	});
};
//#endregion
export { y as Dropdown, _ as useDropdown };

//# sourceMappingURL=Dropdown.js.map