import '@testing-library/jest-dom'
import { afterEach, vi } from 'vitest'
import { cleanup } from '@testing-library/react'

// Cleanup after each test
afterEach(() => {
    cleanup()
})

// Mock window.matchMedia
Object.defineProperty(window, 'matchMedia', {
    writable: true,
    value: vi.fn().mockImplementation((query: string) => ({
        matches: false,
        media: query,
        onchange: null,
        addListener: vi.fn(),
        removeListener: vi.fn(),
        addEventListener: vi.fn(),
        removeEventListener: vi.fn(),
        dispatchEvent: vi.fn(),
    })),
})

// Mock ResizeObserver
class ResizeObserverMock {
    observe = vi.fn()
    unobserve = vi.fn()
    disconnect = vi.fn()
}
window.ResizeObserver = ResizeObserverMock

// Mock WebSocket
class WebSocketMock {
    static CONNECTING = 0
    static OPEN = 1
    static CLOSING = 2
    static CLOSED = 3

    readyState = WebSocketMock.OPEN
    url: string
    onopen: (() => void) | null = null
    onmessage: ((event: { data: string }) => void) | null = null
    onerror: ((error: Event) => void) | null = null
    onclose: (() => void) | null = null

    constructor(url: string) {
        this.url = url
        // Simulate connection
        setTimeout(() => this.onopen?.(), 0)
    }

    send = vi.fn()
    close = vi.fn(() => {
        this.readyState = WebSocketMock.CLOSED
        this.onclose?.()
    })
}
; (globalThis as unknown as { WebSocket: typeof WebSocketMock }).WebSocket = WebSocketMock

// Mock fetch
globalThis.fetch = vi.fn() as unknown as typeof fetch

// Suppress console errors in tests (optional, enable if needed)
// vi.spyOn(console, 'error').mockImplementation(() => {})
