import { jsx, jsxs } from 'react/jsx-runtime';
import React__default from 'react';
import { cn } from '../../../utils/cn.js';
import { useAccordionContext } from './Accordion.js';
import { useAccordionItemContext } from './AccordionItem.js';
import { motion } from 'motion/react';
import { PlusIcon } from '@heroicons/react/24/outline';
import { createTriggerIconVariants } from './Accordion.animations.js';

const AccordionTrigger = React__default.forwardRef(({ children, className, chevron, chevronRotation, iconRotation, chevronPosition = "right", ...props }, ref) => {
    var _a;
    const { theme } = useAccordionContext();
    const { isOpen, toggle, triggerId, contentId } = useAccordionItemContext();
    const resolvedChevronRotation = (_a = chevronRotation !== null && chevronRotation !== void 0 ? chevronRotation : iconRotation) !== null && _a !== void 0 ? _a : 45;
    // Resolve the indicator icon
    const defaultChevronIcon = jsx(PlusIcon, { className: "size-4", strokeWidth: 2 });
    const resolvedChevron = chevron === null
        ? null
        : chevron !== undefined
            ? chevron
            : chevronPosition === "left"
                ? null // rendered in the left slot instead
                : defaultChevronIcon;
    // When chevronPosition="left", render the default animated chevron before children
    const useDefaultOnLeft = chevron !== null && chevronPosition === "left" && chevron === undefined;
    const animatedChevron = (rotation, extraClass) => (jsx(motion.div, { initial: false, animate: isOpen ? "open" : "closed", variants: createTriggerIconVariants(rotation), className: cn("flex items-center justify-center shrink-0", extraClass), children: defaultChevronIcon }));
    return (jsxs("button", { ref: ref, type: "button", id: triggerId, className: cn(theme === null || theme === void 0 ? void 0 : theme.triggerStyle, className), "aria-expanded": isOpen, "aria-controls": contentId, "data-state": isOpen ? "open" : "closed", onClick: toggle, ...props, children: [useDefaultOnLeft && animatedChevron(resolvedChevronRotation), children, resolvedChevron && (jsx(motion.div, { initial: false, animate: isOpen ? "open" : "closed", variants: createTriggerIconVariants(resolvedChevronRotation), className: "flex items-center justify-center shrink-0 ml-auto", children: resolvedChevron }))] }));
});
AccordionTrigger.displayName = "Accordion.Trigger";

export { AccordionTrigger };
