import * as i from "react";
import { b as k, c as B, d as L, t as K, a as T, f as z, T as $, e as J, A as m, g as G, h as Q, n as X } from "./useOpenChangeComplete-RUbg6RBw.mjs";
import { u as Y, a as Z } from "./useTransitionStatus-CktVVKxz.mjs";
function ee({
  controlled: t,
  default: n,
  name: s,
  state: R = "value"
}) {
  const {
    current: g
  } = i.useRef(t !== void 0), [b, p] = i.useState(n), S = g ? t : b;
  if (process.env.NODE_ENV !== "production") {
    i.useEffect(() => {
      g !== (t !== void 0) && console.error([`Base UI: A component is changing the ${g ? "" : "un"}controlled ${R} state of ${s} to be ${g ? "un" : ""}controlled.`, "Elements should not switch from uncontrolled to controlled (or vice versa).", `Decide between using a controlled or uncontrolled ${s} element for the lifetime of the component.`, "The nature of the state is determined during the first render. It's considered controlled if the value is not `undefined`.", "More info: https://fb.me/react-controlled-components"].join(`
`));
    }, [R, s, t]);
    const {
      current: u
    } = i.useRef(n);
    i.useEffect(() => {
      !g && JSON.stringify(u) !== JSON.stringify(n) && console.error([`Base UI: A component is changing the default ${R} state of an uncontrolled ${s} after being initialized. To suppress this warning opt to use a controlled ${s}.`].join(`
`));
    }, [JSON.stringify(n)]);
  }
  const y = i.useCallback((u) => {
    g || p(u);
  }, []);
  return [S, y];
}
let H;
process.env.NODE_ENV !== "production" && (H = /* @__PURE__ */ new Set());
function te(...t) {
  if (process.env.NODE_ENV !== "production") {
    const n = t.join(" ");
    H.has(n) || (H.add(n), console.warn(`Base UI: ${n}`));
  }
}
function ue(t) {
  const {
    open: n,
    defaultOpen: s,
    onOpenChange: R,
    disabled: g
  } = t, b = n !== void 0, [p, S] = ee({
    controlled: n,
    default: s,
    name: "Collapsible",
    state: "open"
  }), {
    mounted: y,
    setMounted: u,
    transitionStatus: c
  } = Y(p, !0, !0), [a, O] = i.useState(p), [{
    height: h,
    width: C
  }, I] = i.useState({
    height: void 0,
    width: void 0
  }), A = Z(), [D, P] = i.useState(), _ = D ?? A, [N, x] = i.useState(!1), [w, M] = i.useState(!1), F = i.useRef(null), f = i.useRef(null), j = i.useRef(null), E = i.useRef(null), e = k(E, !1), l = B((o) => {
    const r = !p, d = L(K, o.nativeEvent);
    if (R(r, d), d.isCanceled)
      return;
    const v = E.current;
    f.current === "css-animation" && v != null && v.style.removeProperty("animation-name"), !N && !w && (f.current != null && f.current !== "css-animation" && !y && r && u(!0), f.current === "css-animation" && (!a && r && O(!0), !y && r && u(!0))), S(r), f.current === "none" && y && !r && u(!1);
  });
  return T(() => {
    b && f.current === "none" && !w && !p && u(!1);
  }, [b, w, p, n, u]), i.useMemo(() => ({
    abortControllerRef: F,
    animationTypeRef: f,
    disabled: g,
    handleTrigger: l,
    height: h,
    mounted: y,
    open: p,
    panelId: _,
    panelRef: E,
    runOnceAnimationsFinish: e,
    setDimensions: I,
    setHiddenUntilFound: x,
    setKeepMounted: M,
    setMounted: u,
    setOpen: S,
    setPanelIdState: P,
    setVisible: O,
    transitionDimensionRef: j,
    transitionStatus: c,
    visible: a,
    width: C
  }), [F, f, g, l, h, y, p, _, E, e, I, x, M, u, S, O, j, c, a, C]);
}
const q = /* @__PURE__ */ i.createContext(void 0);
process.env.NODE_ENV !== "production" && (q.displayName = "CollapsibleRootContext");
function ce() {
  const t = i.useContext(q);
  if (t === void 0)
    throw new Error(process.env.NODE_ENV !== "production" ? "Base UI: CollapsibleRootContext is missing. Collapsible parts must be placed within <Collapsible.Root>." : z(15));
  return t;
}
let V = (function(t) {
  return t.open = "data-open", t.closed = "data-closed", t[t.startingStyle = $.startingStyle] = "startingStyle", t[t.endingStyle = $.endingStyle] = "endingStyle", t;
})({}), ne = /* @__PURE__ */ (function(t) {
  return t.panelOpen = "data-panel-open", t;
})({});
const re = {
  [V.open]: ""
}, ie = {
  [V.closed]: ""
}, fe = {
  open(t) {
    return t ? {
      [ne.panelOpen]: ""
    } : null;
  }
}, de = {
  open(t) {
    return t ? re : ie;
  }
};
let se = /* @__PURE__ */ (function(t) {
  return t.disabled = "data-disabled", t.orientation = "data-orientation", t;
})({});
function pe(t) {
  const {
    abortControllerRef: n,
    animationTypeRef: s,
    externalRef: R,
    height: g,
    hiddenUntilFound: b,
    keepMounted: p,
    id: S,
    mounted: y,
    onOpenChange: u,
    open: c,
    panelRef: a,
    runOnceAnimationsFinish: O,
    setDimensions: h,
    setMounted: C,
    setOpen: I,
    setVisible: A,
    transitionDimensionRef: D,
    visible: P,
    width: _
  } = t, N = i.useRef(!1), x = i.useRef(null), w = i.useRef(c), M = i.useRef(c), F = J(), f = i.useMemo(() => s.current === "css-animation" ? !P : !c && !y, [c, y, P, s]), j = B((e) => {
    if (!e)
      return;
    if (s.current == null || D.current == null) {
      const r = getComputedStyle(e), d = r.animationName !== "none" && r.animationName !== "", v = r.transitionDuration !== "0s" && r.transitionDuration !== "";
      d && v ? process.env.NODE_ENV !== "production" && te("CSS transitions and CSS animations both detected on Collapsible or Accordion panel.", "Only one of either animation type should be used.") : r.animationName === "none" && r.transitionDuration !== "0s" ? s.current = "css-transition" : r.animationName !== "none" && r.transitionDuration === "0s" ? s.current = "css-animation" : s.current = "none", e.getAttribute(se.orientation) === "horizontal" || r.transitionProperty.indexOf("width") > -1 ? D.current = "width" : D.current = "height";
    }
    if (s.current !== "css-transition")
      return;
    (g === void 0 || _ === void 0) && (h({
      height: e.scrollHeight,
      width: e.scrollWidth
    }), M.current && e.style.setProperty("transition-duration", "0s"));
    let l = -1, o = -1;
    return l = m.request(() => {
      M.current = !1, o = m.request(() => {
        setTimeout(() => {
          e.style.removeProperty("transition-duration");
        });
      });
    }), () => {
      m.cancel(l), m.cancel(o);
    };
  }), E = G(R, a, j);
  return T(() => {
    if (s.current !== "css-transition")
      return;
    const e = a.current;
    if (!e)
      return;
    let l = -1;
    if (n.current != null && (n.current.abort(), n.current = null), c) {
      const o = {
        "justify-content": e.style.justifyContent,
        "align-items": e.style.alignItems,
        "align-content": e.style.alignContent,
        "justify-items": e.style.justifyItems
      };
      Object.keys(o).forEach((r) => {
        e.style.setProperty(r, "initial", "important");
      }), !M.current && !p && e.setAttribute(V.startingStyle, ""), h({
        height: e.scrollHeight,
        width: e.scrollWidth
      }), l = m.request(() => {
        Object.entries(o).forEach(([r, d]) => {
          d === "" ? e.style.removeProperty(r) : e.style.setProperty(r, d);
        });
      });
    } else {
      if (e.scrollHeight === 0 && e.scrollWidth === 0)
        return;
      h({
        height: e.scrollHeight,
        width: e.scrollWidth
      });
      const o = new AbortController();
      n.current = o;
      const r = o.signal;
      let d = null;
      const v = V.endingStyle;
      return d = new MutationObserver((W) => {
        W.some((U) => U.type === "attributes" && U.attributeName === v) && (d?.disconnect(), d = null, O(() => {
          h({
            height: 0,
            width: 0
          }), e.style.removeProperty("content-visibility"), C(!1), n.current === o && (n.current = null);
        }, r));
      }), d.observe(e, {
        attributes: !0,
        attributeFilter: [v]
      }), () => {
        d?.disconnect(), F.cancel(), n.current === o && (o.abort(), n.current = null);
      };
    }
    return () => {
      m.cancel(l);
    };
  }, [n, s, F, b, p, y, c, a, O, h, C]), T(() => {
    if (s.current !== "css-animation")
      return;
    const e = a.current;
    e && (x.current = e.style.animationName || x.current, e.style.setProperty("animation-name", "none"), h({
      height: e.scrollHeight,
      width: e.scrollWidth
    }), !w.current && !N.current && e.style.removeProperty("animation-name"), c ? (n.current != null && (n.current.abort(), n.current = null), C(!0), A(!0)) : (n.current = new AbortController(), O(() => {
      C(!1), A(!1), n.current = null;
    }, n.current.signal)));
  }, [n, s, c, a, O, h, C, A, P]), Q(() => {
    const e = m.request(() => {
      w.current = !1;
    });
    return () => m.cancel(e);
  }), T(() => {
    if (!b)
      return;
    const e = a.current;
    if (!e)
      return;
    let l = -1, o = -1;
    return c && N.current && (e.style.transitionDuration = "0s", h({
      height: e.scrollHeight,
      width: e.scrollWidth
    }), l = m.request(() => {
      N.current = !1, o = m.request(() => {
        setTimeout(() => {
          e.style.removeProperty("transition-duration");
        });
      });
    })), () => {
      m.cancel(l), m.cancel(o);
    };
  }, [b, c, a, h]), T(() => {
    const e = a.current;
    e && b && f && (e.setAttribute("hidden", "until-found"), s.current === "css-transition" && e.setAttribute(V.startingStyle, ""));
  }, [b, f, s, a]), i.useEffect(function() {
    const l = a.current;
    if (!l)
      return;
    function o(r) {
      N.current = !0, I(!0), u(!0, L(X, r));
    }
    return l.addEventListener("beforematch", o), () => {
      l.removeEventListener("beforematch", o);
    };
  }, [u, a, I]), i.useMemo(() => ({
    props: {
      hidden: f,
      id: S,
      ref: E
    }
  }), [f, S, E]);
}
export {
  q as C,
  ce as a,
  pe as b,
  de as c,
  ee as d,
  fe as t,
  ue as u,
  te as w
};
//# sourceMappingURL=useCollapsiblePanel-kbMNl4Z4.mjs.map
