import * as React from "react";
import { ChevronDown } from "lucide-react";
import * as NavigationMenuPrimitive from "@radix-ui/react-navigation-menu";
import { cva } from "class-variance-authority";

import { cn } from "../lib/utils";

const NavigationMenu = React.forwardRef<
  React.ElementRef<typeof NavigationMenuPrimitive.Root>,
  React.ComponentPropsWithoutRef<typeof NavigationMenuPrimitive.Root>
>(({ className, children, ...props }, ref) => (
  <NavigationMenuPrimitive.Root
    ref={ref}
    className={cn(
      "tw-figr-relative tw-figr-z-10 tw-figr-flex tw-figr-max-w-max tw-figr-flex-1 tw-figr-items-center tw-figr-justify-center",
      className
    )}
    {...props}
  >
    {children}
    <NavigationMenuViewport />
  </NavigationMenuPrimitive.Root>
));
NavigationMenu.displayName = NavigationMenuPrimitive.Root.displayName;

const NavigationMenuList = React.forwardRef<
  React.ElementRef<typeof NavigationMenuPrimitive.List>,
  React.ComponentPropsWithoutRef<typeof NavigationMenuPrimitive.List>
>(({ className, ...props }, ref) => (
  <NavigationMenuPrimitive.List
    ref={ref}
    className={cn(
      "tw-figr-group tw-figr-flex tw-figr-flex-1 tw-figr-list-none tw-figr-items-center tw-figr-justify-center tw-figr-space-x-xxs",
      className
    )}
    {...props}
  />
));
NavigationMenuList.displayName = NavigationMenuPrimitive.List.displayName;

const NavigationMenuItem = NavigationMenuPrimitive.Item;

const navigationMenuTriggerStyle = cva(
  "tw-figr-group tw-figr-inline-flex tw-figr-h-9 tw-figr-w-max tw-figr-items-center tw-figr-justify-center tw-figr-rounded-sm tw-figr-bg-background tw-figr-px-s tw-figr-py-xxs tw-figr-text-sm tw-figr-font-medium tw-figr-transition-colors hover:tw-figr-bg-neutral-100 hover:tw-figr-text-base-black focus:tw-figr-bg-neutral-100 focus:tw-figr-text-base-black focus:tw-figr-outline-none disabled:tw-figr-pointer-events-none disabled:tw-figr-opacity-50 data-[active]:tw-figr-bg-neutral-100 data-[state=open]:tw-figr-bg-neutral-100"
);

const NavigationMenuTrigger = React.forwardRef<
  React.ElementRef<typeof NavigationMenuPrimitive.Trigger>,
  React.ComponentPropsWithoutRef<typeof NavigationMenuPrimitive.Trigger>
>(({ className, children, ...props }, ref) => (
  <NavigationMenuPrimitive.Trigger
    ref={ref}
    className={cn(navigationMenuTriggerStyle(), "group", className)}
    {...props}
  >
    {children}{" "}
    <ChevronDown
      className="tw-figr-relative tw-figr-top-[1px] tw-figr-ml-xxs tw-figr-h-3 tw-figr-w-3 tw-figr-transition tw-figr-duration-300 group-data-[state=open]:tw-figr-rotate-180"
      aria-hidden="true"
    />
  </NavigationMenuPrimitive.Trigger>
));
NavigationMenuTrigger.displayName = NavigationMenuPrimitive.Trigger.displayName;

const NavigationMenuContent = React.forwardRef<
  React.ElementRef<typeof NavigationMenuPrimitive.Content>,
  React.ComponentPropsWithoutRef<typeof NavigationMenuPrimitive.Content>
>(({ className, ...props }, ref) => (
  <NavigationMenuPrimitive.Content
    ref={ref}
    className={cn(
      "tw-figr-left-0 tw-figr-top-0 tw-figr-w-full data-[motion^=from-]:tw-figr-animate-in data-[motion^=to-]:tw-figr-animate-out data-[motion^=from-]:tw-figr-fade-in data-[motion^=to-]:tw-figr-fade-out data-[motion=from-end]:tw-figr-slide-in-from-right-52 data-[motion=from-start]:tw-figr-slide-in-from-left-52 data-[motion=to-end]:tw-figr-slide-out-to-right-52 data-[motion=to-start]:tw-figr-slide-out-to-left-52 md:tw-figr-absolute md:tw-figr-w-auto",
      className
    )}
    {...props}
  />
));
NavigationMenuContent.displayName = NavigationMenuPrimitive.Content.displayName;

const NavigationMenuLink = NavigationMenuPrimitive.Link;

const NavigationMenuViewport = React.forwardRef<
  React.ElementRef<typeof NavigationMenuPrimitive.Viewport>,
  React.ComponentPropsWithoutRef<typeof NavigationMenuPrimitive.Viewport>
>(({ className, ...props }, ref) => (
  <div
    className={cn(
      "tw-figr-absolute tw-figr-left-0 tw-figr-top-full tw-figr-flex tw-figr-justify-center"
    )}
  >
    <NavigationMenuPrimitive.Viewport
      className={cn(
        "tw-figr-origin-top-center tw-figr-relative tw-figr-mt-xs tw-figr-h-[var(--radix-navigation-menu-viewport-height)] tw-figr-w-full tw-figr-overflow-hidden tw-figr-rounded-sm tw-figr-border tw-figr-bg-neutral-100 tw-figr-text-base-black tw-figr-shadow data-[state=open]:tw-figr-animate-in data-[state=closed]:tw-figr-animate-out data-[state=closed]:tw-figr-zoom-out-95 data-[state=open]:tw-figr-zoom-in-90 md:tw-figr-w-[var(--radix-navigation-menu-viewport-width)]",
        className
      )}
      ref={ref}
      {...props}
    />
  </div>
));
NavigationMenuViewport.displayName =
  NavigationMenuPrimitive.Viewport.displayName;

const NavigationMenuIndicator = React.forwardRef<
  React.ElementRef<typeof NavigationMenuPrimitive.Indicator>,
  React.ComponentPropsWithoutRef<typeof NavigationMenuPrimitive.Indicator>
>(({ className, ...props }, ref) => (
  <NavigationMenuPrimitive.Indicator
    ref={ref}
    className={cn(
      "tw-figr-top-full tw-figr-z-[1] tw-figr-flex tw-figr-h-1.5 tw-figr-items-end tw-figr-justify-center tw-figr-overflow-hidden data-[state=visible]:tw-figr-animate-in data-[state=hidden]:tw-figr-animate-out data-[state=hidden]:tw-figr-fade-out data-[state=visible]:tw-figr-fade-in",
      className
    )}
    {...props}
  >
    <div className="tw-figr-relative tw-figr-top-[60%] tw-figr-h-2 tw-figr-w-2 tw-figr-rotate-45 tw-figr-rounded-tl-xs tw-figr-bg-neutral-200 tw-figr-shadow-md" />
  </NavigationMenuPrimitive.Indicator>
));
NavigationMenuIndicator.displayName =
  NavigationMenuPrimitive.Indicator.displayName;

export {
  navigationMenuTriggerStyle,
  NavigationMenu,
  NavigationMenuList,
  NavigationMenuItem,
  NavigationMenuContent,
  NavigationMenuTrigger,
  NavigationMenuLink,
  NavigationMenuIndicator,
  NavigationMenuViewport
};
