import{c as M,j as t,r as x,B as w,T as q,I as z,C as E,ab as L,ac as B,y as $,z as F,A as R,n as V}from"./main-BMMRv56V.js";import{C as A}from"./Close-Bxz3dk_v.js";import{u as I}from"./useMutation-CKlbob41.js";const P=M(t.jsx("path",{d:"M19 7v2.99s-1.99.01-2 0V7h-3s.01-1.99 0-2h3V2h2v3h3v2zm-3 4V8h-3V5H5c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h12c1.1 0 2-.9 2-2v-8zM5 19l3-4 2 3 3-4 4 5z"})),_=({imageId:u,imageUrl:d,onChange:o,label:s="Image"})=>{const a=x.useCallback(()=>{var n;if(!((n=window.wp)!=null&&n.media))return;const l=window.wp.media({title:`Select ${s}`,button:{text:"Use this image"},multiple:!1,library:{type:"image"}});l.on("select",()=>{var m;const i=l.state().get("selection").first().toJSON(),r=i.sizes;o(i.id,((m=r==null?void 0:r.medium)==null?void 0:m.url)||i.url)}),l.open()},[s,o]),p=x.useCallback(()=>{o(0,"")},[o]);return t.jsxs(w,{children:[t.jsx(q,{variant:"body2",color:"text.secondary",sx:{mb:.5},children:s}),d?t.jsxs(w,{sx:{position:"relative",display:"inline-block"},children:[t.jsx(w,{component:"img",src:d,alt:s,sx:{width:120,height:120,objectFit:"cover",borderRadius:1,border:"1px solid",borderColor:"divider"}}),t.jsx(z,{size:"small",onClick:p,sx:{position:"absolute",top:-8,right:-8,bgcolor:"error.main",color:"white","&:hover":{bgcolor:"error.dark"},width:24,height:24},children:t.jsx(A,{sx:{fontSize:14}})})]}):t.jsxs(E,{variant:"outlined",startIcon:t.jsx(P,{}),onClick:a,sx:{borderStyle:"dashed"},children:["Select ",s]})]})};function H({basePath:u,allQueryKey:d,detailQueryKeyFn:o,apiFns:s,entityName:a}){const{id:p}=L(),l=B(),n=$(),i=F(),{showSuccess:r,showError:m}=R(),b=l.pathname.endsWith("/new"),e=p?parseInt(p,10):null,h=e!==null&&!b,{data:g,isLoading:f}=V({queryKey:e?o(e):["_noop"],queryFn:()=>s.getById(e),enabled:h&&e!==null}),j=(g==null?void 0:g.data)??null,v=I({mutationFn:c=>s.create(c),onSuccess:()=>{r(`${a} created successfully`),i.invalidateQueries({queryKey:d}),n(u)},onError:()=>m(`Failed to create ${a.toLowerCase()}`)}),y=I({mutationFn:c=>s.update(e,c),onSuccess:()=>{r(`${a} updated successfully`),i.invalidateQueries({queryKey:d}),e&&i.invalidateQueries({queryKey:o(e)}),n(u)},onError:()=>m(`Failed to update ${a.toLowerCase()}`)}),C=v.isPending||y.isPending,S=x.useCallback(()=>{n(u)},[n,u]),k=x.useCallback(c=>{h&&e?y.mutate(c):v.mutate(c)},[h,e,v,y]);return{isEditMode:h,entityId:e,entity:j,isLoading:h&&f,isSaving:C,handleClose:S,handleSave:k}}export{P as A,_ as I,H as u};
