import { o as U } from "./rolldown-runtime.js";
import { i as k } from "./vendor/react.js";
import { o as K, s as z } from "./useRenderElement.js";
import { n as W, t as I } from "./useIsoLayoutEffect.js";
import { t as V } from "./useOnMount.js";
import { n as G, t as m } from "./useAnimationFrame.js";
import { i as j, r as J, t as Q } from "./useTransitionStatus.js";
import { t as X } from "./useBaseUiId.js";
import { t as Y } from "./addEventListener.js";
import { C as Z, g as $, t as B } from "./createBaseUIEventDetails.js";
import { t as ee } from "./useControlled.js";
var s = /* @__PURE__ */ U(k());
function ye(t) {
  const { open: n, defaultOpen: a, onOpenChange: D, disabled: H } = t, y = n !== void 0, [p, O] = ee({
    controlled: n,
    default: a,
    name: "Collapsible",
    state: "open"
  }), { mounted: g, setMounted: h, transitionStatus: c } = Q(p, !0, !0), [u, v] = s.useState(p), [{ height: d, width: b }, M] = s.useState({
    height: void 0,
    width: void 0
  }), w = X(), [P, E] = s.useState(), T = P ?? w, [R, F] = s.useState(!1), [_, C] = s.useState(!1), x = s.useRef(null), f = s.useRef(null), A = s.useRef(null), e = s.useRef(null), l = J(e, !1), i = W((r) => {
    const o = !p, S = B(Z, r.nativeEvent);
    if (D(o, S), S.isCanceled) return;
    const q = e.current;
    f.current === "css-animation" && q != null && q.style.removeProperty("animation-name"), !R && !_ && (f.current != null && f.current !== "css-animation" && !g && o && h(!0), f.current === "css-animation" && (!u && o && v(!0), !g && o && h(!0))), O(o), f.current === "none" && g && !o && h(!1);
  });
  return I(() => {
    y && f.current === "none" && !p && h(!1);
  }, [
    y,
    p,
    n,
    h
  ]), s.useMemo(() => ({
    abortControllerRef: x,
    animationTypeRef: f,
    disabled: H,
    handleTrigger: i,
    height: d,
    mounted: g,
    open: p,
    panelId: T,
    panelRef: e,
    runOnceAnimationsFinish: l,
    setDimensions: M,
    setHiddenUntilFound: F,
    setKeepMounted: C,
    setMounted: h,
    setOpen: O,
    setPanelIdState: E,
    setVisible: v,
    transitionDimensionRef: A,
    transitionStatus: c,
    visible: u,
    width: b
  }), [
    x,
    f,
    H,
    i,
    d,
    g,
    p,
    T,
    e,
    l,
    M,
    F,
    C,
    h,
    O,
    v,
    A,
    c,
    u,
    b
  ]);
}
var te = /* @__PURE__ */ s.createContext(void 0);
function ve() {
  const t = s.useContext(te);
  if (t === void 0) throw new Error(z(15));
  return t;
}
var N = (function(t) {
  return t.open = "data-open", t.closed = "data-closed", t[t.startingStyle = j.startingStyle] = "startingStyle", t[t.endingStyle = j.endingStyle] = "endingStyle", t;
})({}), ne = /* @__PURE__ */ (function(t) {
  return t.panelOpen = "data-panel-open", t;
})({}), re = { [N.open]: "" }, ie = { [N.closed]: "" }, be = { open(t) {
  return t ? { [ne.panelOpen]: "" } : null;
} }, Se = { open(t) {
  return t ? re : ie;
} }, se = /* @__PURE__ */ (function(t) {
  return t.disabled = "data-disabled", t.orientation = "data-orientation", t;
})({});
function Oe(t) {
  const { abortControllerRef: n, animationTypeRef: a, externalRef: D, height: H, hiddenUntilFound: y, keepMounted: p, id: O, mounted: g, onOpenChange: h, open: c, panelRef: u, runOnceAnimationsFinish: v, setDimensions: d, setMounted: b, setOpen: M, setVisible: w, transitionDimensionRef: P, visible: E, width: T } = t, R = s.useRef(!1), F = s.useRef(null), _ = s.useRef(c), C = s.useRef(c), x = G(), f = s.useMemo(() => a.current === "css-animation" ? !E : !c && !g, [
    c,
    g,
    E,
    a
  ]), A = K(D, u, W((e) => {
    if (!e) return;
    if (a.current == null || P.current == null) {
      const r = getComputedStyle(e), o = r.animationName !== "none" && r.animationName !== "", S = r.transitionDuration !== "0s" && r.transitionDuration !== "";
      o && S || (r.animationName === "none" && r.transitionDuration !== "0s" ? a.current = "css-transition" : r.animationName !== "none" && r.transitionDuration === "0s" ? a.current = "css-animation" : a.current = "none"), e.getAttribute(se.orientation) === "horizontal" || r.transitionProperty.indexOf("width") > -1 ? P.current = "width" : P.current = "height";
    }
    if (a.current !== "css-transition") return;
    (H === void 0 || T === void 0) && (d({
      height: e.scrollHeight,
      width: e.scrollWidth
    }), C.current && e.style.setProperty("transition-duration", "0s"));
    let l = -1, i = -1;
    return l = m.request(() => {
      C.current = !1, i = m.request(() => {
        setTimeout(() => {
          e.style.removeProperty("transition-duration");
        });
      });
    }), () => {
      m.cancel(l), m.cancel(i);
    };
  }));
  return I(() => {
    if (a.current !== "css-transition") return;
    const e = u.current;
    if (!e) return;
    let l = -1;
    if (n.current != null && (n.current.abort(), n.current = null), c) {
      const i = {
        "justify-content": e.style.justifyContent,
        "align-items": e.style.alignItems,
        "align-content": e.style.alignContent,
        "justify-items": e.style.justifyItems
      };
      Object.keys(i).forEach((r) => {
        e.style.setProperty(r, "initial", "important");
      }), !C.current && !p && e.setAttribute(N.startingStyle, ""), d({
        height: e.scrollHeight,
        width: e.scrollWidth
      }), l = m.request(() => {
        Object.entries(i).forEach(([r, o]) => {
          o === "" ? e.style.removeProperty(r) : e.style.setProperty(r, o);
        });
      });
    } else {
      if (e.scrollHeight === 0 && e.scrollWidth === 0) return;
      d({
        height: e.scrollHeight,
        width: e.scrollWidth
      });
      const i = new AbortController();
      n.current = i;
      const r = i.signal;
      let o = null;
      const S = N.endingStyle;
      return o = new MutationObserver((q) => {
        q.some((L) => L.type === "attributes" && L.attributeName === S) && (o?.disconnect(), o = null, v(() => {
          d({
            height: 0,
            width: 0
          }), e.style.removeProperty("content-visibility"), b(!1), n.current === i && (n.current = null);
        }, r));
      }), o.observe(e, {
        attributes: !0,
        attributeFilter: [S]
      }), () => {
        o?.disconnect(), x.cancel(), n.current === i && (i.abort(), n.current = null);
      };
    }
    return () => {
      m.cancel(l);
    };
  }, [
    n,
    a,
    x,
    y,
    p,
    g,
    c,
    u,
    v,
    d,
    b
  ]), I(() => {
    if (a.current !== "css-animation") return;
    const e = u.current;
    e && (F.current = e.style.animationName || F.current, e.style.setProperty("animation-name", "none"), d({
      height: e.scrollHeight,
      width: e.scrollWidth
    }), !_.current && !R.current && e.style.removeProperty("animation-name"), c ? (n.current != null && (n.current.abort(), n.current = null), b(!0), w(!0)) : (n.current = new AbortController(), v(() => {
      b(!1), w(!1), n.current = null;
    }, n.current.signal)));
  }, [
    n,
    a,
    c,
    u,
    v,
    d,
    b,
    w,
    E
  ]), V(() => {
    const e = m.request(() => {
      _.current = !1;
    });
    return () => m.cancel(e);
  }), I(() => {
    if (!y) return;
    const e = u.current;
    if (!e) return;
    let l = -1, i = -1;
    return c && R.current && (e.style.transitionDuration = "0s", d({
      height: e.scrollHeight,
      width: e.scrollWidth
    }), l = m.request(() => {
      R.current = !1, i = m.request(() => {
        setTimeout(() => {
          e.style.removeProperty("transition-duration");
        });
      });
    })), () => {
      m.cancel(l), m.cancel(i);
    };
  }, [
    y,
    c,
    u,
    d
  ]), I(() => {
    const e = u.current;
    e && y && f && (e.setAttribute("hidden", "until-found"), a.current === "css-transition" && e.setAttribute(N.startingStyle, ""));
  }, [
    y,
    f,
    a,
    u
  ]), s.useEffect(function() {
    const l = u.current;
    if (!l) return;
    function i(r) {
      R.current = !0, M(!0), h(!0, B($, r));
    }
    return Y(l, "beforematch", i);
  }, [
    h,
    u,
    M
  ]), s.useMemo(() => ({ props: {
    hidden: f,
    id: O,
    ref: A
  } }), [
    f,
    O,
    A
  ]);
}
export {
  ve as a,
  te as i,
  Se as n,
  ye as o,
  be as r,
  Oe as t
};

//# sourceMappingURL=useCollapsiblePanel.js.map