import * as React from 'react'
import { cn } from './utils'
import { RadarChart as RadarChartImpl } from '../../charts/radar/index'
import type { RadarChartConfig, RadarDataset } from '../../charts/radar/types'

export interface RadarChartProps extends Omit<RadarChartConfig, 'container'> {
  className?: string
}

function RadarChart({ className, ...config }: RadarChartProps) {
  const containerRef = React.useRef<HTMLDivElement>(null)
  const chartRef = React.useRef<RadarChartImpl | null>(null)

  React.useEffect(() => {
    if (!containerRef.current) return
    chartRef.current = new RadarChartImpl({ ...config, container: containerRef.current })
    return () => {
      chartRef.current?.destroy()
      chartRef.current = null
    }
  }, [])

  React.useEffect(() => {
    if (chartRef.current && config.datasets) {
      chartRef.current.update(config.datasets)
    }
  }, [config.datasets])

  return <div ref={containerRef} data-slot="radar-chart" className={cn('w-full', className)} />
}

export { RadarChart }
export type { RadarDataset }
