import { flushSync as x } from "react-dom";
import { useRef as u, useCallback as a, useEffect as y } from "react";
import { a as f, b } from "./mergeProps-B-cqsBhL.es.js";
import { $ as m } from "./useEvent-CKoH5wGJ.es.js";
import { $ as g } from "./SSRProvider-CQ9PmFV6.es.js";
import { $ } from "./useControlledState-3TGZUXux.es.js";
function C(n, t, r) {
  let { isDisabled: i } = n, l = f(), s = f(), o = g(), d = u(null), h = a(() => {
    d.current = requestAnimationFrame(() => {
      r.current && r.current.setAttribute("hidden", "until-found");
    }), x(() => {
      t.toggle();
    });
  }, [
    r,
    t
  ]);
  m(r, "beforematch", h);
  let p = u(null);
  return b(() => {
    if (d.current && cancelAnimationFrame(d.current), r.current && !o) {
      let e = r.current;
      p.current == null || typeof e.getAnimations != "function" ? t.isExpanded ? (e.removeAttribute("hidden"), e.style.setProperty("--disclosure-panel-width", "auto"), e.style.setProperty("--disclosure-panel-height", "auto")) : (e.setAttribute("hidden", "until-found"), e.style.setProperty("--disclosure-panel-width", "0px"), e.style.setProperty("--disclosure-panel-height", "0px")) : t.isExpanded !== p.current && (t.isExpanded ? (e.removeAttribute("hidden"), e.style.setProperty("--disclosure-panel-width", e.scrollWidth + "px"), e.style.setProperty("--disclosure-panel-height", e.scrollHeight + "px"), Promise.all(e.getAnimations().map((c) => c.finished)).then(() => {
        e.style.setProperty("--disclosure-panel-width", "auto"), e.style.setProperty("--disclosure-panel-height", "auto");
      }).catch(() => {
      })) : (e.style.setProperty("--disclosure-panel-width", e.scrollWidth + "px"), e.style.setProperty("--disclosure-panel-height", e.scrollHeight + "px"), window.getComputedStyle(e).height, e.style.setProperty("--disclosure-panel-width", "0px"), e.style.setProperty("--disclosure-panel-height", "0px"), Promise.all(e.getAnimations().map((c) => c.finished)).then(() => e.setAttribute("hidden", "until-found")).catch(() => {
      }))), p.current = t.isExpanded;
    }
  }, [
    i,
    r,
    t.isExpanded,
    o
  ]), y(() => () => {
    d.current && cancelAnimationFrame(d.current);
  }, []), {
    buttonProps: {
      id: l,
      "aria-expanded": t.isExpanded,
      "aria-controls": s,
      onPress: (e) => {
        !i && e.pointerType !== "keyboard" && t.toggle();
      },
      isDisabled: i,
      onPressStart(e) {
        e.pointerType === "keyboard" && !i && t.toggle();
      }
    },
    panelProps: {
      id: s,
      // This can be overridden at the panel element level.
      role: "group",
      "aria-labelledby": l,
      "aria-hidden": !t.isExpanded,
      hidden: o ? !t.isExpanded : void 0
    }
  };
}
function D(n) {
  let [t, r] = $(n.isExpanded, n.defaultExpanded || !1, n.onExpandedChange);
  const i = a(() => {
    r(!0);
  }, [
    r
  ]), l = a(() => {
    r(!1);
  }, [
    r
  ]), s = a(() => {
    r(!t);
  }, [
    r,
    t
  ]);
  return {
    isExpanded: t,
    setExpanded: r,
    expand: i,
    collapse: l,
    toggle: s
  };
}
export {
  D as $,
  C as a
};
//# sourceMappingURL=useDisclosureState-CPUsvP5-.es.js.map
