"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react/jsx-runtime"),t=require("@sanity/dashboard"),r=require("@sanity/ui"),s=require("lodash/intersection.js"),i=require("react"),n=require("sanity"),a=require("lodash/uniqBy"),o=require("rxjs"),u=require("rxjs/operators");function d(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var c=d(s),l=d(a);const m=e=>`drafts.${e._id}`;function p(e,t,r){return r.listen(e,t,{events:["welcome","mutation"],includeResult:!1,visibility:"query"}).pipe(u.switchMap((s=>o.of(1).pipe("welcome"===s.type?u.tap():u.delay(1e3),u.mergeMap((()=>r.fetch(e,t).then((e=>function(e,t){if(!e)return Promise.resolve([]);const r=Array.isArray(e)?e:[e],s=r.filter((e=>!e._id.startsWith("drafts."))).map(m);return t.fetch("*[_id in $ids]",{ids:s}).then((e=>{const t=r.map((t=>e.find((e=>e._id===m(t)))||t));return l.default(t,"_id")})).catch((e=>{throw new Error(`Problems fetching docs ${s}. Error: ${e.message}`)}))}(e,r))).catch((r=>{throw r.message.startsWith("Problems fetching docs")?r:new Error(`Query failed ${e} and ${JSON.stringify(t)}. Error: ${r.message}`)}))))))))}const y={title:"Last created",order:"_createdAt desc",limit:10,queryParams:{},showCreateButton:!0,apiVersion:"v1"};function h(s){const{query:a,limit:o,apiVersion:u,queryParams:d,types:l,order:m,title:h,showCreateButton:x,createButtonText:j}={...y,...s},[g,b]=i.useState(),[q,v]=i.useState(!0),[w,_]=i.useState(),$=n.useClient({apiVersion:u}),C=n.useSchema(),{assembledQuery:P,params:S}=i.useMemo((()=>{if(a)return{assembledQuery:a,params:d};const e=C.getTypeNames().filter((e=>{var t;const r=C.get(e);return"document"===(null==(t=null==r?void 0:r.type)?void 0:t.name)}));return{assembledQuery:`*[_type in $types] | order(${m}) [0...${2*o}]`,params:{types:l?c.default(l,e):e}}}),[C,a,d,m,o,l]);return i.useEffect((()=>{if(!P)return;const e=p(P,S,$).subscribe({next:e=>{b(e.slice(0,o)),v(!1)},error:e=>{_(e),v(!1)}});return()=>{e.unsubscribe()}}),[o,$,P,S]),e.jsx(t.DashboardWidgetContainer,{header:h,footer:l&&1===l.length&&x&&e.jsx(n.IntentButton,{mode:"bleed",style:{width:"100%"},paddingY:4,tone:"primary",type:"button",intent:"create",params:{type:l[0]},text:j||`Create new ${l[0]}`}),children:e.jsxs(r.Card,{children:[w&&e.jsx("div",{children:w.message}),!w&&q&&e.jsx(r.Card,{padding:4,children:e.jsx(r.Flex,{justify:"center",children:e.jsx(r.Spinner,{muted:!0})})}),!w&&!g&&!q&&e.jsx("div",{children:"Could not locate any documents :/"}),e.jsx(r.Stack,{space:2,children:g&&g.map((t=>e.jsx(f,{doc:t},t._id)))})]})})}function f({doc:t}){const s=n.useSchema().get(t._type);return e.jsx(r.Card,{flex:1,children:e.jsx(n.IntentButton,{intent:"edit",mode:"bleed",tooltipProps:{},params:{type:t._type,id:n.getPublishedId(t._id)},style:{width:"100%"},children:s?e.jsx(n.Preview,{layout:"default",schemaType:s,value:t},t._id):"Schema-type missing"})})}exports.documentListWidget=function(t){return{name:"document-list-widget",component:function(){return e.jsx(h,{...t})},layout:t.layout}};//# sourceMappingURL=index.cjs.map