import { t as e } from "../../storage-B-gK537W.js";
import { useCallback as t, useMemo as n, useRef as r, useSyncExternalStore as i } from "react";
//#region src/react/hooks/useStorage.ts
function a(a, o) {
	let { defaultValue: s, namespace: c } = o ?? {}, l = n(() => new e(c === void 0 ? void 0 : { prefix: c }), [c]), u = r({
		serialised: "",
		value: s
	}), d = t(() => {
		let e = l.get(a), t = e === null ? s : e, n = JSON.stringify(t);
		return n !== u.current.serialised && (u.current = {
			serialised: n,
			value: t
		}), u.current.value;
	}, [
		l,
		a,
		s
	]);
	return [
		i(t((e) => {
			let t = (t) => {
				(t.storageArea === localStorage || t.storageArea === null) && e();
			}, n = () => {
				e();
			};
			return window.addEventListener("storage", t), window.addEventListener("cm:storage-change", n), () => {
				window.removeEventListener("storage", t), window.removeEventListener("cm:storage-change", n);
			};
		}, []), d, d),
		t((e) => {
			l.set(a, e), window.dispatchEvent(new CustomEvent("cm:storage-change"));
		}, [l, a]),
		t(() => {
			l.remove(a), window.dispatchEvent(new CustomEvent("cm:storage-change"));
		}, [l, a])
	];
}
//#endregion
export { a as useStorage };

//# sourceMappingURL=useStorage.js.map