import { describe, it, expect, beforeEach } from 'vitest'
import { useShortlistStore } from '../shortlistStore'
import type { ShortlistItemInput } from '../../domain/shortlist'

const item: ShortlistItemInput = {
  resultId: 'r-1',
  resultType: 'VERIFIED_PORTFOLIO',
  title: 'Bürofläche Teststrasse 1',
  matchScore: 85,
  addedBy: 'user-1',
}

describe('shortlistStore', () => {
  beforeEach(() => {
    useShortlistStore.setState({ selectedShortlistId: null, dialogOpen: false, pendingItem: null })
  })

  describe('setSelectedShortlist', () => {
    it('sets the selected id', () => {
      useShortlistStore.getState().setSelectedShortlist('sl-42')
      expect(useShortlistStore.getState().selectedShortlistId).toBe('sl-42')
    })

    it('clears the selected id with null', () => {
      useShortlistStore.getState().setSelectedShortlist('sl-42')
      useShortlistStore.getState().setSelectedShortlist(null)
      expect(useShortlistStore.getState().selectedShortlistId).toBeNull()
    })
  })

  describe('openAddDialog', () => {
    it('opens dialog and stores pending item', () => {
      useShortlistStore.getState().openAddDialog(item)
      const state = useShortlistStore.getState()
      expect(state.dialogOpen).toBe(true)
      expect(state.pendingItem).toEqual(item)
    })
  })

  describe('closeAddDialog', () => {
    it('closes dialog and clears pending item', () => {
      useShortlistStore.getState().openAddDialog(item)
      useShortlistStore.getState().closeAddDialog()
      const state = useShortlistStore.getState()
      expect(state.dialogOpen).toBe(false)
      expect(state.pendingItem).toBeNull()
    })
  })
})
