import{y as w,A as B,z as K,r as f,n as P,j as e,B as t,C as y,T as b,D as q,$ as F,w as j,v as R}from"./main-BMMRv56V.js";import{u as T}from"./useMutation-CKlbob41.js";import{u as A,a as v,D as L,c as M,S as N,v as O,b as Q,s as z,K as G,P as H,d as W,e as $,C as J}from"./sortable.esm-Cf71MVYe.js";import{c as C}from"./cuisines.api-cRv1I8z1.js";import{B as U}from"./ArrowBack-wNhJWsVJ.js";import{S as V}from"./Save-cfs8JpKk.js";function X({cuisine:r}){const{attributes:l,listeners:x,setNodeRef:u,transform:o,transition:d,isDragging:a}=W({id:r.id});return e.jsxs(R,{ref:u,style:{transform:J.Transform.toString(o),transition:d,opacity:a?.5:1},sx:{display:"flex",alignItems:"center",p:2,mb:1,cursor:a?"grabbing":"grab",border:a?"2px solid":"1px solid",borderColor:a?"primary.main":"divider","&:hover":{borderColor:"primary.light",bgcolor:"action.hover"}},...l,...x,children:[e.jsx($,{sx:{mr:2,color:"text.secondary"}}),e.jsxs(t,{sx:{flex:1,display:"flex",alignItems:"center",gap:2},children:[r.image_url?e.jsx(t,{component:"img",src:r.image_url,alt:r.name,sx:{width:32,height:32,borderRadius:1,objectFit:"cover"}}):e.jsx(t,{sx:{width:32,height:32,borderRadius:1,bgcolor:"#f0f0f1"}}),e.jsxs(t,{children:[e.jsx(b,{variant:"body2",fontWeight:500,children:r.name}),e.jsxs(b,{variant:"caption",color:"text.secondary",children:[r.recipe_count," recipes"]})]})]})]})}function oe(){const r=w(),{showSuccess:l,showError:x}=B(),u=K(),[o,d]=f.useState([]),[a,g]=f.useState(!1),S=A(v(H),v(G,{coordinateGetter:z})),{data:i,isLoading:I}=P({queryKey:j.cuisines.list({ordering:!0}),queryFn:()=>C.getList({sort_by:"sort_order",sort_order:"asc",per_page:500})});f.useEffect(()=>{var s;(s=i==null?void 0:i.data)!=null&&s.items&&(d(i.data.items),g(!1))},[i]);const m=T({mutationFn:s=>C.reorder(s),onSuccess:()=>{l("Order saved successfully"),g(!1),u.invalidateQueries({queryKey:j.cuisines.all})},onError:()=>x("Failed to save order")}),D=s=>{const{active:c,over:n}=s;n&&c.id!==n.id&&(d(p=>{const k=p.findIndex(h=>h.id===c.id),E=p.findIndex(h=>h.id===n.id);return Q(p,k,E)}),g(!0))},_=()=>{const s=o.map((c,n)=>({id:c.id,sort_order:n}));m.mutate(s)};return e.jsxs(t,{children:[e.jsxs(t,{sx:{display:"flex",alignItems:"center",gap:2,mb:3},children:[e.jsx(y,{startIcon:e.jsx(U,{}),onClick:()=>r("/cuisines"),variant:"text",children:"Back"}),e.jsx(b,{variant:"h4",children:"Reorder Cuisines"})]}),I?e.jsx(t,{sx:{display:"flex",justifyContent:"center",py:4},children:e.jsx(q,{})}):o.length===0?e.jsx(F,{severity:"info",children:"No cuisines to reorder"}):e.jsxs(e.Fragment,{children:[e.jsx(t,{sx:{mb:3},children:e.jsx(L,{sensors:S,collisionDetection:M,onDragEnd:D,children:e.jsx(N,{items:o.map(s=>s.id),strategy:O,children:o.map(s=>e.jsx(X,{cuisine:s},s.id))})})}),e.jsxs(t,{sx:{display:"flex",gap:2,position:"sticky",bottom:0,py:2,bgcolor:"background.default",borderTop:"1px solid",borderColor:"divider"},children:[e.jsx(y,{variant:"contained",startIcon:e.jsx(V,{}),onClick:_,disabled:!a||m.isPending,children:m.isPending?"Saving...":"Save Order"}),e.jsx(y,{variant:"outlined",onClick:()=>r("/cuisines"),children:"Cancel"})]})]})]})}export{oe as default};
