import{r as u,c as L,g as z,a3 as q,m as V,n as W,u as k,M as N,i as O,s as _,a4 as B,j as l,N as F,P as G,o as Q,a5 as Y}from"./index-e867d936.js";import{f as H,T as x}from"./_utils-98e246cc.js";var J=Object.defineProperty,K=Object.defineProperties,U=Object.getOwnPropertyDescriptors,b=Object.getOwnPropertySymbols,X=Object.prototype.hasOwnProperty,Z=Object.prototype.propertyIsEnumerable,M=(c,r,a)=>r in c?J(c,r,{enumerable:!0,configurable:!0,writable:!0,value:a}):c[r]=a,$=(c,r)=>{for(var a in r||(r={}))X.call(r,a)&&M(c,a,r[a]);if(b)for(var a of b(r))Z.call(r,a)&&M(c,a,r[a]);return c},ee=(c,r)=>K(c,U(r));function te(c=[]){const[r,a]=u.useState(c);return[r,{setState:a,append:(...t)=>a(s=>[...s,...t]),prepend:(...t)=>a(s=>[...t,...s]),insert:(t,...s)=>a(i=>[...i.slice(0,t),...s,...i.slice(t)]),pop:()=>a(t=>{const s=[...t];return s.pop(),s}),shift:()=>a(t=>{const s=[...t];return s.shift(),s}),apply:t=>a(s=>s.map((i,n)=>t(i,n))),applyWhere:(t,s)=>a(i=>i.map((n,m)=>t(n,m)?s(n,m):n)),remove:(...t)=>a(s=>s.filter((i,n)=>!t.includes(n))),reorder:({from:t,to:s})=>a(i=>{const n=[...i],m=i[t];return n.splice(t,1),n.splice(s,0,m),n}),setItem:(t,s)=>a(i=>{const n=[...i];return n[t]=s,n}),setItemProp:(t,s,i)=>a(n=>{const m=[...n];return m[t]=ee($({},m[t]),{[s]:i}),m}),filter:t=>{a(s=>s.filter(t))}}]}const ne=({})=>{const c=L(),{modelId:r}=z(),[a,y]=u.useState(1),[h,E]=u.useState(20),e=q(),d=V(),[j,P]=u.useState(!1),{data:o,isLoading:v,isError:w,refetch:S}=W(e==null?void 0:e.name,{params:{page:a,limit:h,...e!=null&&e.hasTimestamps?{"orderBy.created_at":"desc"}:{}}}),{t:g}=k(),[t,s]=te(o==null?void 0:o.data);u.useEffect(()=>{o!=null&&o.data&&s.setState(o==null?void 0:o.data)},[o]);const i=u.useMemo(()=>{if(!o)return!1;const{data:p,...f}=o;return f},[o]),n=async({source:p,destination:f})=>{if((f==null?void 0:f.index)===void 0)return;P(!0);const I=t[p.index].id,T=t[f.index].id;I!==T&&(s.reorder({from:p.index,to:f.index}),await O.entries.swap(e.name,{fromId:I,toId:T})),P(!1)},m=u.useMemo(()=>{if(e)return H(e)},[e]),C=e&&(d!=null&&d.can({action:"delete",targetModel:e==null?void 0:e.name}))?async p=>{confirm(g(N.ON_DELETE_REQUEST_PROMPT))&&(await O.entries.delete(e.name,p),S())}:void 0,D=e&&(d!=null&&d.can({action:"create",targetModel:e==null?void 0:e.name}))?async p=>{confirm(g(N.ENTRY_ITEM_DUPLICATE))&&c(_.entryTypes(e==null?void 0:e.name).duplicate(p))}:void 0,R=()=>c(_.entryTypes(e==null?void 0:e.name).create),A=p=>c(_.entryTypes(e==null?void 0:e.name).view(p));return u.useEffect(()=>y(1),[r]),!e||!m||B(e.name)?l.jsx(F,{text:g("This model with this id does not exist.")}):l.jsxs(G,{children:[l.jsxs("div",{className:"flex w-full flex-col justify-between gap-5 py-10 md:flex-row",children:[l.jsx("h1",{className:"text-3xl font-semibold capitalize",children:g(e.title??e.name)}),l.jsx("div",{className:"flex items-center gap-5",children:(d==null?void 0:d.can({action:"create",targetModel:e.name}))&&l.jsxs(Q,{color:"green",className:" items-center font-semibold uppercase",size:"md",onClick:R,children:[l.jsx("span",{className:"hidden md:block",children:g("Add new entry")}),l.jsx(Y,{className:"inline-block h-5 w-5 md:ml-3"})," "]})})]}),l.jsx(x,{isLoading:v||w,items:t,columns:m,onEditAction:A,onDeleteAction:C,onDuplicateAction:D,ordering:!!e.hasOrdering,onDragEnd:n,disabled:j}),l.jsxs(x.Footer,{children:[l.jsx(x.PageSizeSelect,{value:String(h),onChange:p=>{E(p?Number(p):20)}}),i&&l.jsx(x.Metadata,{className:"mr-auto ml-5",...i}),l.jsx(x.Pagination,{className:"ml-auto",total:(o==null?void 0:o.last_page)||1,page:a,onChange:y})]})]})};export{ne as default};
