'use client'

import React, { ReactNode, useEffect, useState } from 'react'
import { motion, Variants } from "framer-motion";
import clsx from 'clsx';

export type BlurredOverlayContainerProps = {
  visible: boolean
  bgOpacity?: number
  inDelay?: number
  outDelay?: number
  transitionDelay?: number
  inDuration?: number
  outDuration?: number
  children: ReactNode
  noBlur?: boolean
  onClick?: React.MouseEventHandler | undefined
} & React.ComponentProps<typeof motion.div>

const BlurredOverlayContainer = ({
  visible = true,
  bgOpacity = 30,
  inDelay = 0,
  outDelay = 200,
  inDuration = 300,
  outDuration = 300,
  transitionDelay: transitionDelay = 0,
  children,
  className,
  onClick,
  noBlur = false,
  ...props
}: BlurredOverlayContainerProps) => {
  const [loaded, setLoaded] = useState(visible)

  useEffect(() => {
    if (visible) {
      // Show immediately or after delay
      const timeout = setTimeout(() => setLoaded(true), inDelay)
      return () => clearTimeout(timeout)
    } else {
      // Hide with delay (matches opacity transition duration)
      const timeout = setTimeout(() => setLoaded(false), outDelay) // 300ms matches Tailwind transition
      return () => clearTimeout(timeout)
    }
  }, [visible, inDelay, outDelay])

  const variants: Variants = {
    hidden: {
      opacity: 0,
      visibility: 'hidden',
      display: 'none',
      transition: {
        delay: transitionDelay / 1000,
        duration: outDuration / 1000,
        ease: 'easeInOut',
      },
    },
    visible: {
      opacity: 1,
      visibility: 'visible',
      display: 'block',
      transition: {
        delay: transitionDelay / 1000,
        duration: inDuration / 1000,
        ease: 'easeInOut',
      },
    },
  };

  return (
    <motion.div
      data-visible={visible}
      data-loaded={loaded}
      data-blur={!noBlur}
      className={clsx(
        className,
        visible || loaded ? 'z-50' : !visible && !loaded ? '-z-50' : 'z-0',
        'absolute inset-0 flex items-center justify-center h-full w-full data-[blur=true]:backdrop-blur-sm',
      )}
      style={{
        backgroundColor: `rgba(0, 0, 0, ${bgOpacity / 100})`,
      }}

      initial={visible ? 'visible' : 'hidden'}
      animate={visible ? 'visible' : 'hidden'}
      variants={variants}
      {...props}
    >
      <div className='w-full h-dvh overflow-hidden' onClick={onClick}></div>
      {children}
    </motion.div>
  )
}

export default BlurredOverlayContainer
