import { act, renderHook } from '@testing-library/react-hooks';
import type { Mock } from 'vitest';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { Data } from '../../../types';
import { FilterType, FilterValues } from '../../Filters';
import { getState, storeState } from '../../Utils/State/GlobalState';
import { useFilters } from './useFilters';

// vi.mock('../../../hooks', () => ({
//   useExpand: vi.fn(),
// }));

vi.mock('../../Utils/State/GlobalState', () => ({
  getState: vi.fn(),
  storeState: vi.fn(),
}));

describe('useFilters', () => {
  const stationKey = 'testStation';
  const globalStateOptions = { filters: true };
  const filterOptions: FilterType<Data>[] = [];
  const defaultFilterValues = { filter1: 'value1' };
  const onFiltersChange = vi.fn();

  beforeEach(() => {
    // (useExpand as Mock).mockReturnValue({
    //   isExpanded: true,
    //   toggleExpanded: vi.fn(),
    //   collapse: vi.fn(),
    // });
    (getState as Mock).mockReturnValue(undefined);
    vi.clearAllMocks();
  });

  it('should initialize with default filter values', () => {
    const { result } = renderHook(() =>
      useFilters({
        stationKey,
        globalStateOptions,
        filterOptions,
        defaultFilterValues,
        onFiltersChange,
      }),
    );

    expect(result.current.activeFilters).toEqual(defaultFilterValues);
  });

  it('should toggle filter visibility', () => {
    const { result } = renderHook(() =>
      useFilters({
        stationKey,
        globalStateOptions,
        filterOptions,
        defaultFilterValues,
        onFiltersChange,
      }),
    );

    act(() => {
      result.current.toggleFiltersVisible();
    });

    expect(result.current.filtersVisible).toBe(false);
  });

  it('should collapse filters', () => {
    const { result } = renderHook(() =>
      useFilters({
        stationKey,
        globalStateOptions,
        filterOptions,
        defaultFilterValues,
        onFiltersChange,
      }),
    );

    act(() => {
      result.current.collapseFilters();
    });

    expect(result.current.filtersVisible).toBe(false);
  });

  it('should update filters and call onFiltersChange', () => {
    const newFilters: FilterValues<any> = { filter1: 'newValue' };
    const { result } = renderHook(() =>
      useFilters({
        stationKey,
        globalStateOptions,
        filterOptions,
        defaultFilterValues,
        onFiltersChange,
      }),
    );

    act(() => {
      result.current.Filters.props.children.props.onFiltersChange(newFilters);
    });

    expect(result.current.activeFilters).toEqual(newFilters);
    expect(onFiltersChange).toHaveBeenCalledWith(newFilters);
  });

  it('should store state when filters change', () => {
    const newFilters: FilterValues<any> = { filter1: 'newValue' };
    const { result } = renderHook(() =>
      useFilters({
        stationKey,
        globalStateOptions,
        filterOptions,
        defaultFilterValues,
        onFiltersChange,
      }),
    );

    act(() => {
      result.current.Filters.props.children.props.onFiltersChange(newFilters);
    });

    expect(storeState).toHaveBeenCalledWith(stationKey, 'filters', newFilters);
  });

  it('should clear state when filters are empty', () => {
    const emptyFilters: FilterValues<any> = {};
    const { result } = renderHook(() =>
      useFilters({
        stationKey,
        globalStateOptions,
        filterOptions,
        defaultFilterValues,
        onFiltersChange,
      }),
    );

    act(() => {
      result.current.Filters.props.children.props.onFiltersChange(emptyFilters);
    });

    expect(storeState).toHaveBeenCalledWith(stationKey, 'filters', undefined);
  });
});
