"use client";
import { useDisclosureProviderContext } from './S6EF7IVO.js';
import { useButton } from './PCKO3OQ5.js';
import { createHook, forwardRef as forwardRef2, createElement } from './LMDWO4NN.js';
import { useBooleanEvent, useMetadataProps, useEvent, useMergeRefs } from './ABQUS43J.js';
import { __objRest, __spreadProps, __spreadValues } from './3YLGPPWQ.js';
import { invariant } from '../../../../../../@ariakit_core@0.4.15/node_modules/@ariakit/core/esm/__chunks/PBFD2E7P.js';
import { useRef, useState, useEffect } from 'react';

var TagName = "button";
var symbol = Symbol("disclosure");
var useDisclosure = createHook(
  function useDisclosure2(_a) {
    var _b = _a, { store, toggleOnClick = true } = _b, props = __objRest(_b, ["store", "toggleOnClick"]);
    const context = useDisclosureProviderContext();
    store = store || context;
    invariant(
      store,
      process.env.NODE_ENV !== "production" && "Disclosure must receive a `store` prop or be wrapped in a DisclosureProvider component."
    );
    const ref = useRef(null);
    const [expanded, setExpanded] = useState(false);
    const disclosureElement = store.useState("disclosureElement");
    const open = store.useState("open");
    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 = useBooleanEvent(toggleOnClick);
    const [isDuplicate, metadataProps] = useMetadataProps(props, symbol, true);
    const onClick = 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 = __spreadProps(__spreadValues(__spreadValues({
      "aria-expanded": expanded,
      "aria-controls": contentElement == null ? void 0 : contentElement.id
    }, metadataProps), props), {
      ref: useMergeRefs(ref, props.ref),
      onClick
    });
    props = useButton(props);
    return props;
  }
);
forwardRef2(function Disclosure2(props) {
  const htmlProps = useDisclosure(props);
  return createElement(TagName, htmlProps);
});

export { useDisclosure };
//# sourceMappingURL=F5MUBM3I.js.map
