import { o as ue } from "./rolldown-runtime.js";
import { i as pe, t as me } from "./vendor/react.js";
import { t as fe } from "./chevron-down.js";
import { o as ge, s as X, t as V } from "./useRenderElement.js";
import { n as B, t as F } from "./useIsoLayoutEffect.js";
import { t as be } from "./useButton.js";
import { t as U } from "./utils.js";
import { a as he, n as ve } from "./useTransitionStatus.js";
import { t as Y } from "./useBaseUiId.js";
import { o as xe } from "./event.js";
import { n as Ce } from "./DirectionContext.js";
import { g as Ae, t as Re } from "./createBaseUIEventDetails.js";
import { a as Me, i as Pe, n as Ie, o as ye, r as Ne, t as Se } from "./_shared.js";
import { t as Ee } from "./useControlled.js";
import { n as Oe } from "./useCompositeListItem.js";
import { t as je } from "./CompositeList.js";
import { t as J } from "./isElementDisabled.js";
import { a as Z, c as ke, i as ee, n as te, o as oe } from "./composite2.js";
import { t as _e } from "./use-controllable-state.js";
import { a as ne, i as we, n as Ve, o as Te, r as Fe, t as Ue } from "./useCollapsiblePanel.js";
var o = /* @__PURE__ */ ue(pe()), re = /* @__PURE__ */ o.createContext(void 0);
function L() {
  const e = o.useContext(re);
  if (e === void 0) throw new Error(X(10));
  return e;
}
var x = me(), De = { value: () => null }, We = /* @__PURE__ */ o.forwardRef(function(t, r) {
  const { render: m, className: l, disabled: a = !1, hiddenUntilFound: c, keepMounted: A, loopFocus: w = !0, onValueChange: _, multiple: S = !1, orientation: g = "vertical", value: R, defaultValue: E, style: O, ...I } = t, b = Ce(), y = o.useMemo(() => {
    if (R === void 0) return E ?? [];
  }, [R, E]), h = B(_), C = o.useRef([]), [s, u] = Ee({
    controlled: R,
    default: y,
    name: "Accordion",
    state: "value"
  }), d = B((f, M) => {
    const p = Re(Ae);
    if (S)
      if (M) {
        const n = s.slice();
        if (n.push(f), h(n, p), p.isCanceled) return;
        u(n);
      } else {
        const n = s.filter((i) => i !== f);
        if (h(n, p), p.isCanceled) return;
        u(n);
      }
    else {
      const n = s[0] === f ? [] : [f];
      if (h(n, p), p.isCanceled) return;
      u(n);
    }
  }), v = o.useMemo(() => ({
    value: s,
    disabled: a,
    orientation: g
  }), [
    s,
    a,
    g
  ]), j = o.useMemo(() => ({
    accordionItemRefs: C,
    direction: b,
    disabled: a,
    handleValueChange: d,
    hiddenUntilFound: c ?? !1,
    keepMounted: A ?? !1,
    loopFocus: w,
    orientation: g,
    state: v,
    value: s
  }), [
    b,
    a,
    d,
    c,
    A,
    w,
    g,
    v,
    s
  ]), N = V("div", t, {
    state: v,
    ref: r,
    props: [{
      dir: b,
      role: "region"
    }, I],
    stateAttributesMapping: De
  });
  return /* @__PURE__ */ (0, x.jsx)(re.Provider, {
    value: j,
    children: /* @__PURE__ */ (0, x.jsx)(je, {
      elementsRef: C,
      children: N
    })
  });
}), ae = /* @__PURE__ */ o.createContext(void 0);
function q() {
  const e = o.useContext(ae);
  if (e === void 0) throw new Error(X(9));
  return e;
}
var He = /* @__PURE__ */ (function(e) {
  return e.index = "data-index", e.disabled = "data-disabled", e.open = "data-open", e;
})({}), z = {
  ...Ve,
  index: (e) => Number.isInteger(e) ? { [He.index]: String(e) } : null,
  ...he,
  value: () => null
}, Be = /* @__PURE__ */ o.forwardRef(function(t, r) {
  const { className: m, disabled: l = !1, onOpenChange: a, render: c, value: A, style: w, ..._ } = t, { ref: S, index: g } = Oe(), R = ge(r, S), { disabled: E, handleValueChange: O, state: I, value: b } = L(), y = Y(), h = A ?? y, C = l || E, s = o.useMemo(() => {
    if (!b) return !1;
    for (let i = 0; i < b.length; i += 1) if (b[i] === h) return !0;
    return !1;
  }, [b, h]), u = B((i, P) => {
    a?.(i, P), !P.isCanceled && O(h, i);
  }), d = Te({
    open: s,
    onOpenChange: u,
    disabled: C
  }), v = o.useMemo(() => ({
    open: d.open,
    disabled: d.disabled,
    hidden: !d.mounted,
    transitionStatus: d.transitionStatus
  }), [
    d.open,
    d.disabled,
    d.mounted,
    d.transitionStatus
  ]), j = o.useMemo(() => ({
    ...d,
    onOpenChange: u,
    state: v
  }), [
    d,
    v,
    u
  ]), N = o.useMemo(() => ({
    ...I,
    index: g,
    disabled: C,
    open: s
  }), [
    C,
    g,
    s,
    I
  ]), [f, M] = o.useState(Y()), p = o.useMemo(() => ({
    open: s,
    state: N,
    setTriggerId: M,
    triggerId: f
  }), [
    s,
    N,
    M,
    f
  ]), n = V("div", t, {
    state: N,
    ref: R,
    props: _,
    stateAttributesMapping: z
  });
  return /* @__PURE__ */ (0, x.jsx)(we.Provider, {
    value: j,
    children: /* @__PURE__ */ (0, x.jsx)(ae.Provider, {
      value: p,
      children: n
    })
  });
}), Ke = /* @__PURE__ */ o.forwardRef(function(t, r) {
  const { render: m, className: l, style: a, ...c } = t, { state: A } = q();
  return V("h3", t, {
    state: A,
    ref: r,
    props: c,
    stateAttributesMapping: z
  });
}), $e = /* @__PURE__ */ new Set([
  te,
  oe,
  Z,
  ee,
  ke,
  "End"
]);
function Le(e) {
  const { current: t } = e, r = [];
  for (let m = 0; m < t.length; m += 1) {
    const l = t[m];
    if (!J(l)) {
      const a = l?.querySelector('[type="button"], [role="button"]');
      a && !J(a) && r.push(a);
    }
  }
  return r;
}
var qe = /* @__PURE__ */ o.forwardRef(function(t, r) {
  const { disabled: m, className: l, id: a, render: c, nativeButton: A = !0, style: w, ..._ } = t, { panelId: S, open: g, handleTrigger: R, disabled: E } = ne(), { getButtonProps: O, buttonRef: I } = be({
    disabled: m ?? E,
    focusableWhenDisabled: !0,
    native: A,
    composite: !0
  }), { accordionItemRefs: b, direction: y, loopFocus: h, orientation: C } = L(), s = y === "rtl", u = C === "horizontal", { state: d, setTriggerId: v, triggerId: j } = q();
  F(() => (a && v(a), () => {
    v(void 0);
  }), [a, v]);
  const N = o.useMemo(() => ({
    "aria-controls": g ? S : void 0,
    "aria-expanded": g,
    id: j,
    tabIndex: 0,
    onClick: R,
    onKeyDown(f) {
      if (!$e.has(f.key)) return;
      xe(f);
      const M = Le(b), p = M.length - 1;
      let n = -1;
      const i = M.indexOf(f.currentTarget);
      function P() {
        h ? n = i + 1 > p ? 0 : i + 1 : n = Math.min(i + 1, p);
      }
      function k() {
        h ? n = i === 0 ? p : i - 1 : n = i - 1;
      }
      switch (f.key) {
        case te:
          u || P();
          break;
        case oe:
          u || k();
          break;
        case Z:
          u && (s ? k() : P());
          break;
        case ee:
          u && (s ? P() : k());
          break;
        case "Home":
          n = 0;
          break;
        case "End":
          n = p;
          break;
        default:
          break;
      }
      n > -1 && M[n].focus();
    }
  }), [
    b,
    R,
    j,
    u,
    s,
    h,
    g,
    S
  ]);
  return V("button", t, {
    state: d,
    ref: [r, I],
    props: [
      N,
      _,
      O
    ],
    stateAttributesMapping: Fe
  });
}), Q = /* @__PURE__ */ (function(e) {
  return e.accordionPanelHeight = "--accordion-panel-height", e.accordionPanelWidth = "--accordion-panel-width", e;
})({}), ze = /* @__PURE__ */ o.forwardRef(function(t, r) {
  const { className: m, hiddenUntilFound: l, keepMounted: a, id: c, render: A, style: w, ..._ } = t, { hiddenUntilFound: S, keepMounted: g } = L(), { abortControllerRef: R, animationTypeRef: E, height: O, mounted: I, onOpenChange: b, open: y, panelId: h, panelRef: C, runOnceAnimationsFinish: s, setDimensions: u, setHiddenUntilFound: d, setKeepMounted: v, setMounted: j, setOpen: N, setVisible: f, transitionDimensionRef: M, visible: p, width: n, setPanelIdState: i, transitionStatus: P } = ne(), k = l ?? S, T = a ?? g;
  F(() => {
    if (c)
      return i(c), () => {
        i(void 0);
      };
  }, [c, i]), F(() => {
    d(k);
  }, [d, k]), F(() => {
    v(T);
  }, [v, T]), ve({
    open: y && P === "idle",
    ref: C,
    onComplete() {
      y && u({
        width: void 0,
        height: void 0
      });
    }
  });
  const { props: le } = Ue({
    abortControllerRef: R,
    animationTypeRef: E,
    externalRef: r,
    height: O,
    hiddenUntilFound: k,
    id: c ?? h,
    keepMounted: T,
    mounted: I,
    onOpenChange: b,
    open: y,
    panelRef: C,
    runOnceAnimationsFinish: s,
    setDimensions: u,
    setMounted: j,
    setOpen: N,
    setVisible: f,
    transitionDimensionRef: M,
    visible: p,
    width: n
  }), { state: G, triggerId: de } = q(), ce = V("div", t, {
    state: o.useMemo(() => ({
      ...G,
      transitionStatus: P
    }), [G, P]),
    ref: [r, C],
    props: [
      le,
      {
        "aria-labelledby": de,
        role: "region",
        style: {
          [Q.accordionPanelHeight]: O === void 0 ? "auto" : `${O}px`,
          [Q.accordionPanelWidth]: n === void 0 ? "auto" : `${n}px`
        }
      },
      _
    ],
    stateAttributesMapping: z
  });
  return T || k || I ? ce : null;
});
function se({ className: e, ...t }) {
  return /* @__PURE__ */ (0, x.jsx)(We, {
    "data-slot": "accordion",
    className: U("flex w-full flex-col", e),
    ...t
  });
}
function D({ className: e, ...t }) {
  return /* @__PURE__ */ (0, x.jsx)(Be, {
    "data-slot": "accordion-item",
    className: U("not-last:border-b", e),
    ...t
  });
}
function W({ className: e, children: t, ...r }) {
  return /* @__PURE__ */ (0, x.jsx)(Ke, {
    className: "flex",
    children: /* @__PURE__ */ (0, x.jsxs)(qe, {
      "data-slot": "accordion-trigger",
      className: U("focus-visible:ring-ring/50 focus-visible:border-ring focus-visible:after:border-ring **:data-[slot=accordion-trigger-icon]:text-muted-foreground rounded-md py-4 text-left text-sm font-medium focus-visible:ring-[3px] **:data-[slot=accordion-trigger-icon]:ms-auto **:data-[slot=accordion-trigger-icon]:size-5 group/accordion-trigger relative flex flex-1 items-center gap-3 justify-between border border-transparent transition-[color,border-color,box-shadow] duration-200 ease-smooth motion-reduce:transition-none outline-none disabled:pointer-events-none disabled:opacity-50", e),
      ...r,
      children: [t, /* @__PURE__ */ (0, x.jsx)(fe, {
        "data-slot": "accordion-trigger-icon",
        className: "pointer-events-none shrink-0 transition-transform duration-200 ease-smooth group-aria-expanded/accordion-trigger:rotate-180 motion-reduce:transition-none"
      })]
    })
  });
}
function H({ className: e, children: t, ...r }) {
  return /* @__PURE__ */ (0, x.jsx)(ze, {
    "data-slot": "accordion-content",
    className: "grid overflow-hidden text-sm transition-[grid-template-rows] duration-300 ease-decelerate motion-reduce:transition-none data-closed:grid-rows-[0fr] data-open:grid-rows-[1fr]",
    ...r,
    children: /* @__PURE__ */ (0, x.jsx)("div", {
      className: "min-h-0",
      children: /* @__PURE__ */ (0, x.jsx)("div", {
        className: U("pt-0 pb-4 [&_a]:hover:text-foreground [&_a]:underline [&_a]:underline-offset-3 [&_p:not(:last-child)]:mb-4", e),
        children: t
      })
    })
  });
}
se.displayName = "Accordion";
D.displayName = "Accordion.Item";
W.displayName = "Accordion.Trigger";
H.displayName = "Accordion.Content";
var Ge = Object.assign(se, {
  Item: D,
  Trigger: W,
  Content: H
});
function K({ value: e, defaultValue: t = [], onValueChange: r, multiple: m = !1, children: l, ...a }) {
  const [c, A] = _e({
    value: e,
    defaultValue: t,
    onChange: r
  });
  return /* @__PURE__ */ (0, x.jsx)(Ge, {
    value: c,
    onValueChange: A,
    multiple: m,
    ...a,
    children: l
  });
}
K.displayName = "Accordion";
var Ye = Object.assign(K, {
  Root: K,
  Item: D,
  Trigger: W,
  Content: H
}), $ = {
  items: "json",
  value: "json",
  defaultValue: "json",
  multiple: "boolean"
}, ie = class extends Se {
  get propDefs() {
    return $;
  }
  static get observedAttributes() {
    return Object.keys($).map(Ie);
  }
  _renderReact() {
    const e = this._readAllProps(), t = e.items || [], r = e.multiple || !1, m = [];
    for (const l of t) {
      const a = "content-" + l.value, c = this._getSlot(a);
      m.push(o.createElement(D, {
        key: l.value,
        value: l.value
      }, o.createElement(W, null, l.trigger), o.createElement(H, null, c ? Me(c) : null)));
    }
    return o.createElement(Ye, {
      value: e.value,
      defaultValue: e.defaultValue || [],
      onValueChange: (l) => Ne(this, "value-change", l.map(String)),
      multiple: r
    }, ...m);
  }
};
Pe(ie, $);
ye("hf-accordion", ie);
export {
  ie as AccordionWC,
  ie as default
};

//# sourceMappingURL=hf-accordion.js.map