import '@testing-library/jest-dom';
import { vi, beforeEach, afterEach, beforeAll } from 'vitest';
import i18n from '@/i18n';

// Mock matchMedia for jsdom
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 IntersectionObserver
class MockIntersectionObserver {
  observe = vi.fn();
  unobserve = vi.fn();
  disconnect = vi.fn();
  takeRecords = vi.fn();
  root = null;
  rootMargin = '';
  thresholds = [];
}

Object.defineProperty(window, 'IntersectionObserver', {
  writable: true,
  configurable: true,
  value: MockIntersectionObserver,
});

// Mock ResizeObserver
class MockResizeObserver {
  observe = vi.fn();
  unobserve = vi.fn();
  disconnect = vi.fn();
}

Object.defineProperty(window, 'ResizeObserver', {
  writable: true,
  configurable: true,
  value: MockResizeObserver,
});


// Mock EventSource for jsdom
class MockEventSource {
  constructor(url: string) {
    this.url = url;
    this.readyState = 0;
  }
  url: string;
  readyState: number;
  onopen: any = null;
  onmessage: any = null;
  onerror: any = null;
  addEventListener = vi.fn();
  removeEventListener = vi.fn();
  close = vi.fn();
}
Object.defineProperty(window, 'EventSource', { writable: true, configurable: true, value: MockEventSource });

// Mock scrollTo
window.scrollTo = vi.fn() as any;


// Initialize i18n before all tests
beforeAll(async () => {
  await i18n.changeLanguage('de');
});

// Clean up after each test
afterEach(() => {
  vi.clearAllMocks();
  localStorage.clear();
});

beforeEach(() => {
  localStorage.clear();
});
