var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import * as React from "react";
import { useEffect, useState, Children } from "react";
import { cn } from "@utils";
import { ADPIcon, iconList } from "../adpIcon";
import { Button } from "../button";
import { accordionVariants, accordionHeaderVariants, accordionContentVariants } from "./accordionVariants";
const AccordionHeader = React.forwardRef(({ children, className }, ref) => (_jsx("span", { ref: ref, className: className, children: children })));
AccordionHeader.displayName = "AccordionHeader";
const AccordionBody = React.forwardRef(({ children, className }, ref) => (_jsx("div", { ref: ref, className: className, children: children })));
AccordionBody.displayName = "AccordionBody";
const arrowSizeMapping = {
    xs: "xs",
    sm: "xs",
    md: "xs",
    lg: "sm",
    xl: "sm"
};
const AccordionComponent = React.forwardRef((_a, ref) => {
    var { children, expand, clickableHeader = true, className, size = "sm" } = _a, props = __rest(_a, ["children", "expand", "clickableHeader", "className", "size"]);
    const [expanded, setExpanded] = useState(false);
    const [bodyProps, setBodyProps] = useState();
    const [headerProps, setHeaderProps] = useState();
    const clickHandler = () => setExpanded((isExpanded) => !isExpanded);
    useEffect(() => setExpanded(Boolean(expand)), [expand]);
    useEffect(() => {
        if (children) {
            Children.forEach(children, (child) => {
                if (child.type === AccordionHeader || child.type === Accordion.Header) {
                    setHeaderProps(child.props);
                }
                else if (child.type === AccordionBody || child.type === Accordion.Body) {
                    setBodyProps(child.props);
                }
            });
        }
    }, [children]);
    if (!children) {
        return null;
    }
    return (_jsxs("div", Object.assign({ ref: ref, className: cn(accordionVariants({ size }), className) }, props, { children: [clickableHeader ? (_jsxs("button", { onClick: clickHandler, type: "button", className: cn(accordionHeaderVariants({ size }), "w-full justify-start", headerProps === null || headerProps === void 0 ? void 0 : headerProps.className), "aria-expanded": expanded ? "true" : "false", "aria-controls": "bodySection", "data-testid": "accordionHeader", children: [_jsx(ADPIcon, { className: cn("text-gray-500 dark:text-gray-800 transition-all duration-700 rtl:rotate-180", expanded && "rotate-90"), size: arrowSizeMapping[size], icon: iconList.rightArrow, fixedWidth: true }), headerProps === null || headerProps === void 0 ? void 0 : headerProps.children] })) : (_jsxs("div", { "aria-expanded": expanded ? "true" : "false", "aria-controls": "bodySection", className: cn(accordionHeaderVariants({ size }), headerProps === null || headerProps === void 0 ? void 0 : headerProps.className), "data-testid": "accordionHeader", children: [_jsx(Button, { variant: "text", onClick: clickHandler, color: "secondary", children: _jsx(ADPIcon, { className: cn("text-gray-500 dark:text-gray-800 transition-all duration-700 rtl:rotate-180", expanded && "rotate-90"), size: arrowSizeMapping[size], icon: iconList.rightArrow, fixedWidth: true }) }), headerProps === null || headerProps === void 0 ? void 0 : headerProps.children] })), _jsx("div", { "data-testid": "accordionBody", className: cn(accordionContentVariants({
                    expanded,
                    size
                }), bodyProps === null || bodyProps === void 0 ? void 0 : bodyProps.className), children: expanded && (bodyProps === null || bodyProps === void 0 ? void 0 : bodyProps.children) })] })));
});
AccordionComponent.displayName = "Accordion";
// Add subcomponents with type assertion
AccordionComponent.Header = AccordionHeader;
AccordionComponent.Body = AccordionBody;
// Export with proper type
export const Accordion = AccordionComponent;
//# sourceMappingURL=accordion.js.map