/**
 * Tests for LassoToolsUIChip — covers both visible/invisible branches,
 * custom label overrides, custom chip/tooltip props, and the delete +
 * click callbacks.
 */
import { describe, it, expect, vi } from 'vitest'
import { render, fireEvent, screen } from '@testing-library/react'
import { LassoToolsUIChip } from './chip'
import type { LassoToolsData } from './types'

const baseValue: LassoToolsData = {
  id: 'p1',
  label: 'Polygon 1',
  visible: true,
}

describe('LassoToolsUIChip', () => {
  it('renders with active state when value.visible=true (color=secondary)', () => {
    const { container } = render(
      <LassoToolsUIChip
        value={baseValue}
        onChipToggle={vi.fn()}
        onDelete={vi.fn()}
      />,
    )
    // MUI Chip with color=secondary has a class containing 'colorSecondary'.
    const chip = container.querySelector('.MuiChip-root')
    expect(chip).not.toBeNull()
    expect(chip!.className).toContain('colorSecondary')
  })

  it('renders with inactive state when value.visible=false (color=default)', () => {
    const { container } = render(
      <LassoToolsUIChip
        value={{ ...baseValue, visible: false }}
        onChipToggle={vi.fn()}
        onDelete={vi.fn()}
      />,
    )
    const chip = container.querySelector('.MuiChip-root')
    expect(chip).not.toBeNull()
    expect(chip!.className).not.toContain('colorSecondary')
  })

  it('renders the value.label as the chip text', () => {
    render(
      <LassoToolsUIChip
        value={{ ...baseValue, label: 'My polygon' }}
        onChipToggle={vi.fn()}
        onDelete={vi.fn()}
      />,
    )
    expect(screen.getByText('My polygon')).toBeDefined()
  })

  it('invokes onChipToggle with id + !visible when chip is clicked', () => {
    const onChipToggle = vi.fn()
    render(
      <LassoToolsUIChip
        value={{ ...baseValue, visible: true }}
        onChipToggle={onChipToggle}
        onDelete={vi.fn()}
      />,
    )
    fireEvent.click(screen.getByText('Polygon 1'))
    expect(onChipToggle).toHaveBeenCalledWith('p1', false)
  })

  it('invokes onChipToggle with id + true when starting from invisible', () => {
    const onChipToggle = vi.fn()
    render(
      <LassoToolsUIChip
        value={{ ...baseValue, visible: false }}
        onChipToggle={onChipToggle}
        onDelete={vi.fn()}
      />,
    )
    fireEvent.click(screen.getByText('Polygon 1'))
    expect(onChipToggle).toHaveBeenCalledWith('p1', true)
  })

  it('invokes onDelete with id when delete icon is clicked', () => {
    const onDelete = vi.fn()
    const { getByTestId } = render(
      <LassoToolsUIChip
        value={baseValue}
        onChipToggle={vi.fn()}
        onDelete={onDelete}
      />,
    )
    // MUI Chip delete icon has data-testid='CancelIcon' by default.
    fireEvent.click(getByTestId('CancelIcon'))
    expect(onDelete).toHaveBeenCalledWith('p1')
  })

  it('uses custom labels.tooltip.active when provided + visible=true', () => {
    // The tooltip is rendered lazily on hover; we just confirm the chip
    // renders without throwing when a custom label is supplied.
    expect(() =>
      render(
        <LassoToolsUIChip
          value={{ ...baseValue, visible: true }}
          labels={{
            tooltip: { active: 'Hide me', inactive: 'Show me' },
          }}
          onChipToggle={vi.fn()}
          onDelete={vi.fn()}
        />,
      ),
    ).not.toThrow()
  })

  it('uses custom labels.tooltip.inactive when provided + visible=false', () => {
    expect(() =>
      render(
        <LassoToolsUIChip
          value={{ ...baseValue, visible: false }}
          labels={{
            tooltip: { active: 'Hide me', inactive: 'Show me' },
          }}
          onChipToggle={vi.fn()}
          onDelete={vi.fn()}
        />,
      ),
    ).not.toThrow()
  })

  it('falls back to default labels when labels is omitted', () => {
    // Just renders without crashing; the labels?. chain falls back to
    // LASSO_TOOLS_LABELS.chip.tooltip[chipState].
    expect(() =>
      render(
        <LassoToolsUIChip
          value={baseValue}
          onChipToggle={vi.fn()}
          onDelete={vi.fn()}
        />,
      ),
    ).not.toThrow()
  })

  it('threads chipProps.ChipsProps onto the underlying MUI Chip', () => {
    const { container } = render(
      <LassoToolsUIChip
        value={baseValue}
        chipProps={
          {
            ChipsProps: { 'data-testid': 'pinned-chip' } as never,
          } as never
        }
        onChipToggle={vi.fn()}
        onDelete={vi.fn()}
      />,
    )
    expect(
      container.querySelector('[data-testid="pinned-chip"]'),
    ).not.toBeNull()
  })

  it('threads chipProps.TooltipProps onto the wrapping Tooltip', () => {
    // The Tooltip wrapper isn't directly assertable without rendering it
    // open; we confirm no throw on the spread.
    expect(() =>
      render(
        <LassoToolsUIChip
          value={baseValue}
          chipProps={
            { TooltipProps: { placement: 'left' as const } as never } as never
          }
          onChipToggle={vi.fn()}
          onDelete={vi.fn()}
        />,
      ),
    ).not.toThrow()
  })
})
