import { generateToastId, runToastPromise } from "./createToastManager.js"; import { reactive, shallowRef } from "vue"; //#region src/Toast/useToastStore.ts function useToastStore() { const toasts = shallowRef([]); const restartKeys = /*#__PURE__*/ reactive(/* @__PURE__ */ new Map()); function find(id) { return toasts.value.find((toast) => toast.id === id); } function replace(id, next) { toasts.value = toasts.value.map((toast) => toast.id === id ? next : toast); } function remove(id) { const toast = find(id); if (!toast || toast.open) return; toasts.value = toasts.value.filter((item) => item.id !== id); restartKeys.delete(id); toast.onRemove?.(); } function add(options) { const id = options.id ?? generateToastId(); const existing = find(id); if (existing?.open) { const { id: _id,...updates } = options; replace(id, { ...existing, ...updates, updateKey: existing.updateKey + 1 }); restartKeys.set(id, (restartKeys.get(id) ?? 0) + 1); return id; } const rest = existing ? toasts.value.filter((toast) => toast.id !== id) : toasts.value; toasts.value = [{ ...options, id, open: true, updateKey: 0 }, ...rest]; return id; } function close(id) { const closing = toasts.value.filter((toast) => toast.open && (id === void 0 || toast.id === id)); if (!closing.length) return; toasts.value = toasts.value.map((toast) => closing.includes(toast) ? { ...toast, open: false } : toast); closing.forEach((toast) => toast.onClose?.()); } function update(id, updates) { const prevToast = find(id); if (!prevToast?.open) return; const patch = typeof updates === "function" ? updates(prevToast) : updates; const current = find(id); if (!current?.open) return; replace(id, { ...current, ...patch, id, open: true, updateKey: current.updateKey + 1 }); } return { toasts, add, close, update, promise: (promise, options) => runToastPromise(promise, options, add, update), remove, getRestartKey: (id) => restartKeys.get(id) ?? 0 }; } //#endregion export { useToastStore }; //# sourceMappingURL=useToastStore.js.map