import * as React from 'react'
import { cn } from './utils'
import { LineChart as LineChartImpl } from '../../charts/line/index'
import type { LineChartConfig } from '../../charts/line/types'
import type { ChartSeries } from '../../charts/types'

export interface LineChartProps extends Omit<LineChartConfig, 'container'> {
  className?: string
}

function LineChart({ className, ...config }: LineChartProps) {
  const containerRef = React.useRef<HTMLDivElement>(null)
  const chartRef = React.useRef<LineChartImpl | null>(null)

  React.useEffect(() => {
    if (!containerRef.current) return
    chartRef.current = new LineChartImpl({ ...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="line-chart" className={cn('w-full', className)} />
}

export { LineChart }
export type { ChartSeries as LineChartSeries }
