"use client";
import r from "react";
import { createContextScope as y } from "../../react-context/dist/index.js";
import { useComposedRefs as M } from "../../react-compose-refs/dist/index.js";
import { createSlot as x } from "../node_modules/@radix-ui/react-slot/dist/index.js";
import { jsx as d } from "react/jsx-runtime";
function g(s) {
  const m = s + "CollectionProvider", [A, N] = y(m), [_, f] = A(
    m,
    { collectionRef: { current: null }, itemMap: /* @__PURE__ */ new Map() }
  ), p = (c) => {
    const { scope: e, children: l } = c, o = r.useRef(null), t = r.useRef(/* @__PURE__ */ new Map()).current;
    return /* @__PURE__ */ d(_, { scope: e, itemMap: t, collectionRef: o, children: l });
  };
  p.displayName = m;
  const u = s + "CollectionSlot", E = x(u), C = r.forwardRef(
    (c, e) => {
      const { scope: l, children: o } = c, t = f(u, l), n = M(e, t.collectionRef);
      return /* @__PURE__ */ d(E, { ref: n, children: o });
    }
  );
  C.displayName = u;
  const a = s + "CollectionItemSlot", R = "data-radix-collection-item", T = x(a), I = r.forwardRef(
    (c, e) => {
      const { scope: l, children: o, ...t } = c, n = r.useRef(null), S = M(e, n), i = f(a, l);
      return r.useEffect(() => (i.itemMap.set(n, { ref: n, ...t }), () => void i.itemMap.delete(n))), /* @__PURE__ */ d(T, { [R]: "", ref: S, children: o });
    }
  );
  I.displayName = a;
  function O(c) {
    const e = f(s + "CollectionConsumer", c);
    return r.useCallback(() => {
      const o = e.collectionRef.current;
      if (!o) return [];
      const t = Array.from(o.querySelectorAll(`[${R}]`));
      return Array.from(e.itemMap.values()).sort(
        (i, v) => t.indexOf(i.ref.current) - t.indexOf(v.ref.current)
      );
    }, [e.collectionRef, e.itemMap]);
  }
  return [
    { Provider: p, Slot: C, ItemSlot: I },
    O,
    N
  ];
}
export {
  g as createCollection
};
