import { describe, it, expect, vi } from 'vitest'
import { fireEvent, render, screen } from '@testing-library/react'
import StarIcon from '@mui/icons-material/Star'
import { SelectionSummary } from './selection-summary'

describe('<SelectionSummary>', () => {
  it('renders nothing when total === 0 (no data to summarize)', () => {
    const { container } = render(<SelectionSummary count={0} total={0} />)
    expect(container.textContent).toBe('')
  })

  it('renders nothing when total is omitted (undefined)', () => {
    const { container } = render(<SelectionSummary count={0} />)
    expect(container.textContent).toBe('')
  })

  it('renders total / total when nothing is selected (count === 0)', () => {
    render(<SelectionSummary count={0} total={10} />)
    expect(screen.getByLabelText('10 / 10')).toBeTruthy()
    // No Clear button when nothing is selected.
    expect(screen.queryByText('Clear')).toBeNull()
  })

  it('renders count / total + Clear when count > 0', () => {
    const onClear = vi.fn()
    render(<SelectionSummary count={3} total={10} onClear={onClear} />)
    expect(screen.getByLabelText('3 / 10')).toBeTruthy()
    fireEvent.click(screen.getByText('Clear'))
    expect(onClear).toHaveBeenCalledTimes(1)
  })

  it('hides the Clear link when no onClear is provided', () => {
    render(<SelectionSummary count={3} total={10} />)
    expect(screen.queryByText('Clear')).toBeNull()
  })

  it('honors a custom summary renderer and clear label', () => {
    render(
      <SelectionSummary
        count={3}
        total={10}
        labels={{
          summary: (selected, total) => `${selected} of ${total} pinned`,
          clear: 'Reset',
        }}
        onClear={() => undefined}
      />,
    )
    expect(screen.getByText('3 of 10 pinned')).toBeTruthy()
    expect(screen.getByText('Reset')).toBeTruthy()
  })

  it('renders the custom icon component when provided', () => {
    const { container } = render(
      <SelectionSummary count={0} total={10} icon={StarIcon} />,
    )
    expect(container.querySelector('[data-testid="StarIcon"]')).toBeTruthy()
  })
})
