import { jsxs as oe, jsx as f } from "react/jsx-runtime";
import { useScrollOverflow as ne } from "@spark-ui/hooks/use-scroll-overflow";
import { a as w } from "../index-BmAFn37q.mjs";
import { useState as V, useCallback as $, useLayoutEffect as j, useEffect as k, useMemo as se, createContext as re, useRef as z, useContext as T, Children as le, cloneElement as ie, isValidElement as ae } from "react";
import { a as ce } from "../Slot-C98rL4yy.mjs";
import { ArrowVerticalRight as de } from "@spark-ui/icons/ArrowVerticalRight";
import { I as K } from "../Icon-D1RueiPY.mjs";
import { I as U } from "../IconButton-BY3gYXtU.mjs";
import { ArrowVerticalLeft as ue } from "@spark-ui/icons/ArrowVerticalLeft";
import { B as ge } from "../Button-D32Avk2j.mjs";
var fe = typeof document < "u" ? j : k, pe = ({
  axis: o = "x",
  initialPages: n = []
} = {}) => {
  const e = o === "x" ? "width" : "height", r = o === "x" ? "scrollWidth" : "scrollHeight", t = o === "x" ? "clientWidth" : "clientHeight", s = o === "x" ? "left" : "top", c = o === "x" ? "right" : "bottom", d = o === "x" ? "scrollLeft" : "scrollTop", [l, h] = V(null), [{ pages: i, activePageIndex: u }, b] = V({
    pages: n,
    activePageIndex: 0
  }), y = $(
    (a) => {
      if (!l)
        return;
      if (Math.floor(l[r] - l[d]) <= l[t]) {
        b({ pages: a, activePageIndex: a.length - 1 });
        return;
      }
      const x = Array.from(l.children), R = l.getBoundingClientRect(), p = a.map((L) => {
        const M = L[0], B = x[M];
        E(B instanceof HTMLElement, "Expected HTMLElement");
        const ee = H(
          l,
          B,
          s
        ), te = B.getBoundingClientRect()[s] - R[s] - ee;
        return Math.abs(te);
      }), g = Math.min(...p), S = p.indexOf(g);
      b({ pages: a, activePageIndex: S });
    },
    [l, t, s, r, d]
  ), m = $(() => {
    if (!l)
      return;
    const a = Array.from(l.children), v = l.getBoundingClientRect();
    let x;
    const R = a.reduce((p, g, S) => {
      E(g instanceof HTMLElement, "Expected HTMLElement");
      const L = p[p.length - 1], M = W(g, g.parentElement);
      if (!L || g.dataset.shouldSnap === "true" || M[c] - x > Math.ceil(v[e])) {
        p.push([S]);
        const B = H(
          l,
          g,
          s
        );
        x = M[s] - B;
      } else
        L.push(S);
      return p;
    }, []);
    y(R);
  }, [y, l, e, c, s]);
  fe(() => {
    m();
  }, [m]), k(() => {
    const a = () => {
      m();
    };
    return window.addEventListener("resize", a), window.addEventListener("orientationchange", a), () => {
      window.removeEventListener("resize", a), window.removeEventListener("orientationchange", a);
    };
  }, [m]), k(() => {
    if (!l)
      return;
    const a = () => {
      y(i);
    };
    return l.addEventListener("scroll", a), () => {
      l.removeEventListener("scroll", a);
    };
  }, [y, i, l]);
  const N = (a, v) => {
    if (!l)
      return;
    const x = i[a];
    if (!x)
      return;
    const R = Array.from(l.children), p = x[0], g = R[p];
    if (!(g instanceof HTMLElement))
      return;
    const S = H(
      l,
      g,
      s
    ), L = v?.behavior || "smooth";
    l.scrollTo({
      behavior: L,
      [s]: W(g, g.parentElement)[s] - S
    });
  }, O = (a) => {
    N(u - 1, a);
  }, C = (a) => {
    N(u + 1, a);
  }, P = se(
    () => new Set(i.map((a) => a[0])),
    [i]
  ), F = u > 0, A = u < i.length - 1;
  return {
    hasPrevPage: F,
    hasNextPage: A,
    prev: O,
    next: C,
    goTo: N,
    refresh: m,
    pages: i,
    activePageIndex: u,
    snapPointIndexes: P,
    scrollRef: h
  };
}, W = (o, n) => {
  const e = D(o);
  if (!n)
    return e;
  const r = D(n);
  return {
    left: e.left - r.left,
    top: e.top - r.top,
    right: e.right - r.left,
    bottom: e.bottom - r.top,
    width: e.width,
    height: e.height
  };
}, D = (o) => {
  const n = o.getBoundingClientRect();
  let e = 0, r = 0, t = o.parentElement;
  for (; t; )
    e += t.scrollLeft, r += t.scrollTop, t = t.parentElement;
  const s = n.left + e, c = n.top + r;
  return {
    left: s,
    top: c,
    right: s + n.width,
    bottom: c + n.height,
    width: n.width,
    height: n.height
  };
}, he = (o, n) => {
  const r = window.getComputedStyle(o).getPropertyValue(`scroll-padding-${n}`) || "0px";
  if (r === "auto")
    return 0;
  const t = `Unsupported scroll padding value, expected <length> or <percentage> value, received ${r}`;
  if (r.endsWith("px")) {
    const s = parseInt(r);
    return E(!Number.isNaN(s), t), s;
  }
  if (r.endsWith("%")) {
    const s = parseInt(r);
    return E(!Number.isNaN(s), t), o.clientWidth / 100 * s;
  }
  throw new q(t);
}, me = (o, n) => {
  const r = window.getComputedStyle(o).getPropertyValue(`scroll-margin-${n}`) || "0px", t = `Unsupported scroll margin value, expected <length> value, received ${r}`;
  E(r.endsWith("px"), t);
  const s = parseInt(r);
  return E(!Number.isNaN(s), t), s;
}, H = (o, n, e) => {
  const r = he(o, e), t = me(n, e), s = W(n, n.parentElement);
  return Math.min(r + t, s[e]);
};
function E(o, n) {
  if (!o)
    throw new q(n);
}
var q = class extends Error {
  constructor(o) {
    super(`[react-snap-carousel]: ${o}`);
  }
};
const I = re(
  null
), G = ({
  snapType: o = "none",
  snapStop: n = "normal",
  scrollBehavior: e = "smooth",
  loop: r = !1,
  gap: t = 16,
  withFade: s = !1,
  scrollPadding: c = 0,
  children: d,
  className: l,
  ...h
}) => {
  const i = z(null), u = z(null), b = pe(), { overflow: y, refresh: m } = ne(i, {
    precisionTreshold: 1
  }), { activePageIndex: N, pages: O, refresh: C } = b, P = O[N], F = P ? [P[0] + 1, P[P.length - 1] + 1] : [0, 0], A = $(() => {
    C && i.current && setTimeout(() => {
      C();
    }, 0);
  }, [C]);
  k(() => {
    A();
  }, [d, A]), j(() => {
    i.current && requestAnimationFrame(() => {
      m();
    });
  }, [d, m]);
  const v = {
    ...b,
    snapType: o,
    snapStop: n,
    skipKeyboardNavigation: () => {
      u.current?.focus();
    },
    scrollBehavior: e,
    visibleItemsRange: F,
    loop: r,
    gap: t,
    withFade: s,
    scrollPadding: c,
    scrollAreaRef: i,
    overflow: y
  };
  return /* @__PURE__ */ oe(I.Provider, { value: v, children: [
    /* @__PURE__ */ f(
      "div",
      {
        "data-spark-component": "scrolling-list",
        className: w(
          "gap-lg group/scrolling-list relative flex flex-col default:w-full",
          l
        ),
        ...h,
        children: d
      }
    ),
    /* @__PURE__ */ f("span", { ref: u, className: "size-0 overflow-hidden", tabIndex: -1 })
  ] });
};
G.displayName = "ScrollingList";
const _ = ({
  children: o,
  visibility: n = "always",
  className: e,
  ...r
}) => /* @__PURE__ */ f(
  "div",
  {
    "data-spark-component": "scrolling-list-controls",
    className: w(
      "default:px-md pointer-events-none absolute inset-0 flex flex-row items-center justify-between overflow-hidden",
      e
    ),
    style: {
      "--scrolling-list-controls-opacity": n === "hover" ? "0" : "1"
    },
    "data-orientation": "horizontal",
    ...r,
    children: o
  }
);
_.displayName = "ScrollingList.Controls";
function ve(o, n) {
  const [e, r] = V(!1);
  return k(() => {
    const t = (d) => {
      r(!0);
      const l = d.target, h = n.current;
      if (l && h) {
        const i = l.getBoundingClientRect(), u = h.getBoundingClientRect();
        i.left >= u.left && i.right <= u.right && i.top >= u.top && i.bottom <= u.bottom || l.scrollIntoView({ behavior: "smooth", inline: "center", block: "nearest" });
      }
    }, s = (d) => {
      o.current && !o.current.contains(d.relatedTarget) && r(!1);
    }, c = o.current;
    return c && (c.addEventListener("focusin", t), c.addEventListener("focusout", s)), () => {
      c && (c.removeEventListener("focusin", t), c.removeEventListener("focusout", s));
    };
  }, [o, n]), e;
}
const J = ({
  asChild: o = !1,
  children: n,
  index: e = 0,
  className: r = "",
  ...t
}) => {
  const s = T(I), c = z(null), d = s.snapPointIndexes.has(e);
  return ve(c, s.scrollAreaRef), /* @__PURE__ */ f(
    o ? ce : "div",
    {
      "data-spark-component": "scrolling-list-item",
      role: "listitem",
      ref: c,
      className: w(
        "default:w-auto default:shrink-0",
        {
          "snap-start": d,
          "snap-normal": d && s.snapStop === "normal",
          "snap-always": d && s.snapStop === "always"
        },
        r
      ),
      ...t,
      children: n
    }
  );
};
J.displayName = "ScrollingList.Item";
function xe(...o) {
  return (n) => {
    o.forEach((e) => {
      typeof e == "function" ? e(n) : e && typeof e == "object" && "current" in e && (e.current = n);
    });
  };
}
const Q = ({ children: o, ref: n, className: e = "", ...r }) => {
  const t = T(I), s = {
    mandatory: "x mandatory",
    proximity: "x proximity",
    none: "none"
  }, c = (i) => {
    !t.loop && !t.hasPrevPage || (i.preventDefault(), t.goTo(t.hasPrevPage ? t.activePageIndex - 1 : t.pages.length - 1, {
      behavior: t.scrollBehavior
    }));
  }, d = (i) => {
    !t.loop && !t.hasNextPage || (i.preventDefault(), t.goTo(t.hasNextPage ? t.activePageIndex + 1 : 0, { behavior: t.scrollBehavior }));
  }, l = (i) => {
    i.key === "ArrowLeft" && c(i), i.key === "ArrowRight" && d(i);
  }, h = {
    scrollSnapType: s[t.snapType],
    scrollPaddingInline: "var(--scrolling-list-px)",
    "--scrolling-list-px": `${t.scrollPadding}px`,
    "--scrolling-list-gap": `${t.gap}px`,
    ...t.withFade && {
      maskImage: "linear-gradient(to right, rgba(0, 0, 0, 0), rgba(0, 0, 0, 1) 44px, rgba(0, 0, 0, 1) calc(100% - 44px), rgba(0, 0, 0, 0))",
      maskSize: `calc(100% + ${t.overflow.left ? "0px" : "44px"} + ${t.overflow.right ? "0px" : "44px"}) 100%`,
      maskPosition: `${t.overflow.left ? "0px" : "-44px"} 0`
    }
  };
  return /* @__PURE__ */ f(
    "div",
    {
      "data-spark-component": "scrolling-list-items",
      id: "scrolling-list-items",
      role: "list",
      className: w(
        "relative transition-all duration-300",
        "u-no-scrollbar overflow-x-auto scroll-smooth",
        "w-full gap-(--scrolling-list-gap) default:flex default:flex-row",
        "focus-visible:u-outline",
        e
      ),
      ref: xe(t.scrollAreaRef, t.scrollRef, n),
      style: h,
      onKeyDown: l,
      ...r,
      children: le.map(
        o,
        (i, u) => ae(i) ? ie(i, { index: u }) : i
      )
    }
  );
};
Q.displayName = "ScrollingList.Items";
const X = ({ "aria-label": o, ...n }) => {
  const e = T(I), r = () => {
    e.hasNextPage ? e.next({ behavior: e.scrollBehavior }) : e.goTo(0, { behavior: e.scrollBehavior });
  }, s = !(e.overflow.left || e.overflow.right) || !e.loop && !e.overflow.right;
  return /* @__PURE__ */ f(
    U,
    {
      "data-spark-component": "scrolling-list-next-button",
      size: "sm",
      intent: "surface",
      design: "filled",
      className: w(
        "pointer-events-auto opacity-(--scrolling-list-controls-opacity) shadow-sm disabled:invisible",
        "group-hover/scrolling-list:opacity-none focus-visible:opacity-none"
      ),
      onClick: r,
      disabled: s,
      "aria-label": o,
      "aria-controls": "scrolling-list-items",
      ...n,
      children: /* @__PURE__ */ f(K, { children: /* @__PURE__ */ f(de, {}) })
    }
  );
};
X.displayName = "ScrollingList.NextButton";
const Y = ({
  "aria-label": o,
  ...n
}) => {
  const e = T(I), r = () => {
    e.activePageIndex === 0 && (e.scrollAreaRef.current?.scrollLeft || 0) > 0 ? e.goTo(0, { behavior: e.scrollBehavior }) : e.hasPrevPage ? e.prev({ behavior: e.scrollBehavior }) : e.goTo(e.pages.length - 1, { behavior: e.scrollBehavior });
  }, s = !(e.overflow.left || e.overflow.right) || !e.loop && !e.overflow.left;
  return /* @__PURE__ */ f(
    U,
    {
      "data-spark-component": "scrolling-list-prev-button",
      size: "sm",
      intent: "surface",
      design: "filled",
      className: w(
        "pointer-events-auto opacity-(--scrolling-list-controls-opacity) shadow-sm disabled:invisible",
        "group-hover/scrolling-list:opacity-none focus-visible:opacity-none"
      ),
      onClick: r,
      disabled: s,
      "aria-label": o,
      "aria-controls": "scrolling-list-items",
      ...n,
      children: /* @__PURE__ */ f(K, { children: /* @__PURE__ */ f(ue, {}) })
    }
  );
};
Y.displayName = "ScrollingList.PrevButton";
const Z = ({ children: o, ...n }) => {
  const e = T(I);
  return /* @__PURE__ */ f(
    ge,
    {
      type: "button",
      design: "tinted",
      intent: "surface",
      tabIndex: 0,
      className: w(
        "z-raised absolute top-1/2 left-0 -translate-y-1/2",
        "not-focus-visible:pointer-events-none not-focus-visible:size-0 not-focus-visible:opacity-0"
      ),
      onClick: e.skipKeyboardNavigation,
      ...n,
      children: o
    }
  );
};
Z.displayName = "ScrollingList.SkipButton";
const we = Object.assign(G, {
  Controls: _,
  NextButton: X,
  PrevButton: Y,
  Item: J,
  Items: Q,
  SkipButton: Z
});
we.displayName = "ScrollingList";
export {
  we as ScrollingList
};
//# sourceMappingURL=index.mjs.map
