import { Children, cloneElement, isValidElement } from "react"

import { AccordionItemProps } from "./AccordionItem/accordionItem"
import { AccordionProps } from "./accordion"
import { accordionVariants } from "./variants"

export default function Accordion({
  children,
  className,
  theme,
  variant,
}: AccordionProps) {
  return (
    <ul className={accordionVariants({ className, variant, theme })}>
      {Children.map(children, (child) => {
        if (isValidElement<AccordionItemProps>(child)) {
          return cloneElement(child, { variant, theme })
        } else {
          return child
        }
      })}
    </ul>
  )
}
