import * as React from 'react'
import { cn } from './utils'
import { ScatterChart as ScatterChartImpl } from '../../charts/scatter/index'
import type { ScatterChartConfig } from '../../charts/scatter/types'
import type { ChartSeries } from '../../charts/types'

export interface ScatterChartProps extends Omit<ScatterChartConfig, 'container'> {
  className?: string
}

function ScatterChart({ className, ...config }: ScatterChartProps) {
  const containerRef = React.useRef<HTMLDivElement>(null)
  const chartRef = React.useRef<ScatterChartImpl | null>(null)

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

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

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

export { ScatterChart }
export type { ChartSeries as ScatterChartSeries }
