'use strict';

var S6EF7IVO = require('./S6EF7IVO.cjs');
var PCKO3OQ5 = require('./PCKO3OQ5.cjs');
var LMDWO4NN = require('./LMDWO4NN.cjs');
var ABQUS43J = require('./ABQUS43J.cjs');
var _3YLGPPWQ = require('./3YLGPPWQ.cjs');
var PBFD2E7P = require('../../../../../../@ariakit_core@0.4.15/node_modules/@ariakit/core/esm/__chunks/PBFD2E7P.cjs');
var React = require('react');

var TagName = "button";
var symbol = Symbol("disclosure");
var useDisclosure = LMDWO4NN.createHook(
  function useDisclosure2(_a) {
    var _b = _a, { store, toggleOnClick = true } = _b, props = _3YLGPPWQ.__objRest(_b, ["store", "toggleOnClick"]);
    const context = S6EF7IVO.useDisclosureProviderContext();
    store = store || context;
    PBFD2E7P.invariant(
      store,
      process.env.NODE_ENV !== "production" && "Disclosure must receive a `store` prop or be wrapped in a DisclosureProvider component."
    );
    const ref = React.useRef(null);
    const [expanded, setExpanded] = React.useState(false);
    const disclosureElement = store.useState("disclosureElement");
    const open = store.useState("open");
    React.useEffect(() => {
      let isCurrentDisclosure = disclosureElement === ref.current;
      if (!(disclosureElement == null ? void 0 : disclosureElement.isConnected)) {
        store == null ? void 0 : store.setDisclosureElement(ref.current);
        isCurrentDisclosure = true;
      }
      setExpanded(open && isCurrentDisclosure);
    }, [disclosureElement, store, open]);
    const onClickProp = props.onClick;
    const toggleOnClickProp = ABQUS43J.useBooleanEvent(toggleOnClick);
    const [isDuplicate, metadataProps] = ABQUS43J.useMetadataProps(props, symbol, true);
    const onClick = ABQUS43J.useEvent((event) => {
      onClickProp == null ? void 0 : onClickProp(event);
      if (event.defaultPrevented) return;
      if (isDuplicate) return;
      if (!toggleOnClickProp(event)) return;
      store == null ? void 0 : store.setDisclosureElement(event.currentTarget);
      store == null ? void 0 : store.toggle();
    });
    const contentElement = store.useState("contentElement");
    props = _3YLGPPWQ.__spreadProps(_3YLGPPWQ.__spreadValues(_3YLGPPWQ.__spreadValues({
      "aria-expanded": expanded,
      "aria-controls": contentElement == null ? void 0 : contentElement.id
    }, metadataProps), props), {
      ref: ABQUS43J.useMergeRefs(ref, props.ref),
      onClick
    });
    props = PCKO3OQ5.useButton(props);
    return props;
  }
);
LMDWO4NN.forwardRef(function Disclosure2(props) {
  const htmlProps = useDisclosure(props);
  return LMDWO4NN.createElement(TagName, htmlProps);
});

exports.useDisclosure = useDisclosure;
//# sourceMappingURL=F5MUBM3I.cjs.map
