"use client";
import { jsx as R, jsxs as le } from "react/jsx-runtime";
import { ChevronDownIcon as ue, ChevronUpIcon as pe } from "lucide-react";
import { cn as H } from "../../../lib/utils.js";
import * as o from "react";
import { u as fe } from "../../../useControlled-Sx-_x7Sz.js";
import { u as W } from "../../../useStableCallback-CudqoENs.js";
import { u as U } from "../../../useIsoLayoutEffect-DpJo20by.js";
import { f as Y, w as J, u as F, c as me } from "../../../useRenderElement-DNB2vtF1.js";
import { C as ge } from "../../../CompositeList-yq5K_yj6.js";
import { u as he } from "../../../DirectionContext-BSOheMwC.js";
import { c as be, n as xe } from "../../../createBaseUIEventDetails-CqidROtw.js";
import { c as ve, u as Ce, C as Ne, a as Q, t as Re, b as Ae } from "../../../useCollapsiblePanel-9pDlDh3R.js";
import { t as Ee, a as Me } from "../../../useOpenChangeComplete-DkeH9FGJ.js";
import { u as q } from "../../../useBaseUiId-C8Dq0BQq.js";
import { u as Ie } from "../../../useCompositeListItem-CYQrZg5-.js";
import { i as z } from "../../../isElementDisabled-Cbd8W9kH.js";
import { b as X, e as Z, c as ee, d as ne, H as Oe, E as Pe } from "../../../composite-BSpUD7nI.js";
import { a as we } from "../../../useButton-JYPKQKj4.js";
import { s as Ve } from "../../../event-BCa0Un_3.js";
const B = /* @__PURE__ */ o.createContext(void 0);
process.env.NODE_ENV !== "production" && (B.displayName = "AccordionRootContext");
function K() {
  const e = o.useContext(B);
  if (e === void 0)
    throw new Error(process.env.NODE_ENV !== "production" ? "Base UI: AccordionRootContext is missing. Accordion parts must be placed within <Accordion.Root>." : Y(10));
  return e;
}
const ke = {
  value: () => null
}, te = /* @__PURE__ */ o.forwardRef(function(n, r) {
  const {
    render: A,
    className: h,
    disabled: i = !1,
    hiddenUntilFound: p,
    keepMounted: E,
    loopFocus: y = !0,
    onValueChange: D,
    multiple: M = !1,
    orientation: b = "vertical",
    value: w,
    defaultValue: I,
    ...V
  } = n, m = he();
  process.env.NODE_ENV !== "production" && U(() => {
    p && E === !1 && J("The `keepMounted={false}` prop on a Accordion.Root will be ignored when using `hiddenUntilFound` since it requires Panels to remain mounted when closed.");
  }, [p, E]);
  const O = o.useMemo(() => {
    if (w === void 0)
      return I ?? [];
  }, [w, I]), x = W(D), l = o.useRef([]), [a, u] = fe({
    controlled: w,
    default: O,
    name: "Accordion",
    state: "value"
  }), t = W((C, _) => {
    const d = be(xe);
    if (M)
      if (_) {
        const s = a.slice();
        if (s.push(C), x(s, d), d.isCanceled)
          return;
        u(s);
      } else {
        const s = a.filter((f) => f !== C);
        if (x(s, d), d.isCanceled)
          return;
        u(s);
      }
    else {
      const s = a[0] === C ? [] : [C];
      if (x(s, d), d.isCanceled)
        return;
      u(s);
    }
  }), v = o.useMemo(() => ({
    value: a,
    disabled: i,
    orientation: b
  }), [a, i, b]), k = o.useMemo(() => ({
    accordionItemRefs: l,
    direction: m,
    disabled: i,
    handleValueChange: t,
    hiddenUntilFound: p ?? !1,
    keepMounted: E ?? !1,
    loopFocus: y,
    orientation: b,
    state: v,
    value: a
  }), [m, i, t, p, E, y, b, v, a]), P = F("div", n, {
    state: v,
    ref: r,
    props: [{
      dir: m,
      role: "region"
    }, V],
    stateAttributesMapping: ke
  });
  return /* @__PURE__ */ R(B.Provider, {
    value: k,
    children: /* @__PURE__ */ R(ge, {
      elementsRef: l,
      children: P
    })
  });
});
process.env.NODE_ENV !== "production" && (te.displayName = "AccordionRoot");
const L = /* @__PURE__ */ o.createContext(void 0);
process.env.NODE_ENV !== "production" && (L.displayName = "AccordionItemContext");
function $() {
  const e = o.useContext(L);
  if (e === void 0)
    throw new Error(process.env.NODE_ENV !== "production" ? "Base UI: AccordionItemContext is missing. Accordion parts must be placed within <Accordion.Item>." : Y(9));
  return e;
}
let ye = /* @__PURE__ */ (function(e) {
  return e.index = "data-index", e.disabled = "data-disabled", e.open = "data-open", e;
})({});
const j = {
  ...ve,
  index: (e) => Number.isInteger(e) ? {
    [ye.index]: String(e)
  } : null,
  ...Ee,
  value: () => null
}, oe = /* @__PURE__ */ o.forwardRef(function(n, r) {
  const {
    className: A,
    disabled: h = !1,
    onOpenChange: i,
    render: p,
    value: E,
    ...y
  } = n, {
    ref: D,
    index: M
  } = Ie(), b = me(r, D), {
    disabled: w,
    handleValueChange: I,
    state: V,
    value: m
  } = K(), O = q(), x = E ?? O, l = h || w, a = o.useMemo(() => {
    if (!m)
      return !1;
    for (let f = 0; f < m.length; f += 1)
      if (m[f] === x)
        return !0;
    return !1;
  }, [m, x]), u = W((f, N) => {
    i?.(f, N), !N.isCanceled && I(x, f);
  }), t = Ce({
    open: a,
    onOpenChange: u,
    disabled: l
  }), v = o.useMemo(() => ({
    open: t.open,
    disabled: t.disabled,
    hidden: !t.mounted,
    transitionStatus: t.transitionStatus
  }), [t.open, t.disabled, t.mounted, t.transitionStatus]), k = o.useMemo(() => ({
    ...t,
    onOpenChange: u,
    state: v
  }), [t, v, u]), P = o.useMemo(() => ({
    ...V,
    index: M,
    disabled: l,
    open: a
  }), [l, M, a, V]), [C, _] = o.useState(q()), d = o.useMemo(() => ({
    open: a,
    state: P,
    setTriggerId: _,
    triggerId: C
  }), [a, P, _, C]), s = F("div", n, {
    state: P,
    ref: b,
    props: y,
    stateAttributesMapping: j
  });
  return /* @__PURE__ */ R(Ne.Provider, {
    value: k,
    children: /* @__PURE__ */ R(L.Provider, {
      value: d,
      children: s
    })
  });
});
process.env.NODE_ENV !== "production" && (oe.displayName = "AccordionItem");
const ie = /* @__PURE__ */ o.forwardRef(function(n, r) {
  const {
    render: A,
    className: h,
    ...i
  } = n, {
    state: p
  } = $();
  return F("h3", n, {
    state: p,
    ref: r,
    props: i,
    stateAttributesMapping: j
  });
});
process.env.NODE_ENV !== "production" && (ie.displayName = "AccordionHeader");
const De = /* @__PURE__ */ new Set([X, Z, ee, ne, Oe, Pe]);
function _e(e) {
  const {
    current: n
  } = e, r = [];
  for (let A = 0; A < n.length; A += 1) {
    const h = n[A];
    if (!z(h)) {
      const i = h?.querySelector('[type="button"], [role="button"]');
      i && !z(i) && r.push(i);
    }
  }
  return r;
}
const re = /* @__PURE__ */ o.forwardRef(function(n, r) {
  const {
    disabled: A,
    className: h,
    id: i,
    render: p,
    nativeButton: E = !0,
    ...y
  } = n, {
    panelId: D,
    open: M,
    handleTrigger: b,
    disabled: w
  } = Q(), I = A ?? w, {
    getButtonProps: V,
    buttonRef: m
  } = we({
    disabled: I,
    focusableWhenDisabled: !0,
    native: E,
    composite: !0
  }), {
    accordionItemRefs: O,
    direction: x,
    loopFocus: l,
    orientation: a
  } = K(), u = x === "rtl", t = a === "horizontal", {
    state: v,
    setTriggerId: k,
    triggerId: P
  } = $();
  U(() => (i && k(i), () => {
    k(void 0);
  }), [i, k]);
  const C = o.useMemo(() => ({
    "aria-controls": M ? D : void 0,
    "aria-expanded": M,
    id: P,
    tabIndex: 0,
    onClick: b,
    onKeyDown(d) {
      if (!De.has(d.key))
        return;
      Ve(d);
      const s = _e(O), N = s.length - 1;
      let c = -1;
      const g = s.indexOf(d.target);
      function T() {
        l ? c = g + 1 > N ? 0 : g + 1 : c = Math.min(g + 1, N);
      }
      function S() {
        l ? c = g === 0 ? N : g - 1 : c = g - 1;
      }
      switch (d.key) {
        case X:
          t || T();
          break;
        case Z:
          t || S();
          break;
        case ee:
          t && (u ? S() : T());
          break;
        case ne:
          t && (u ? T() : S());
          break;
        case "Home":
          c = 0;
          break;
        case "End":
          c = N;
          break;
      }
      c > -1 && s[c].focus();
    }
  }), [O, b, P, t, u, l, M, D]);
  return F("button", n, {
    state: v,
    ref: [r, m],
    props: [C, y, V],
    stateAttributesMapping: Re
  });
});
process.env.NODE_ENV !== "production" && (re.displayName = "AccordionTrigger");
let G = /* @__PURE__ */ (function(e) {
  return e.accordionPanelHeight = "--accordion-panel-height", e.accordionPanelWidth = "--accordion-panel-width", e;
})({});
const se = /* @__PURE__ */ o.forwardRef(function(n, r) {
  const {
    className: A,
    hiddenUntilFound: h,
    keepMounted: i,
    id: p,
    render: E,
    ...y
  } = n, {
    hiddenUntilFound: D,
    keepMounted: M
  } = K(), {
    abortControllerRef: b,
    animationTypeRef: w,
    height: I,
    mounted: V,
    onOpenChange: m,
    open: O,
    panelId: x,
    panelRef: l,
    runOnceAnimationsFinish: a,
    setDimensions: u,
    setHiddenUntilFound: t,
    setKeepMounted: v,
    setMounted: k,
    setOpen: P,
    setVisible: C,
    transitionDimensionRef: _,
    visible: d,
    width: s,
    setPanelIdState: f,
    transitionStatus: N
  } = Q(), c = h ?? D, g = i ?? M;
  process.env.NODE_ENV !== "production" && U(() => {
    i === !1 && c && J("The `keepMounted={false}` prop on a Accordion.Panel will be ignored when using `contextHiddenUntilFound` on the Panel or the Root since it requires the panel to remain mounted when closed.");
  }, [c, i]), U(() => {
    if (p)
      return f(p), () => {
        f(void 0);
      };
  }, [p, f]), U(() => {
    t(c);
  }, [t, c]), U(() => {
    v(g);
  }, [v, g]), Me({
    open: O && N === "idle",
    ref: l,
    onComplete() {
      O && u({
        width: void 0,
        height: void 0
      });
    }
  });
  const {
    props: T
  } = Ae({
    abortControllerRef: b,
    animationTypeRef: w,
    externalRef: r,
    height: I,
    hiddenUntilFound: c,
    id: p ?? x,
    keepMounted: g,
    mounted: V,
    onOpenChange: m,
    open: O,
    panelRef: l,
    runOnceAnimationsFinish: a,
    setDimensions: u,
    setMounted: k,
    setOpen: P,
    setVisible: C,
    transitionDimensionRef: _,
    visible: d,
    width: s
  }), {
    state: S,
    triggerId: ae
  } = $(), de = o.useMemo(() => ({
    ...S,
    transitionStatus: N
  }), [S, N]), ce = F("div", n, {
    state: de,
    ref: [r, l],
    props: [T, {
      "aria-labelledby": ae,
      role: "region",
      style: {
        [G.accordionPanelHeight]: I === void 0 ? "auto" : `${I}px`,
        [G.accordionPanelWidth]: s === void 0 ? "auto" : `${s}px`
      }
    }, y],
    stateAttributesMapping: j
  });
  return g || c || !g && V ? ce : null;
});
process.env.NODE_ENV !== "production" && (se.displayName = "AccordionPanel");
function en({
  className: e,
  type: n,
  multiple: r,
  collapsible: A,
  ...h
}) {
  return /* @__PURE__ */ R(
    te,
    {
      className: H("flex w-full flex-col", e),
      "data-slot": "accordion",
      multiple: r ?? n === "multiple",
      ...h
    }
  );
}
function nn({ className: e, ...n }) {
  return /* @__PURE__ */ R(
    oe,
    {
      className: H("not-last:border-b", e),
      "data-slot": "accordion-item",
      ...n
    }
  );
}
function tn({
  className: e,
  children: n,
  ...r
}) {
  return /* @__PURE__ */ R(ie, { className: "flex", children: /* @__PURE__ */ le(
    re,
    {
      className: H(
        "group/accordion-trigger relative flex flex-1 items-start justify-between rounded-lg border border-transparent py-2.5 text-left font-medium text-sm outline-none transition-all hover:underline focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 focus-visible:after:border-ring aria-disabled:pointer-events-none aria-disabled:opacity-50 **:data-[slot=accordion-trigger-icon]:ml-auto **:data-[slot=accordion-trigger-icon]:size-4 **:data-[slot=accordion-trigger-icon]:text-muted-foreground",
        e
      ),
      "data-slot": "accordion-trigger",
      ...r,
      children: [
        n,
        /* @__PURE__ */ R(
          ue,
          {
            className: "pointer-events-none shrink-0 group-aria-expanded/accordion-trigger:hidden",
            "data-slot": "accordion-trigger-icon"
          }
        ),
        /* @__PURE__ */ R(
          pe,
          {
            className: "pointer-events-none hidden shrink-0 group-aria-expanded/accordion-trigger:inline",
            "data-slot": "accordion-trigger-icon"
          }
        )
      ]
    }
  ) });
}
function on({
  className: e,
  children: n,
  ...r
}) {
  return /* @__PURE__ */ R(
    se,
    {
      className: "overflow-hidden text-sm data-closed:animate-accordion-up data-open:animate-accordion-down",
      "data-slot": "accordion-content",
      ...r,
      children: /* @__PURE__ */ R(
        "div",
        {
          className: H(
            "h-(--accordion-panel-height) pt-0 pb-2.5 data-ending-style:h-0 data-starting-style:h-0 [&_a]:underline [&_a]:underline-offset-3 [&_a]:hover:text-foreground [&_p:not(:last-child)]:mb-4",
            e
          ),
          children: n
        }
      )
    }
  );
}
export {
  en as Accordion,
  on as AccordionContent,
  nn as AccordionItem,
  tn as AccordionTrigger
};
