import * as React from "react"
import * as PopoverPrimitive from "@radix-ui/react-popover"

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

const Popover = PopoverPrimitive.Root

const PopoverTrigger = PopoverPrimitive.Trigger

const PopoverContent = React.forwardRef<
  React.ElementRef<typeof PopoverPrimitive.Content>,
  React.ComponentPropsWithoutRef<typeof PopoverPrimitive.Content>
>(({ className, align = "center", sideOffset = 4, ...props }, ref) => (
  <PopoverPrimitive.Portal>
    <PopoverPrimitive.Content
      ref={ref}
      align={align}
      sideOffset={sideOffset}
      onEscapeKeyDown={(e) => {
        // Ensure popover closes on Escape key for WCAG 2.1 Level A compliance
        e.preventDefault();
      }}
      className={cn(
        "smx-z-wordpress-dropdown smx-w-72 smx-rounded-md smx-border smx-bg-popover smx-p-4 smx-text-popover-foreground smx-shadow-md smx-outline-none data-[state=open]:smx-animate-in data-[state=closed]:smx-animate-out data-[state=closed]:smx-fade-out-0 data-[state=open]:smx-fade-in-0 data-[state=closed]:smx-zoom-out-95 data-[state=open]:smx-zoom-in-95 data-[side=bottom]:smx-slide-in-from-top-2 data-[side=left]:smx-slide-in-from-right-2 data-[side=right]:smx-slide-in-from-left-2 data-[side=top]:smx-slide-in-from-bottom-2 smx-origin-[--radix-popover-content-transform-origin]",
        className
      )}
      {...props}
    />
  </PopoverPrimitive.Portal>
))
PopoverContent.displayName = PopoverPrimitive.Content.displayName

export { Popover, PopoverTrigger, PopoverContent }
