import * as React from "react";
import * as AccordionPrimitive from "@radix-ui/react-accordion";
import { ChevronDown } from "lucide-react";

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

const Accordion = React.forwardRef<
  React.ElementRef<typeof AccordionPrimitive.Root>,
  React.ComponentPropsWithoutRef<typeof AccordionPrimitive.Root>
>(({ className, ...props }, ref) => (
  <AccordionPrimitive.Root
    ref={ref}
    className={cn(
      "tw-figr-w-full tw-figr-rounded-xs tw-figr-bg-base-white tw-figr-p-ml tw-figr-font-secondary",
      className
    )}
    {...props}
  />
));

const AccordionItem = React.forwardRef<
  React.ElementRef<typeof AccordionPrimitive.Item>,
  React.ComponentPropsWithoutRef<typeof AccordionPrimitive.Item>
>(({ className, ...props }, ref) => (
  <AccordionPrimitive.Item
    ref={ref}
    className={cn(
      "tw-figr-border-b tw-figr-border-neutral-200 tw-figr-text-base-black last:tw-figr-border-b-0",
      className
    )}
    {...props}
  />
));
AccordionItem.displayName = "AccordionItem";

const AccordionTrigger = React.forwardRef<
  React.ElementRef<typeof AccordionPrimitive.Trigger>,
  React.ComponentPropsWithoutRef<typeof AccordionPrimitive.Trigger>
>(({ className, children, ...props }, ref) => (
  <AccordionPrimitive.Header className="tw-figr-flex">
    <AccordionPrimitive.Trigger
      ref={ref}
      className={cn(
        "tw-figr-flex tw-figr-flex-1 tw-figr-items-center tw-figr-justify-between tw-figr-py-m tw-figr-transition-all hover:tw-figr-underline [&[data-state=open]>svg]:tw-figr-rotate-180 tw-figr-text-desktop-content-accent",
        className
      )}
      {...props}
    >
      {children}
      <ChevronDown className="tw-figr-h-4 tw-figr-w-4 tw-figr-shrink-0 tw-figr-transition-transform tw-figr-duration-200" />
    </AccordionPrimitive.Trigger>
  </AccordionPrimitive.Header>
));
AccordionTrigger.displayName = AccordionPrimitive.Trigger.displayName;

const AccordionContent = React.forwardRef<
  React.ElementRef<typeof AccordionPrimitive.Content>,
  React.ComponentPropsWithoutRef<typeof AccordionPrimitive.Content>
>(({ className, children, ...props }, ref) => (
  <AccordionPrimitive.Content
    ref={ref}
    className="tw-figr-overflow-hidden tw-figr-transition-all data-[state=closed]:tw-figr-animate-accordion-up data-[state=open]:tw-figr-animate-accordion-down tw-figr-text-desktop-caption-regular"
    {...props}
  >
    <div className={cn("tw-figr-pb-4 tw-figr-pt-0", className)}>{children}</div>
  </AccordionPrimitive.Content>
));
AccordionContent.displayName = AccordionPrimitive.Content.displayName;

export { Accordion, AccordionItem, AccordionTrigger, AccordionContent };
