import * as React from 'react'
import * as AccordionPrimitive from '@radix-ui/react-accordion'
import { ChevronDown } from 'lucide-react'
import tw from 'twin.macro'

const Accordion = AccordionPrimitive.Root

const AccordionItem = React.forwardRef<
  React.ElementRef<typeof AccordionPrimitive.Item>,
  React.ComponentPropsWithoutRef<typeof AccordionPrimitive.Item>
>((props, ref) => (
  <AccordionPrimitive.Item
    ref={ref}
    css={[tw`border-b border-b-slate-200 dark:border-b-slate-700`]}
    {...props}
  />
))
AccordionItem.displayName = 'AccordionItem'

const AccordionTrigger = React.forwardRef<
  React.ElementRef<typeof AccordionPrimitive.Trigger>,
  React.ComponentPropsWithoutRef<typeof AccordionPrimitive.Trigger>
>(({ children, ...props }, ref) => (
  <AccordionPrimitive.Header tw="flex">
    <AccordionPrimitive.Trigger
      ref={ref}
      css={tw`
        flex
        flex-1
        items-center
        justify-between
        py-4
        font-medium
        transition-all
        hover:underline
        [&[data-state=open]>svg]:rotate-180
      `}
      {...props}
    >
      {children}
      <ChevronDown tw="h-4 w-4 transition-transform 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>
>(({ children, ...props }, ref) => (
  <AccordionPrimitive.Content
    ref={ref}
    css={tw`
      overflow-hidden
      text-sm
      transition-all
      data-[state=open]:animate-accordion-down
      data-[state=closed]:animate-accordion-up
    `}
    {...props}
  >
    <div tw="pt-0 pb-4">{children}</div>
  </AccordionPrimitive.Content>
))
AccordionContent.displayName = AccordionPrimitive.Content.displayName

const _Accordion = Accordion

export {
  _Accordion as Root,
  AccordionItem as Item,
  AccordionTrigger as Trigger,
  AccordionContent as Content,
}
