/*! © 2026 Yahoo, Inc. UDS v0.0.0-development */
"use client";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
//#region src/components/client/Modal/modalStore.ts
let nextSlotId = 0;
let nextOwnerId = 0;
let slot = null;
const subscribers = /* @__PURE__ */ new Set();
const notify = () => {
	for (const fn of subscribers) fn();
};
const subscribe = (fn) => {
	subscribers.add(fn);
	return () => {
		subscribers.delete(fn);
	};
};
const getSnapshot = () => slot;
const splitArgs = (args) => {
	const { actionStartContent, replace, ...rest } = args;
	return {
		props: rest,
		options: {
			actionStartContent,
			replace
		}
	};
};
const errorIfNoPortalMounted = () => {
	if (process.env.NODE_ENV === "production") return;
	if (subscribers.size === 0) console.error("[UDS] createModal was called but no <ModalPortal /> is mounted in this React tree. Render <ModalPortal /> inside your UDSConfigProvider for the modal to display. Note: ModalPortal must be in the same React tree (and the same @yahoo/uds module instance) as the createModal caller — multi-tenant apps with isolated React trees need their own ModalPortal in each tree.");
};
const scheduleOpen = (slotId, ownerId) => {
	if (typeof window === "undefined") return;
	window.requestAnimationFrame(() => {
		window.requestAnimationFrame(() => {
			if (!slot || slot.slotId !== slotId || slot.ownerId !== ownerId || slot.closing) return;
			slot = {
				...slot,
				open: true
			};
			notify();
		});
	});
};
const addModalEntry = (args) => {
	errorIfNoPortalMounted();
	nextOwnerId += 1;
	const ownerId = nextOwnerId;
	const { props, options } = splitArgs(args);
	const newEntry = {
		ownerId,
		props,
		options
	};
	if (!slot) {
		nextSlotId += 1;
		const slotId = nextSlotId;
		slot = {
			slotId,
			ownerId,
			open: false,
			closing: false,
			props,
			options,
			stack: [],
			pending: null,
			preserveOnExit: false
		};
		notify();
		scheduleOpen(slotId, ownerId);
		return ownerId;
	}
	if (options.replace === true) {
		slot = {
			...slot,
			open: false,
			closing: true,
			stack: [],
			pending: newEntry,
			preserveOnExit: false
		};
		notify();
		return ownerId;
	}
	if (slot.closing) {
		slot = {
			...slot,
			pending: newEntry
		};
		notify();
		return ownerId;
	}
	slot = {
		...slot,
		open: false,
		closing: true,
		pending: newEntry,
		preserveOnExit: true
	};
	notify();
	return ownerId;
};
const updateModalEntry = (ownerId, next) => {
	if (!slot) return;
	const { props: nextProps, options: nextOptions } = splitArgs(next);
	if (slot.ownerId === ownerId) {
		slot = {
			...slot,
			props: {
				...slot.props,
				...nextProps
			},
			options: {
				...slot.options,
				...nextOptions
			}
		};
		notify();
		return;
	}
	if (slot.pending?.ownerId === ownerId) {
		const updatedPending = {
			ownerId: slot.pending.ownerId,
			props: {
				...slot.pending.props,
				...nextProps
			},
			options: {
				...slot.pending.options,
				...nextOptions
			}
		};
		slot = {
			...slot,
			pending: updatedPending
		};
		notify();
		return;
	}
	const buriedIndex = slot.stack.findIndex((entry) => entry.ownerId === ownerId);
	if (buriedIndex === -1) return;
	const buried = slot.stack[buriedIndex];
	if (!buried) return;
	const updatedEntry = {
		ownerId: buried.ownerId,
		props: {
			...buried.props,
			...nextProps
		},
		options: {
			...buried.options,
			...nextOptions
		}
	};
	const nextStack = slot.stack.slice();
	nextStack[buriedIndex] = updatedEntry;
	slot = {
		...slot,
		stack: nextStack
	};
	notify();
};
const closeModalEntry = (ownerId) => {
	if (!slot) return;
	if (slot.ownerId === ownerId) {
		slot = {
			...slot,
			open: false,
			closing: true,
			preserveOnExit: false
		};
		notify();
		return;
	}
	if (slot.pending?.ownerId === ownerId) {
		slot = {
			...slot,
			pending: null
		};
		notify();
		return;
	}
	const buriedIndex = slot.stack.findIndex((entry) => entry.ownerId === ownerId);
	if (buriedIndex === -1) return;
	const nextStack = slot.stack.slice();
	nextStack.splice(buriedIndex, 1);
	slot = {
		...slot,
		stack: nextStack
	};
	notify();
};
const removeModalEntry = (ownerId) => {
	if (!slot || slot.ownerId !== ownerId) return;
	if (slot.pending) {
		const nextStack = slot.preserveOnExit ? [...slot.stack, {
			ownerId: slot.ownerId,
			props: slot.props,
			options: slot.options
		}] : slot.stack;
		nextSlotId += 1;
		const slotId = nextSlotId;
		const promoted = slot.pending;
		slot = {
			slotId,
			ownerId: promoted.ownerId,
			open: false,
			closing: false,
			props: promoted.props,
			options: promoted.options,
			stack: nextStack,
			pending: null,
			preserveOnExit: false
		};
		notify();
		scheduleOpen(slotId, promoted.ownerId);
		return;
	}
	if (slot.stack.length === 0) {
		slot = null;
		notify();
		return;
	}
	const restored = slot.stack[slot.stack.length - 1];
	if (!restored) {
		slot = null;
		notify();
		return;
	}
	const restoredStack = slot.stack.slice(0, -1);
	nextSlotId += 1;
	const slotId = nextSlotId;
	slot = {
		slotId,
		ownerId: restored.ownerId,
		open: false,
		closing: false,
		props: restored.props,
		options: restored.options,
		stack: restoredStack,
		pending: null,
		preserveOnExit: false
	};
	notify();
	scheduleOpen(slotId, restored.ownerId);
};
//#endregion
exports.addModalEntry = addModalEntry;
exports.closeModalEntry = closeModalEntry;
exports.getSnapshot = getSnapshot;
exports.removeModalEntry = removeModalEntry;
exports.subscribe = subscribe;
exports.updateModalEntry = updateModalEntry;
