import { Button as e } from "../primitives/button.js";
import { useConsent as t } from "../hooks/useConsent.js";
import { Dialog as n, DialogContent as r, DialogDescription as i, DialogFooter as a, DialogHeader as o, DialogTitle as s } from "../primitives/dialog.js";
import { t as c } from "../../switch-CoTkNGEB.js";
import { Skeleton as l } from "../primitives/skeleton.js";
import { Collapsible as u, CollapsibleContent as d, CollapsibleTrigger as f } from "../primitives/collapsible.js";
import { useEffect as p, useState as m } from "react";
import { jsx as h, jsxs as g } from "react/jsx-runtime";
//#region src/react/consent/preferences.tsx
function _({ open: c, onOpenChange: u }) {
	let { preferences: d, acceptAll: f, rejectAll: _, fetchCategories: y, saveConsent: b } = t(), [x, S] = m([]), [C, w] = m(!1), [T, E] = m(!1), [D, O] = m({
		essential: !0,
		performance: !1,
		analytics: !1,
		advertising: !1,
		social_media: !1
	});
	p(() => {
		c && (w(!0), y().then((e) => {
			S(e);
		}).catch(() => {
			S([]);
		}).finally(() => {
			w(!1);
		}));
	}, [c, y]), p(() => {
		c && d && O(d);
	}, [c, d]);
	function k(e, t) {
		O((n) => ({
			...n,
			[e]: t
		}));
	}
	function A() {
		f(), u(!1);
	}
	function j() {
		_(), u(!1);
	}
	async function M() {
		E(!0);
		try {
			await b(D, "preferences");
		} finally {
			E(!1), u(!1);
		}
	}
	return /* @__PURE__ */ h(n, {
		open: c,
		onOpenChange: u,
		children: /* @__PURE__ */ g(r, {
			className: "max-h-[85dvh] overflow-y-auto sm:max-w-xl",
			showCloseButton: !0,
			children: [
				/* @__PURE__ */ g(o, { children: [/* @__PURE__ */ h(s, { children: "Cookie preferences" }), /* @__PURE__ */ h(i, { children: "Manage which categories of cookies you allow. Required cookies cannot be disabled." })] }),
				/* @__PURE__ */ g("div", {
					className: "flex gap-2 border-b border-border-glass pb-4",
					children: [/* @__PURE__ */ h(e, {
						variant: "secondary",
						size: "sm",
						type: "button",
						onClick: A,
						children: "Accept all"
					}), /* @__PURE__ */ h(e, {
						variant: "ghost",
						size: "sm",
						type: "button",
						onClick: j,
						children: "Reject all"
					})]
				}),
				/* @__PURE__ */ h("div", {
					className: "space-y-4",
					role: "list",
					children: C ? Array.from({ length: 3 }).map((e, t) => /* @__PURE__ */ g("div", {
						className: "space-y-2",
						children: [/* @__PURE__ */ h(l, { className: "h-5 w-1/3" }), /* @__PURE__ */ h(l, { className: "h-4 w-full" })]
					}, t)) : x.length > 0 ? x.map((e) => /* @__PURE__ */ h(v, {
						category: e,
						value: D[e.id] ?? !1,
						onToggle: k
					}, e.id)) : null
				}),
				/* @__PURE__ */ h(a, { children: /* @__PURE__ */ h(e, {
					variant: "default",
					type: "button",
					onClick: () => void M(),
					loading: T,
					children: "Save preferences"
				}) })
			]
		})
	});
}
function v({ category: e, value: t, onToggle: n }) {
	let r = `consent-cat-${e.id}`;
	return /* @__PURE__ */ g("div", {
		role: "listitem",
		className: "flex flex-col gap-2 rounded-xl border border-border-glass p-4",
		children: [/* @__PURE__ */ g("div", {
			className: "flex items-center justify-between gap-3",
			children: [/* @__PURE__ */ g("div", { children: [/* @__PURE__ */ g("label", {
				htmlFor: r,
				className: "text-foreground text-sm font-medium",
				children: [e.name, e.required && /* @__PURE__ */ h("span", {
					className: "text-foreground-muted ml-2 text-xs font-normal",
					children: "(Required)"
				})]
			}), /* @__PURE__ */ h("p", {
				className: "text-foreground-muted mt-0.5 text-xs",
				children: e.description
			})] }), /* @__PURE__ */ h(c, {
				id: r,
				checked: e.required ? !0 : t,
				disabled: e.required,
				onCheckedChange: (t) => {
					n(e.id, t);
				},
				"aria-label": `${e.name} cookies`
			})]
		}), e.cookies.length > 0 && /* @__PURE__ */ g(u, {
			defaultOpen: !1,
			children: [/* @__PURE__ */ g(f, {
				className: "text-foreground-muted flex items-center gap-1 text-xs hover:text-foreground",
				children: [
					"View cookies (",
					e.cookies.length,
					")"
				]
			}), /* @__PURE__ */ h(d, { children: /* @__PURE__ */ h("div", {
				className: "mt-2 space-y-1.5",
				children: e.cookies.map((e) => /* @__PURE__ */ g("div", {
					className: "rounded-lg bg-glass px-3 py-2 text-xs",
					children: [
						/* @__PURE__ */ h("span", {
							className: "text-foreground font-medium",
							children: e.name
						}),
						e.provider && /* @__PURE__ */ g("span", {
							className: "text-foreground-muted ml-2",
							children: ["· ", e.provider]
						}),
						e.description && /* @__PURE__ */ h("p", {
							className: "text-foreground-muted mt-0.5",
							children: e.description
						})
					]
				}, e.name))
			}) })]
		})]
	});
}
//#endregion
export { _ as ConsentPreferences };

//# sourceMappingURL=preferences.js.map