import { act, renderHook } from '@testing-library/react-hooks';
import { TraceLog } from '@grafana/data';
import { useDetailState } from './useDetailState';

describe('useDetailState', () => {
  it('toggles detail', async () => {
    const { result } = renderHook(() => useDetailState());
    expect(result.current.detailStates.size).toBe(0);

    act(() => result.current.toggleDetail('span1'));
    expect(result.current.detailStates.size).toBe(1);
    expect(result.current.detailStates.has('span1')).toBe(true);

    act(() => result.current.toggleDetail('span1'));
    expect(result.current.detailStates.size).toBe(0);
  });

  it('toggles logs and logs items', async () => {
    const { result } = renderHook(() => useDetailState());
    act(() => result.current.toggleDetail('span1'));
    act(() => result.current.detailLogsToggle('span1'));
    expect(result.current.detailStates.get('span1')?.logs.isOpen).toBe(true);

    const log = { timestamp: 1 } as TraceLog;
    act(() => result.current.detailLogItemToggle('span1', log));
    expect(result.current.detailStates.get('span1')?.logs.openedItems.has(log)).toBe(true);
  });

  it('toggles warnings', async () => {
    const { result } = renderHook(() => useDetailState());
    act(() => result.current.toggleDetail('span1'));
    act(() => result.current.detailWarningsToggle('span1'));
    expect(result.current.detailStates.get('span1')?.isWarningsOpen).toBe(true);
  });

  it('toggles references', async () => {
    const { result } = renderHook(() => useDetailState());
    act(() => result.current.toggleDetail('span1'));
    act(() => result.current.detailReferencesToggle('span1'));
    expect(result.current.detailStates.get('span1')?.isReferencesOpen).toBe(true);
  });

  it('toggles processes', async () => {
    const { result } = renderHook(() => useDetailState());
    act(() => result.current.toggleDetail('span1'));
    act(() => result.current.detailProcessToggle('span1'));
    expect(result.current.detailStates.get('span1')?.isProcessOpen).toBe(true);
  });

  it('toggles tags', async () => {
    const { result } = renderHook(() => useDetailState());
    act(() => result.current.toggleDetail('span1'));
    act(() => result.current.detailTagsToggle('span1'));
    expect(result.current.detailStates.get('span1')?.isTagsOpen).toBe(true);
  });
});
