import { describe, it, expect, beforeEach } from 'vitest'
import { useToastStore } from '../toastStore'

describe('toastStore', () => {
  beforeEach(() => {
    useToastStore.setState({ toasts: [] })
  })

  describe('showToast', () => {
    it('adds a toast with message and default severity success', () => {
      useToastStore.getState().showToast('Gespeichert')
      const { toasts } = useToastStore.getState()
      expect(toasts).toHaveLength(1)
      expect(toasts[0].message).toBe('Gespeichert')
      expect(toasts[0].severity).toBe('success')
      expect(toasts[0].duration).toBe(4000)
    })

    it('accepts custom severity and duration', () => {
      useToastStore.getState().showToast('Fehler', 'error', 8000)
      const { toasts } = useToastStore.getState()
      expect(toasts[0].severity).toBe('error')
      expect(toasts[0].duration).toBe(8000)
    })

    it('assigns unique ids across multiple toasts', () => {
      useToastStore.getState().showToast('A')
      useToastStore.getState().showToast('B')
      const ids = useToastStore.getState().toasts.map(t => t.id)
      expect(new Set(ids).size).toBe(2)
    })

    it('stacks multiple toasts', () => {
      useToastStore.getState().showToast('First')
      useToastStore.getState().showToast('Second')
      expect(useToastStore.getState().toasts).toHaveLength(2)
    })
  })

  describe('dismissToast', () => {
    it('removes the toast with the given id', () => {
      useToastStore.getState().showToast('Keep')
      useToastStore.getState().showToast('Remove')
      const { toasts } = useToastStore.getState()
      const idToRemove = toasts[1].id
      useToastStore.getState().dismissToast(idToRemove)
      expect(useToastStore.getState().toasts).toHaveLength(1)
      expect(useToastStore.getState().toasts[0].message).toBe('Keep')
    })

    it('is a no-op for unknown id', () => {
      useToastStore.getState().showToast('Keep')
      useToastStore.getState().dismissToast('nonexistent-id')
      expect(useToastStore.getState().toasts).toHaveLength(1)
    })
  })
})
