/* eslint-disable @cspell/spellchecker */
import { cleanup } from '@testing-library/react';
import { afterEach, vi } from 'vitest';
import '@testing-library/jest-dom/vitest';
import '@testing-library/jest-dom';

/* msw */
let server: any;

export const initializeServer = async () => {
  if (typeof window === 'undefined') {
    /*
    const { handlers } = await import('~/__mocks__/handlers');
    const { mockData } = await import('~/__mocks__/response');
    const { setUpMockServer } = await import('~/__mocks__/server');

    server = await setUpMockServer(handlers(mockData));
    server.listen();
    */
  }
};

beforeAll(() => {
  // vi.stubGlobal('document', {});
  console.log('beforeAll');

  server.listen();
});

beforeEach(() => {});

afterEach(() => {
  server.resetHandlers();
  vi.clearAllMocks();
  cleanup();
});

afterAll(() => {
  vi.resetAllMocks();
  server.close();
});

// vi.mock('@sentry/react');
vi.mock('zustand');

vi.mock('~/shared/lib/hooks', () => ({
  useNavigate: () => ({
    navigate: vi.fn(),
    navigateBack: vi.fn(),
    reload: vi.fn(),
  }),
}));
// https://github.com/vitest-dev/vitest/issues/821
Object.defineProperty(window, 'matchMedia', {
  writable: true,
  value: vi.fn().mockImplementation((query) => ({
    matches: false,
    media: query,
    onchange: null,
    addListener: vi.fn(), // deprecated
    removeListener: vi.fn(), // deprecated
    addEventListener: vi.fn(),
    removeEventListener: vi.fn(),
    dispatchEvent: vi.fn(),
  })),
});

// Mocking window.localStorage
Object.defineProperty(window, 'localStorage', {
  writable: true,
  value: {
    getItem: vi.fn(),
    setItem: vi.fn(),
    removeItem: vi.fn(),
    clear: vi.fn(),
  },
});
