import React from 'react';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { render, screen } from '@testing-library/react';

class IntersectionObserverMock {
  observe() {}
  unobserve() {}
  disconnect() {}
}

Object.defineProperty(globalThis, 'IntersectionObserver', {
  writable: true,
  value: IntersectionObserverMock,
});

beforeEach(() => {
  window.matchMedia = vi.fn().mockImplementation(() => ({
    matches: false,
    media: '',
    onchange: null,
    addListener: vi.fn(),
    removeListener: vi.fn(),
    addEventListener: vi.fn(),
    removeEventListener: vi.fn(),
    dispatchEvent: vi.fn(),
  }));
});

vi.mock('react-i18next', () => ({
  useTranslation: () => ({
    t: (key: string, fallback?: string | { defaultValue?: unknown }) => {
      if (typeof fallback === 'string') {
        return fallback;
      }
      if (fallback && typeof fallback === 'object' && 'defaultValue' in fallback) {
        return fallback.defaultValue;
      }
      return key;
    },
    i18n: { language: 'de' },
  }),
}));

vi.mock('@inertiajs/react', () => ({
  Head: () => null,
  Link: ({ children, href }: { children: React.ReactNode; href: string }) => <a href={href}>{children}</a>,
  usePage: () => ({ props: { supportedLocales: ['de', 'en'] } }),
}));

vi.mock('@/layouts/mainWebsite', () => ({
  default: ({ children, title }: { children: React.ReactNode; title: string }) => (
    <div>
      <h1>{title}</h1>
      {children}
    </div>
  ),
}));

vi.mock('@/hooks/useLocalizedRoutes', () => ({
  useLocalizedRoutes: () => ({
    localizedPath: (path: string) => path,
  }),
}));

import Blog from '../Blog';

describe('Blog', () => {
  it('renders the hero title', () => {
    render(
      <Blog
        posts={{
          data: [
            {
              id: 1,
              slug: 'hello',
              title: 'Hello',
              excerpt: 'Excerpt',
              published_at: '2024-01-01',
              author: 'Team',
            },
          ],
          links: [],
          current_page: 1,
          last_page: 1,
        }}
      />
    );

    expect(screen.getByText('blog.hero_title')).toBeInTheDocument();
  });
});
