import { ref } from 'vue'

export interface Toast {
  id: string
  type: 'success' | 'error' | 'info'
  title: string
  message?: string
}

const toasts = ref<Toast[]>([])

let counter = 0

export function useToast() {
  function show(type: Toast['type'], title: string, message?: string, duration = 5000) {
    const id = `toast-${++counter}`
    toasts.value.push({ id, type, title, message })
    if (duration > 0) {
      setTimeout(() => dismiss(id), duration)
    }
  }

  function dismiss(id: string) {
    const idx = toasts.value.findIndex(t => t.id === id)
    if (idx !== -1) toasts.value.splice(idx, 1)
  }

  return { toasts, show, dismiss }
}
