import { renderHook } from '@testing-library/react';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { useMapLayers } from './map-layers';

describe('useMapLayers', () => {
  const mockMap = {
    setMap: vi.fn(),
  } as any;

  const mockTrafficLayer = { setMap: vi.fn() };
  const mockTransitLayer = { setMap: vi.fn() };
  const mockBicyclingLayer = { setMap: vi.fn() };

  beforeEach(() => {
    vi.clearAllMocks();
    global.google = {
      maps: {
        TrafficLayer: vi.fn(function () {
          return mockTrafficLayer;
        }),
        TransitLayer: vi.fn(function () {
          return mockTransitLayer;
        }),
        BicyclingLayer: vi.fn(function () {
          return mockBicyclingLayer;
        }),
      },
    } as any;
  });

  it('initializes and sets traffic layer when enabled', () => {
    renderHook(({ layers }) => useMapLayers(mockMap, layers), {
      initialProps: { layers: { traffic: true } },
    });

    expect(global.google.maps.TrafficLayer).toHaveBeenCalled();
    expect(mockTrafficLayer.setMap).toHaveBeenCalledWith(mockMap);
  });

  it('removes traffic layer when disabled', () => {
    const { rerender } = renderHook(({ layers }) => useMapLayers(mockMap, layers), {
      initialProps: { layers: { traffic: true } },
    });

    rerender({ layers: { traffic: false } });
    expect(mockTrafficLayer.setMap).toHaveBeenCalledWith(null);
  });
});
