import { describe, it, expect, beforeEach } from 'vitest'
import { dialogState, showAlert, showConfirm, showPrompt, closeDialog } from '../composables/useDialog'

// Reset dialog state before each test
beforeEach(() => {
  dialogState.visible = false
  dialogState.type = 'alert'
  dialogState.message = ''
  dialogState.defaultValue = ''
  dialogState.resolve = null
})

describe('Dialog System', () => {
  it('starts hidden', () => {
    expect(dialogState.visible).toBe(false)
  })

  it('showAlert opens alert dialog', async () => {
    const promise = showAlert('test message')
    expect(dialogState.visible).toBe(true)
    expect(dialogState.type).toBe('alert')
    expect(dialogState.message).toBe('test message')
    closeDialog()
    await promise
    expect(dialogState.visible).toBe(false)
  })

  it('showAlert resolves when closed', async () => {
    const promise = showAlert('hello')
    closeDialog()
    const result = await promise
    expect(result).toBeUndefined()
  })

  it('showConfirm returns true on ok', async () => {
    const promise = showConfirm('are you sure?')
    expect(dialogState.type).toBe('confirm')
    expect(dialogState.message).toBe('are you sure?')
    closeDialog(true)
    const result = await promise
    expect(result).toBe(true)
  })

  it('showConfirm returns false on cancel', async () => {
    const promise = showConfirm('are you sure?')
    closeDialog(false)
    const result = await promise
    expect(result).toBe(false)
  })

  it('showPrompt opens prompt dialog with default value', async () => {
    const promise = showPrompt('enter name', 'default')
    expect(dialogState.visible).toBe(true)
    expect(dialogState.type).toBe('prompt')
    expect(dialogState.message).toBe('enter name')
    expect(dialogState.defaultValue).toBe('default')
    closeDialog('hello')
    await promise
  })

  it('showPrompt returns input value', async () => {
    const promise = showPrompt('enter name', 'default')
    closeDialog('hello')
    const result = await promise
    expect(result).toBe('hello')
  })

  it('showPrompt returns null on cancel', async () => {
    const promise = showPrompt('enter name')
    closeDialog(null)
    const result = await promise
    expect(result).toBeNull()
  })

  it('showPrompt defaults defaultValue to empty string', async () => {
    const promise = showPrompt('enter name')
    expect(dialogState.defaultValue).toBe('')
    closeDialog('test')
    await promise
  })

  it('closeDialog sets visible to false', async () => {
    showAlert('msg')
    expect(dialogState.visible).toBe(true)
    closeDialog()
    expect(dialogState.visible).toBe(false)
  })

  it('closeDialog clears resolve after calling it', async () => {
    const promise = showAlert('msg')
    closeDialog()
    await promise
    expect(dialogState.resolve).toBeNull()
  })

  it('multiple sequential dialogs work correctly', async () => {
    // First dialog
    const p1 = showAlert('first')
    expect(dialogState.message).toBe('first')
    closeDialog()
    await p1

    // Second dialog
    const p2 = showConfirm('second')
    expect(dialogState.message).toBe('second')
    expect(dialogState.type).toBe('confirm')
    closeDialog(true)
    const r2 = await p2
    expect(r2).toBe(true)

    // Third dialog
    const p3 = showPrompt('third', 'val')
    expect(dialogState.type).toBe('prompt')
    closeDialog('answer')
    const r3 = await p3
    expect(r3).toBe('answer')
  })
})
