import * as React from 'react'
import { cn } from './utils'
import { HeatmapChart as HeatmapChartImpl } from '../../charts/heatmap/index'
import type { HeatmapChartConfig, HeatmapCell } from '../../charts/heatmap/types'

export interface HeatmapChartProps extends Omit<HeatmapChartConfig, 'container'> {
  className?: string
}

function HeatmapChart({ className, ...config }: HeatmapChartProps) {
  const containerRef = React.useRef<HTMLDivElement>(null)
  const chartRef = React.useRef<HeatmapChartImpl | null>(null)

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

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

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

export { HeatmapChart }
export type { HeatmapCell }
