import { describe, it, expect } from 'vitest'
import { useConfirm } from '~/composables/useConfirm'

describe('useConfirm', () => {
  it('returns confirm function and state', () => {
    const { confirm, confirmState, handleConfirm, handleCancel } = useConfirm()
    expect(typeof confirm).toBe('function')
    expect(typeof handleConfirm).toBe('function')
    expect(typeof handleCancel).toBe('function')
    expect(confirmState.open).toBe(false)
  })

  it('opens modal with correct options', () => {
    const { confirm, confirmState } = useConfirm()
    // Don't await — we'll manually resolve
    confirm({ message: 'Delete this item?' })
    expect(confirmState.open).toBe(true)
    expect(confirmState.message).toBe('Delete this item?')
    expect(confirmState.title).toBe('Confirmation')
    expect(confirmState.confirmText).toBe('Supprimer')
    expect(confirmState.cancelText).toBe('Annuler')
    expect(confirmState.dangerous).toBe(true)
    // Clean up by canceling
    const { handleCancel } = useConfirm()
    handleCancel()
  })

  it('resolves true on confirm', async () => {
    const { confirm, handleConfirm } = useConfirm()
    const promise = confirm({ message: 'Confirm?' })
    handleConfirm()
    const result = await promise
    expect(result).toBe(true)
  })

  it('resolves false on cancel', async () => {
    const { confirm, handleCancel } = useConfirm()
    const promise = confirm({ message: 'Cancel?' })
    handleCancel()
    const result = await promise
    expect(result).toBe(false)
  })

  it('closes modal after confirm', async () => {
    const { confirm, confirmState, handleConfirm } = useConfirm()
    confirm({ message: 'Test' })
    expect(confirmState.open).toBe(true)
    handleConfirm()
    expect(confirmState.open).toBe(false)
  })

  it('closes modal after cancel', async () => {
    const { confirm, confirmState, handleCancel } = useConfirm()
    confirm({ message: 'Test' })
    expect(confirmState.open).toBe(true)
    handleCancel()
    expect(confirmState.open).toBe(false)
  })

  it('supports custom options', () => {
    const { confirm, confirmState, handleCancel } = useConfirm()
    confirm({
      title: 'Custom Title',
      message: 'Custom message',
      confirmText: 'Yes',
      cancelText: 'No',
      dangerous: false,
    })
    expect(confirmState.title).toBe('Custom Title')
    expect(confirmState.confirmText).toBe('Yes')
    expect(confirmState.cancelText).toBe('No')
    expect(confirmState.dangerous).toBe(false)
    handleCancel()
  })

  it('shares state across calls (singleton)', () => {
    const a = useConfirm()
    const b = useConfirm()
    expect(a.confirmState).toBe(b.confirmState)
  })
})
