'use client';

import * as React from 'react';
import * as HoverCardPrimitive from '@radix-ui/react-hover-card';
import { motion, useReducedMotion } from 'motion/react';

import { cn } from '@/lib/utils';
import { overlayPanelClass, overlayVariants } from '@/lib/motion';

const HoverCard = HoverCardPrimitive.Root;

const HoverCardTrigger = HoverCardPrimitive.Trigger;

const HoverCardContent = React.forwardRef<
  React.ElementRef<typeof HoverCardPrimitive.Content>,
  React.ComponentPropsWithoutRef<typeof HoverCardPrimitive.Content>
>(({ className, align = 'center', sideOffset = 4, children, ...props }, ref) => {
  const reduceMotion = useReducedMotion();
  return (
    <HoverCardPrimitive.Content
      ref={ref}
      align={align}
      sideOffset={sideOffset}
      asChild
      {...props}
    >
      <motion.div
        className={cn('z-50 w-64 p-4 outline-none', overlayPanelClass, className)}
        initial={reduceMotion ? false : 'initial'}
        animate="animate"
        exit="exit"
        variants={overlayVariants}
      >
        {children}
      </motion.div>
    </HoverCardPrimitive.Content>
  );
});
HoverCardContent.displayName = HoverCardPrimitive.Content.displayName;

export { HoverCard, HoverCardTrigger, HoverCardContent };
