import { createConsentManager as e, getDefaultConsentManager as t } from "../../consent/index.js";
import { useCallback as n, useMemo as r, useRef as i, useSyncExternalStore as a } from "react";
//#region src/react/hooks/useConsent.ts
var o = "cm:consent-changed";
function s() {
	window.dispatchEvent(new CustomEvent(o));
}
function c(c) {
	let l = r(() => c === void 0 ? t() : e(c), [c]), u = n((e) => (window.addEventListener(o, e), () => {
		window.removeEventListener(o, e);
	}), []), d = i({
		raw: "",
		value: null
	}), f = n(() => {
		let e = l.getStoredConsent(), t = e === null ? "" : JSON.stringify(e);
		return t !== d.current.raw && (d.current = {
			raw: t,
			value: e
		}), d.current.value;
	}, [l]);
	return {
		manager: l,
		preferences: a(u, f, f),
		setCategory: n((e, t) => {
			let n = l.getStoredConsent() ?? {
				essential: !0,
				performance: !1,
				analytics: !1,
				advertising: !1,
				social_media: !1
			}, r = {
				...n,
				[e]: t,
				essential: e === "essential" ? !0 : n.essential ?? !0
			};
			l.setStoredConsent(r), s();
		}, [l]),
		acceptAll: n(() => {
			let e = l.acceptAll();
			l.setStoredConsent(e), s();
		}, [l]),
		rejectAll: n(() => {
			let e = l.rejectAll();
			l.setStoredConsent(e), s();
		}, [l]),
		fetchCategories: n(() => l.fetchCategories(), [l]),
		saveConsent: n((e, t) => {
			let n = l.saveConsent(e, t);
			return s(), n;
		}, [l])
	};
}
//#endregion
export { c as useConsent };

//# sourceMappingURL=useConsent.js.map