import { ref } from 'vue' import type { Toast, ToastOptions } from '../types' const toasts = ref([]) let nextId = 0 /** Reactive global toast queue with success/error/warning/info variants and auto-dismiss timers. */ export function useToast() { /** Push a structured toast. `progress: 'indeterminate'` is the in-progress form and never auto-dismisses. */ function push(options: ToastOptions): number { const id = nextId++ const duration = options.duration ?? 3500 toasts.value.push({ id, type: 'success', ...options, message: options.message ?? options.title, duration, actions: options.actions?.slice(0, 2), }) if (options.progress !== 'indeterminate') { setTimeout(() => dismiss(id), duration) } return id } function show(message: string, type: Toast['type'] = 'success', duration = 3500) { push({ title: message, type, duration }) } function success(message: string, duration = 3500) { show(message, 'success', duration) } function error(message: string, duration = 5000) { show(message, 'error', duration) } function warning(message: string, duration = 4000) { show(message, 'warning', duration) } function info(message: string, duration = 3500) { show(message, 'info', duration) } function dismiss(id: number) { toasts.value = toasts.value.filter(t => t.id !== id) } function clear() { toasts.value = [] } return { toasts, push, show, success, error, warning, info, dismiss, clear } }