import { createSlice, PayloadAction } from '@reduxjs/toolkit' export type ToastType = 'success' | 'error' | 'warning' | 'info' export type ToastPosition = | 'top-right' | 'top-left' | 'top-center' | 'bottom-right' | 'bottom-left' | 'bottom-center' export interface Toast { id: string type: ToastType message: string duration?: number position?: ToastPosition icon?: string className?: string dismissible?: boolean action?: { label: string actionId: string } } export type ToastInput = Omit export interface ToastState { toasts: Toast[] } const MAX_TOASTS = 5 const initialState: ToastState = { toasts: [] } const toastSlice = createSlice({ name: 'toast', initialState, reducers: { addToast: { reducer: (state, action: PayloadAction) => { state.toasts.push(action.payload) if (state.toasts.length > MAX_TOASTS) { state.toasts.shift() } }, prepare: (toast: ToastInput) => ({ payload: { ...toast, id: `toast-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`, duration: toast.duration ?? 4000, position: toast.position ?? 'top-right', dismissible: toast.dismissible ?? true } }) }, removeToast: (state, action: PayloadAction) => { const idx = state.toasts.findIndex((t) => t.id === action.payload) if (idx !== -1) state.toasts.splice(idx, 1) } } }) export const { addToast, removeToast } = toastSlice.actions export default toastSlice.reducer