import{s as _}from"./chunk-LvLJmgfZ.js";import{p as b,u as q}from"./useEvent-D91BmmQi.js";import{t as A}from"./react-Bj1aDYRI.js";import{t as C}from"./compiler-runtime-B3qBwwSJ.js";import{t as $}from"./createReducer-DGx5Af-C.js";import{t as y}from"./requests-9-v2bhoi.js";import{t as w}from"./DeferredRequestRegistry-C8EV1PoE.js";import{n as I}from"./useAsyncData-Bg5iAViP.js";const P=new w("storage-list-entries",async(e,a)=>{await y().listStorageEntries({requestId:e,...a})}),N=new w("storage-download",async(e,a)=>{await y().downloadStorage({requestId:e,...a})});function u(e,a){return`${e}::${a??""}`}function W(e){return`${e}::`}var F=C(),p=_(A(),1);function L(){return{namespaces:[],entriesByPath:new Map,pageMetadataByPath:new Map}}var{reducer:X,createActions:Y,valueAtom:M,useActions:R}=$(L,{setNamespaces:(e,a)=>{let t=new Map(e.namespaces.map(r=>[r.name,r]));for(let r of a.namespaces)t.set(r.name,r);return{...e,namespaces:[...t.values()]}},setEntries:(e,a)=>{let t=u(a.namespace,a.prefix),r=new Map(e.entriesByPath),s=a.append&&r.has(t)?[...r.get(t)??[],...a.entries]:a.entries;r.set(t,s);let n=new Map(e.pageMetadataByPath);return n.set(t,{nextPageToken:a.nextPageToken??null}),{...e,entriesByPath:r,pageMetadataByPath:n}},clearNamespaceCache:(e,a)=>{let t=new Map(e.entriesByPath),r=new Map(e.pageMetadataByPath),s=W(a);for(let n of t.keys())n.startsWith(s)&&t.delete(n);for(let n of r.keys())n.startsWith(s)&&r.delete(n);return{...e,entriesByPath:t,pageMetadataByPath:r}},filterFromVariables:(e,a)=>{let t=new Set(a),r=e.namespaces.filter(s=>t.has(s.name));return{...e,namespaces:r}}});const x=()=>q(M),V=b(e=>e(M).namespaces);function m(){return R()}async function f({namespace:e,prefix:a,pageToken:t,append:r,setEntries:s}){let n=await P.request({namespace:e,prefix:a??"",limit:150,...t==null?{}:{pageToken:t}});if(n.error)throw Error(n.error);return s({namespace:e,prefix:a,entries:n.entries,nextPageToken:n.next_page_token,append:r}),n}function j(e,a){var h;let{entriesByPath:t,pageMetadataByPath:r}=x(),{setEntries:s}=m(),n=u(e,a),i=t.get(n),o=((h=r.get(n))==null?void 0:h.nextPageToken)??null,c=(0,p.useRef)(!1),[B,d]=(0,p.useState)(!1),[E,g]=(0,p.useState)(),{isPending:k,error:v,refetch:T}=I(async()=>{i||await f({namespace:e,prefix:a,setEntries:s})},[e,a,i===void 0]),S=(0,p.useCallback)(async()=>{if(!(o==null||c.current)){c.current=!0,d(!0),g(void 0);try{await f({namespace:e,prefix:a,pageToken:o,append:!0,setEntries:s})}catch(l){g(l instanceof Error?l:Error(String(l)))}finally{c.current=!1,d(!1)}}},[e,a,o,s]);return{entries:i??[],isPending:k&&!i,error:i?void 0:v,hasMore:o!==null,loadMore:S,isLoadingMore:B,loadMoreError:E,refetch:T}}function z(){let e=(0,F.c)(2),{setEntries:a}=m(),t;return e[0]===a?t=e[1]:(t=r=>f({...r,setEntries:a}),e[0]=a,e[1]=t),t}export{z as a,P as c,j as i,x as n,u as o,m as r,N as s,V as t};
