// @vitest-environment jsdom
import { describe, it, expect, vi } from 'vitest'
import { renderHook, act } from '@testing-library/react'
import { useDirtyCloseGuard } from '@/components/shared/use-dirty-close-guard'

describe('useDirtyCloseGuard', () => {
  it('sluit direct als form niet dirty is', () => {
    const onClose = vi.fn()
    const { result } = renderHook(() => useDirtyCloseGuard(false, onClose))

    act(() => result.current.attemptClose())

    expect(onClose).toHaveBeenCalledTimes(1)
    expect(result.current.confirmOpen).toBe(false)
  })

  it('opent confirm als form dirty is', () => {
    const onClose = vi.fn()
    const { result } = renderHook(() => useDirtyCloseGuard(true, onClose))

    act(() => result.current.attemptClose())

    expect(onClose).not.toHaveBeenCalled()
    expect(result.current.confirmOpen).toBe(true)
  })

  it('confirmDiscard sluit confirm en roept onClose', () => {
    const onClose = vi.fn()
    const { result } = renderHook(() => useDirtyCloseGuard(true, onClose))

    act(() => result.current.attemptClose())
    expect(result.current.confirmOpen).toBe(true)

    act(() => result.current.confirmDiscard())

    expect(onClose).toHaveBeenCalledTimes(1)
    expect(result.current.confirmOpen).toBe(false)
  })

  it('setConfirmOpen(false) annuleert zonder onClose te roepen', () => {
    const onClose = vi.fn()
    const { result } = renderHook(() => useDirtyCloseGuard(true, onClose))

    act(() => result.current.attemptClose())
    act(() => result.current.setConfirmOpen(false))

    expect(onClose).not.toHaveBeenCalled()
    expect(result.current.confirmOpen).toBe(false)
  })
})
