import { LegendCategoryUI } from '../legend-category/legend-category-ui'
import { LegendRampUI } from '../legend-ramp/legend-ramp-ui'
import { LegendProportionUI } from '../legend-proportion/legend-proportion-ui'
import { LegendIconUI } from '../legend-icon/legend-icon-ui'
import type { LegendLabels } from '../../provider/labels'
import type { LegendVariable } from '../../stores'

export interface LegendItemUIProps {
  data: LegendVariable
  /** Forwarded to the renderers that show text (ramp header, proportion labels). */
  labels?: Pick<
    LegendLabels,
    | 'colorBasedOn'
    | 'colorGradient'
    | 'radiusRangeBy'
    | 'maxPrefix'
    | 'minPrefix'
  >
}

/**
 * Dispatches to the matching pure renderer based on the discriminated
 * `variables.type`. Adding a new legend type is localized to this switch.
 *
 * @experimental This API is new and may change in a future release.
 */
export function LegendItemUI({ data, labels }: LegendItemUIProps) {
  switch (data.type) {
    case 'category':
      return <LegendCategoryUI data={data} />
    case 'ramp':
      return <LegendRampUI data={data} labels={labels} />
    case 'proportion':
      return <LegendProportionUI data={data} labels={labels} />
    case 'icon':
      return <LegendIconUI data={data} />
    default: {
      // Exhaustiveness guard — a new variant must be handled above.
      const _exhaustive: never = data
      return _exhaustive
    }
  }
}
