import e from"react";import{createContextScope as r}from"../../react-context/dist/index.js";import{useComposedRefs as t}from"../../react-compose-refs/dist/index.js";import{Slot as o}from"../../react-slot/dist/index.js";import{jsx as n}from"react/jsx-runtime";function c(c){const l=c+"CollectionProvider",[i,s]=r(l),[f,a]=i(l,{collectionRef:{current:null},itemMap:new Map}),m=r=>{const{scope:t,children:o}=r,c=e.useRef(null),l=e.useRef(new Map).current;return n(f,{scope:t,itemMap:l,collectionRef:c,children:o})};m.displayName=l;const u=c+"CollectionSlot",d=e.forwardRef(((e,r)=>{const{scope:c,children:l}=e,i=a(u,c),s=t(r,i.collectionRef);return n(o,{ref:s,children:l})}));d.displayName=u;const p=c+"CollectionItemSlot",R="data-radix-collection-item",x=e.forwardRef(((r,c)=>{const{scope:l,children:i,...s}=r,f=e.useRef(null),m=t(c,f),u=a(p,l);return e.useEffect((()=>(u.itemMap.set(f,{ref:f,...s}),()=>{u.itemMap.delete(f)}))),n(o,{[R]:"",ref:m,children:i})}));return x.displayName=p,[{Provider:m,Slot:d,ItemSlot:x},function(r){const t=a(c+"CollectionConsumer",r);return e.useCallback((()=>{const e=t.collectionRef.current;if(!e)return[];const r=Array.from(e.querySelectorAll(`[${R}]`));return Array.from(t.itemMap.values()).sort(((e,t)=>r.indexOf(e.ref.current)-r.indexOf(t.ref.current)))}),[t.collectionRef,t.itemMap])},s]}export{c as createCollection};
//# sourceMappingURL=index.js.map
