"use client";
import * as s from "react";
import { u as k } from "./useControlled-Sx-_x7Sz.js";
import { u as I } from "./useIsoLayoutEffect-DpJo20by.js";
import { u as W } from "./useStableCallback-CudqoENs.js";
import { u as K } from "./useBaseUiId-C8Dq0BQq.js";
import { c as B, a as z, n as G } from "./createBaseUIEventDetails-CqidROtw.js";
import { u as J, b as Q, T as q } from "./useOpenChangeComplete-DkeH9FGJ.js";
import { f as X, w as Y, c as Z } from "./useRenderElement-DNB2vtF1.js";
import { u as $ } from "./useOnMount-JHtE6teb.js";
import { u as ee, A as m } from "./useAnimationFrame-DIc6eFHa.js";
function he(t) {
  const {
    open: r,
    defaultOpen: o,
    onOpenChange: _,
    disabled: T
  } = t, g = r !== void 0, [p, R] = k({
    controlled: r,
    default: o,
    name: "Collapsible",
    state: "open"
  }), {
    mounted: y,
    setMounted: h,
    transitionStatus: l
  } = J(p, !0, !0), [a, b] = s.useState(p), [{
    height: d,
    width: S
  }, M] = s.useState({
    height: void 0,
    width: void 0
  }), N = K(), [P, x] = s.useState(), H = P ?? N, [E, A] = s.useState(!1), [v, w] = s.useState(!1), F = s.useRef(null), c = s.useRef(null), L = s.useRef(null), C = s.useRef(null), e = Q(C, !1), u = W((i) => {
    const n = !p, f = B(z, i.nativeEvent);
    if (_(n, f), f.isCanceled)
      return;
    const O = C.current;
    c.current === "css-animation" && O != null && O.style.removeProperty("animation-name"), !E && !v && (c.current != null && c.current !== "css-animation" && !y && n && h(!0), c.current === "css-animation" && (!a && n && b(!0), !y && n && h(!0))), R(n), c.current === "none" && y && !n && h(!1);
  });
  return I(() => {
    g && c.current === "none" && !v && !p && h(!1);
  }, [g, v, p, r, h]), s.useMemo(() => ({
    abortControllerRef: F,
    animationTypeRef: c,
    disabled: T,
    handleTrigger: u,
    height: d,
    mounted: y,
    open: p,
    panelId: H,
    panelRef: C,
    runOnceAnimationsFinish: e,
    setDimensions: M,
    setHiddenUntilFound: A,
    setKeepMounted: w,
    setMounted: h,
    setOpen: R,
    setPanelIdState: x,
    setVisible: b,
    transitionDimensionRef: L,
    transitionStatus: l,
    visible: a,
    width: S
  }), [F, c, T, u, d, y, p, H, C, e, M, A, w, h, R, b, L, l, a, S]);
}
const U = /* @__PURE__ */ s.createContext(void 0);
process.env.NODE_ENV !== "production" && (U.displayName = "CollapsibleRootContext");
function ye() {
  const t = s.useContext(U);
  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>." : X(15));
  return t;
}
let D = (function(t) {
  return t.open = "data-open", t.closed = "data-closed", t[t.startingStyle = q.startingStyle] = "startingStyle", t[t.endingStyle = q.endingStyle] = "endingStyle", t;
})({}), te = /* @__PURE__ */ (function(t) {
  return t.panelOpen = "data-panel-open", t;
})({});
const ne = {
  [D.open]: ""
}, re = {
  [D.closed]: ""
}, ge = {
  open(t) {
    return t ? {
      [te.panelOpen]: ""
    } : null;
  }
}, be = {
  open(t) {
    return t ? ne : re;
  }
};
let ie = /* @__PURE__ */ (function(t) {
  return t.disabled = "data-disabled", t.orientation = "data-orientation", t;
})({});
function Se(t) {
  const {
    abortControllerRef: r,
    animationTypeRef: o,
    externalRef: _,
    height: T,
    hiddenUntilFound: g,
    keepMounted: p,
    id: R,
    mounted: y,
    onOpenChange: h,
    open: l,
    panelRef: a,
    runOnceAnimationsFinish: b,
    setDimensions: d,
    setMounted: S,
    setOpen: M,
    setVisible: N,
    transitionDimensionRef: P,
    visible: x,
    width: H
  } = t, E = s.useRef(!1), A = s.useRef(null), v = s.useRef(l), w = s.useRef(l), F = ee(), c = s.useMemo(() => o.current === "css-animation" ? !x : !l && !y, [l, y, x, o]), L = W((e) => {
    if (!e)
      return;
    if (o.current == null || P.current == null) {
      const n = getComputedStyle(e), f = n.animationName !== "none" && n.animationName !== "", O = n.transitionDuration !== "0s" && n.transitionDuration !== "";
      f && O ? process.env.NODE_ENV !== "production" && Y("CSS transitions and CSS animations both detected on Collapsible or Accordion panel.", "Only one of either animation type should be used.") : n.animationName === "none" && n.transitionDuration !== "0s" ? o.current = "css-transition" : n.animationName !== "none" && n.transitionDuration === "0s" ? o.current = "css-animation" : o.current = "none", e.getAttribute(ie.orientation) === "horizontal" || n.transitionProperty.indexOf("width") > -1 ? P.current = "width" : P.current = "height";
    }
    if (o.current !== "css-transition")
      return;
    (T === void 0 || H === void 0) && (d({
      height: e.scrollHeight,
      width: e.scrollWidth
    }), w.current && e.style.setProperty("transition-duration", "0s"));
    let u = -1, i = -1;
    return u = m.request(() => {
      w.current = !1, i = m.request(() => {
        setTimeout(() => {
          e.style.removeProperty("transition-duration");
        });
      });
    }), () => {
      m.cancel(u), m.cancel(i);
    };
  }), C = Z(_, a, L);
  return I(() => {
    if (o.current !== "css-transition")
      return;
    const e = a.current;
    if (!e)
      return;
    let u = -1;
    if (r.current != null && (r.current.abort(), r.current = null), l) {
      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((n) => {
        e.style.setProperty(n, "initial", "important");
      }), !w.current && !p && e.setAttribute(D.startingStyle, ""), d({
        height: e.scrollHeight,
        width: e.scrollWidth
      }), u = m.request(() => {
        Object.entries(i).forEach(([n, f]) => {
          f === "" ? e.style.removeProperty(n) : e.style.setProperty(n, f);
        });
      });
    } else {
      if (e.scrollHeight === 0 && e.scrollWidth === 0)
        return;
      d({
        height: e.scrollHeight,
        width: e.scrollWidth
      });
      const i = new AbortController();
      r.current = i;
      const n = i.signal;
      let f = null;
      const O = D.endingStyle;
      return f = new MutationObserver((V) => {
        V.some((j) => j.type === "attributes" && j.attributeName === O) && (f?.disconnect(), f = null, b(() => {
          d({
            height: 0,
            width: 0
          }), e.style.removeProperty("content-visibility"), S(!1), r.current === i && (r.current = null);
        }, n));
      }), f.observe(e, {
        attributes: !0,
        attributeFilter: [O]
      }), () => {
        f?.disconnect(), F.cancel(), r.current === i && (i.abort(), r.current = null);
      };
    }
    return () => {
      m.cancel(u);
    };
  }, [r, o, F, g, p, y, l, a, b, d, S]), I(() => {
    if (o.current !== "css-animation")
      return;
    const e = a.current;
    e && (A.current = e.style.animationName || A.current, e.style.setProperty("animation-name", "none"), d({
      height: e.scrollHeight,
      width: e.scrollWidth
    }), !v.current && !E.current && e.style.removeProperty("animation-name"), l ? (r.current != null && (r.current.abort(), r.current = null), S(!0), N(!0)) : (r.current = new AbortController(), b(() => {
      S(!1), N(!1), r.current = null;
    }, r.current.signal)));
  }, [r, o, l, a, b, d, S, N, x]), $(() => {
    const e = m.request(() => {
      v.current = !1;
    });
    return () => m.cancel(e);
  }), I(() => {
    if (!g)
      return;
    const e = a.current;
    if (!e)
      return;
    let u = -1, i = -1;
    return l && E.current && (e.style.transitionDuration = "0s", d({
      height: e.scrollHeight,
      width: e.scrollWidth
    }), u = m.request(() => {
      E.current = !1, i = m.request(() => {
        setTimeout(() => {
          e.style.removeProperty("transition-duration");
        });
      });
    })), () => {
      m.cancel(u), m.cancel(i);
    };
  }, [g, l, a, d]), I(() => {
    const e = a.current;
    e && g && c && (e.setAttribute("hidden", "until-found"), o.current === "css-transition" && e.setAttribute(D.startingStyle, ""));
  }, [g, c, o, a]), s.useEffect(function() {
    const u = a.current;
    if (!u)
      return;
    function i(n) {
      E.current = !0, M(!0), h(!0, B(G, n));
    }
    return u.addEventListener("beforematch", i), () => {
      u.removeEventListener("beforematch", i);
    };
  }, [h, a, M]), s.useMemo(() => ({
    props: {
      hidden: c,
      id: R,
      ref: C
    }
  }), [c, R, C]);
}
export {
  U as C,
  ye as a,
  Se as b,
  be as c,
  ge as t,
  he as u
};
