import * as React from 'react'
import { cn } from './utils'
import { AreaChart as AreaChartImpl } from '../../charts/area/index'
import type { AreaChartConfig } from '../../charts/area/types'
import type { ChartSeries } from '../../charts/types'

export interface AreaChartProps extends Omit<AreaChartConfig, 'container'> {
  className?: string
}

function AreaChart({ className, ...config }: AreaChartProps) {
  const containerRef = React.useRef<HTMLDivElement>(null)
  const chartRef = React.useRef<AreaChartImpl | null>(null)

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

export { AreaChart }
export type { ChartSeries as AreaChartSeries }
