import * as AccordionPrimitive from '@radix-ui/react-accordion'
import type * as React from 'react'
import { SvgTaillessLineArrowDown1 } from '@chainlink/blocks-icons'
import { cn } from '../../utils/cn'
import { typographyVariants } from '../Typography'

function Accordion({
  ...props
}: React.ComponentProps<typeof AccordionPrimitive.Root>) {
  return <AccordionPrimitive.Root data-slot="accordion" {...props} />
}

function AccordionItem({
  className,
  ...props
}: React.ComponentProps<typeof AccordionPrimitive.Item>) {
  return (
    <AccordionPrimitive.Item
      data-slot="accordion-item"
      className={cn('border-b last:border-b-0', className)}
      {...props}
    />
  )
}

function AccordionTrigger({
  className,
  children,
  ...props
}: React.ComponentProps<typeof AccordionPrimitive.Trigger>) {
  return (
    <AccordionPrimitive.Header className="flex">
      <AccordionPrimitive.Trigger
        data-slot="accordion-trigger"
        className={cn(
          typographyVariants({ variant: 'h6' }),
          'flex flex-1 items-start justify-between gap-4 rounded-md py-4 text-left font-medium outline-none transition-all focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50 [&[data-state=open]>svg]:rotate-180 [&[data-state=open]>svg]:text-foreground',

          className,
        )}
        {...props}
      >
        {children}
        <SvgTaillessLineArrowDown1
          color="muted"
          className={cn(
            'pointer-events-none mt-0.5 size-4 shrink-0 translate-y-0.5 transition-transform duration-200',
          )}
        />
      </AccordionPrimitive.Trigger>
    </AccordionPrimitive.Header>
  )
}

function AccordionContent({
  className,
  children,
  ...props
}: React.ComponentProps<typeof AccordionPrimitive.Content>) {
  return (
    <AccordionPrimitive.Content
      data-slot="accordion-content"
      className="data-[state=closed]:animate-accordion-up data-[state=open]:animate-accordion-down overflow-hidden"
      {...props}
    >
      <div
        className={cn(
          typographyVariants({ variant: 'body-s', color: 'muted' }),
          'pb-4 pt-0',
          className,
        )}
      >
        {children}
      </div>
    </AccordionPrimitive.Content>
  )
}

export { Accordion, AccordionItem, AccordionTrigger, AccordionContent }
