import { useSyncExternalStore } from "react";

//#region src/hooks/useScrollBlockage/useScrollBlockageStore.ts
var ScrollBlockageObservable = class {
	listeners = /* @__PURE__ */ new Set();
	scrollBlockers = [];
	subscribe = (listener) => {
		this.listeners.add(listener);
		return () => {
			this.listeners.delete(listener);
		};
	};
	getSnapshot = () => {
		return this.scrollBlockers;
	};
	addBlockage = (blockerName, element) => {
		const blockersElement = this.scrollBlockers.find((blocker) => blocker.element === element);
		if (blockersElement) {
			if (blockersElement.blockers.includes(blockerName)) return;
			this.scrollBlockers = this.scrollBlockers.map((blocker) => blocker.element === element ? {
				...blocker,
				blockers: [...blocker.blockers, blockerName]
			} : blocker);
		} else {
			element.style.overflowY = "hidden";
			element.style.overflowX = "hidden";
			this.scrollBlockers = [...this.scrollBlockers, {
				element,
				blockers: [blockerName]
			}];
		}
		this.emit();
	};
	removeBlockage = (blockerName, element) => {
		const blocker = this.scrollBlockers.find((blocker) => blocker.element === element);
		if (blocker) {
			if (blocker.blockers.length > 1) this.scrollBlockers = this.scrollBlockers.map((b) => b.element === element ? {
				...b,
				blockers: b.blockers.filter((name) => name !== blockerName)
			} : b);
			else {
				element.style.overflowY = "";
				element.style.overflowX = "";
				this.scrollBlockers = this.scrollBlockers.filter((b) => b.element !== element);
			}
			this.emit();
		}
	};
	isElementScrollBlocked = (element) => {
		return this.scrollBlockers.some((blocker) => blocker.element === element);
	};
	emit = () => {
		this.listeners.forEach((listener) => {
			listener();
		});
	};
};
const scrollBlockageManager = new ScrollBlockageObservable();
const serverSnapshot = [];
const useScrollBlockageStore = () => {
	return {
		scrollBlockers: useSyncExternalStore(scrollBlockageManager.subscribe, scrollBlockageManager.getSnapshot, () => serverSnapshot),
		addBlockage: scrollBlockageManager.addBlockage,
		removeBlockage: scrollBlockageManager.removeBlockage,
		isElementScrollBlocked: scrollBlockageManager.isElementScrollBlocked
	};
};

//#endregion
export { scrollBlockageManager, useScrollBlockageStore };
//# sourceMappingURL=useScrollBlockageStore.mjs.map