"use client"

import { useEffect, useState } from "react"
import UnicornScene from "unicornstudio-react"

import { cn } from "@/lib/utils"

export const useWindowSize = () => {
  const [windowSize, setWindowSize] = useState({
    width: typeof window !== "undefined" ? window.innerWidth : 0,
    height: typeof window !== "undefined" ? window.innerHeight : 0,
  })

  useEffect(() => {
    const handleResize = () => {
      setWindowSize({
        width: window.innerWidth,
        height: window.innerHeight,
      })
    }

    window.addEventListener("resize", handleResize)
    handleResize()

    return () => window.removeEventListener("resize", handleResize)
  }, [])

  return windowSize
}

export const Component = ({ className }: { className?: string }) => {
  const { width, height } = useWindowSize()

  if (width === 0 || height === 0) {
    return <div className={cn("flex flex-col items-center", className)} />
  }

  return (
    <div className={cn("flex flex-col items-center", className)}>
      <UnicornScene production projectId="erpu4mAlEe8kmhaGKYe9" width={width} height={height} />
    </div>
  )
}

export const RaycastAnimatedBlackBackground = Component
