'use strict';

var _extends = require('../../../@babel/runtime/helpers/esm/extends.js');
var React = require('react');
var index_module$2 = require('../../primitive/dist/index.module.js');
var index_module = require('../../react-context/dist/index.module.js');
var index_module$6 = require('../external/@radix-ui/react-use-controllable-state/dist/index.module.js');
var index_module$5 = require('../../react-use-layout-effect/dist/index.module.js');
var index_module$4 = require('../../react-compose-refs/dist/index.module.js');
var index_module$1 = require('../../react-primitive/dist/index.module.js');
var index_module$3 = require('../external/@radix-ui/react-presence/dist/index.module.js');
var index_module$7 = require('../../react-id/dist/index.module.js');

/* -------------------------------------------------------------------------------------------------
 * Collapsible
 * -----------------------------------------------------------------------------------------------*/ const $409067139f391064$var$COLLAPSIBLE_NAME = 'Collapsible';
const [$409067139f391064$var$createCollapsibleContext, $409067139f391064$export$952b32dcbe73087a] = index_module.createContextScope($409067139f391064$var$COLLAPSIBLE_NAME);
const [$409067139f391064$var$CollapsibleProvider, $409067139f391064$var$useCollapsibleContext] = $409067139f391064$var$createCollapsibleContext($409067139f391064$var$COLLAPSIBLE_NAME);
const $409067139f391064$export$6eb0f7ddcda6131f = /*#__PURE__*/ React.forwardRef((props, forwardedRef)=>{
    const { __scopeCollapsible: __scopeCollapsible , open: openProp , defaultOpen: defaultOpen , disabled: disabled , onOpenChange: onOpenChange , ...collapsibleProps } = props;
    const [open = false, setOpen] = index_module$6.useControllableState({
        prop: openProp,
        defaultProp: defaultOpen,
        onChange: onOpenChange
    });
    return /*#__PURE__*/ React.createElement($409067139f391064$var$CollapsibleProvider, {
        scope: __scopeCollapsible,
        disabled: disabled,
        contentId: index_module$7.useId(),
        open: open,
        onOpenToggle: React.useCallback(()=>setOpen((prevOpen)=>!prevOpen
            )
        , [
            setOpen
        ])
    }, /*#__PURE__*/ React.createElement(index_module$1.Primitive.div, _extends({
        "data-state": $409067139f391064$var$getState(open),
        "data-disabled": disabled ? '' : undefined
    }, collapsibleProps, {
        ref: forwardedRef
    })));
});
/* -------------------------------------------------------------------------------------------------
 * CollapsibleTrigger
 * -----------------------------------------------------------------------------------------------*/ const $409067139f391064$var$TRIGGER_NAME = 'CollapsibleTrigger';
const $409067139f391064$export$c135dce7b15bbbdc = /*#__PURE__*/ React.forwardRef((props, forwardedRef)=>{
    const { __scopeCollapsible: __scopeCollapsible , ...triggerProps } = props;
    const context = $409067139f391064$var$useCollapsibleContext($409067139f391064$var$TRIGGER_NAME, __scopeCollapsible);
    return /*#__PURE__*/ React.createElement(index_module$1.Primitive.button, _extends({
        type: "button",
        "aria-controls": context.contentId,
        "aria-expanded": context.open || false,
        "data-state": $409067139f391064$var$getState(context.open),
        "data-disabled": context.disabled ? '' : undefined,
        disabled: context.disabled
    }, triggerProps, {
        ref: forwardedRef,
        onClick: index_module$2.composeEventHandlers(props.onClick, context.onOpenToggle)
    }));
});
/* -------------------------------------------------------------------------------------------------
 * CollapsibleContent
 * -----------------------------------------------------------------------------------------------*/ const $409067139f391064$var$CONTENT_NAME = 'CollapsibleContent';
const $409067139f391064$export$aadde00976f34151 = /*#__PURE__*/ React.forwardRef((props, forwardedRef)=>{
    const { forceMount: forceMount , ...contentProps } = props;
    const context = $409067139f391064$var$useCollapsibleContext($409067139f391064$var$CONTENT_NAME, props.__scopeCollapsible);
    return /*#__PURE__*/ React.createElement(index_module$3.Presence, {
        present: forceMount || context.open
    }, ({ present: present  })=>/*#__PURE__*/ React.createElement($409067139f391064$var$CollapsibleContentImpl, _extends({}, contentProps, {
            ref: forwardedRef,
            present: present
        }))
    );
});
/* -----------------------------------------------------------------------------------------------*/ const $409067139f391064$var$CollapsibleContentImpl = /*#__PURE__*/ React.forwardRef((props, forwardedRef)=>{
    const { __scopeCollapsible: __scopeCollapsible , present: present , children: children , ...contentProps } = props;
    const context = $409067139f391064$var$useCollapsibleContext($409067139f391064$var$CONTENT_NAME, __scopeCollapsible);
    const [isPresent, setIsPresent] = React.useState(present);
    const ref = React.useRef(null);
    const composedRefs = index_module$4.useComposedRefs(forwardedRef, ref);
    const heightRef = React.useRef(0);
    const height = heightRef.current;
    const widthRef = React.useRef(0);
    const width = widthRef.current; // when opening we want it to immediately open to retrieve dimensions
    // when closing we delay `present` to retrieve dimensions before closing
    const isOpen = context.open || isPresent;
    const isMountAnimationPreventedRef = React.useRef(isOpen);
    const originalStylesRef = React.useRef();
    React.useEffect(()=>{
        const rAF = requestAnimationFrame(()=>isMountAnimationPreventedRef.current = false
        );
        return ()=>cancelAnimationFrame(rAF)
        ;
    }, []);
    index_module$5.useLayoutEffect(()=>{
        const node = ref.current;
        if (node) {
            originalStylesRef.current = originalStylesRef.current || {
                transitionDuration: node.style.transitionDuration,
                animationName: node.style.animationName
            }; // block any animations/transitions so the element renders at its full dimensions
            node.style.transitionDuration = '0s';
            node.style.animationName = 'none'; // get width and height from full dimensions
            const rect = node.getBoundingClientRect();
            heightRef.current = rect.height;
            widthRef.current = rect.width; // kick off any animations/transitions that were originally set up if it isn't the initial mount
            if (!isMountAnimationPreventedRef.current) {
                node.style.transitionDuration = originalStylesRef.current.transitionDuration;
                node.style.animationName = originalStylesRef.current.animationName;
            }
            setIsPresent(present);
        }
    /**
     * depends on `context.open` because it will change to `false`
     * when a close is triggered but `present` will be `false` on
     * animation end (so when close finishes). This allows us to
     * retrieve the dimensions *before* closing.
     */ }, [
        context.open,
        present
    ]);
    return /*#__PURE__*/ React.createElement(index_module$1.Primitive.div, _extends({
        "data-state": $409067139f391064$var$getState(context.open),
        "data-disabled": context.disabled ? '' : undefined,
        id: context.contentId,
        hidden: !isOpen
    }, contentProps, {
        ref: composedRefs,
        style: {
            [`--radix-collapsible-content-height`]: height ? `${height}px` : undefined,
            [`--radix-collapsible-content-width`]: width ? `${width}px` : undefined,
            ...props.style
        }
    }), isOpen && children);
});
/* -----------------------------------------------------------------------------------------------*/ function $409067139f391064$var$getState(open) {
    return open ? 'open' : 'closed';
}
const $409067139f391064$export$be92b6f5f03c0fe9 = $409067139f391064$export$6eb0f7ddcda6131f;
const $409067139f391064$export$41fb9f06171c75f4 = $409067139f391064$export$c135dce7b15bbbdc;
const $409067139f391064$export$7c6e2c02157bb7d2 = $409067139f391064$export$aadde00976f34151;

exports.Collapsible = $409067139f391064$export$6eb0f7ddcda6131f;
exports.CollapsibleContent = $409067139f391064$export$aadde00976f34151;
exports.CollapsibleTrigger = $409067139f391064$export$c135dce7b15bbbdc;
exports.Content = $409067139f391064$export$7c6e2c02157bb7d2;
exports.Root = $409067139f391064$export$be92b6f5f03c0fe9;
exports.Trigger = $409067139f391064$export$41fb9f06171c75f4;
exports.createCollapsibleScope = $409067139f391064$export$952b32dcbe73087a;
//# sourceMappingURL=index.module.js.map
