import{j as s,c as w,S as C}from"./vendor-mui-CBBEb17G.js";import{a as i}from"./vendor-react-BBazpZul.js";import{aO as D,v as E,by as f,aP as F,r as q,x as K,cA as U,J as d,ga as k}from"./index-IP_FWtso.js";const M=({categoryId:c,onClose:n,selectedItem:r,parentId:l,onSubmit:t})=>{const{control:m,handleSubmit:g,formState:{errors:y}}=D({defaultValues:{name:(r==null?void 0:r.name)??"",categoryId:c}}),{addGeneralHealthInfoGroupAsync:x,updateGeneralHealthInfoGroupAsync:p}=k(),h=new U,[j,o]=i.useState(!1),[G,S]=i.useState([]),A=async()=>{try{o(!0);const{data:a}=await h.getGeneralHealthInfoCategoryAsync();a&&S(a.map(e=>({label:e.name,value:e.id.toString()})))}finally{o(!1)}},b=async a=>{try{if(o(!0),r){const{message:H}=await p({id:r.id,name:a.name,categoryId:a.categoryId,parentId:l});d.success(H),t&&t(r.id,a.name),n();return}const{message:e,data:u}=await x({name:a.name,categoryId:c,parentId:l});d.success(e),u&&t&&t(u.id,a.name),n()}finally{o(!1)}};return i.useEffect(()=>{r&&A()},[r]),s.jsxs(E,{onClose:n,title:r?"ویرایش گروه":"گروه جدید",maxWidth:"xs",children:[s.jsx(w,{className:"m-1 !p-2",children:s.jsxs(C,{component:"form",spacing:1,children:[s.jsx(f,{name:"name",control:m,rules:{required:!0},render:({field:{onChange:a,value:e}})=>s.jsx(F,{value:e,onChange:a,error:!!y.name,label:"عنوان"})}),r&&s.jsx(f,{name:"categoryId",control:m,rules:{required:!0},render:({field:{onChange:a,value:e}})=>s.jsx(q,{items:G,value:e,onchange:a,label:"دسته بندی"})})]})}),s.jsx(K,{onClose:n,onSubmit:g(b),loading:j})]})};export{M as A};
