import{j as se}from"./vendor-mui-CBBEb17G.js";import{a as s}from"./vendor-react-BBazpZul.js";import{e as re,eU as ae,fo as ce,at as ne,J as o,gg as ie}from"./index-DdWwwZXu.js";import"./vendor-utils-BveVE6Uf.js";const N=s.createContext(void 0),le=({children:B})=>{const{officeId:f,clinicId:S,departmentID:m}=re(e=>e.doctorData),{GetSecretariesAsync:G,AddSecretaryAsync:_,GetSecretaryByIdAsync:P,EditSecretaryAsync:Q,ToggleSecretaryAsync:b,GetSecretaryByNationalCodeAsync:j}=ae(),{findUserByNationalCodeAsync:T}=ce(),{getSecretaryDepartmentAsync:E,addSecretaryDepartmentAsync:p,deleteSecretaryDepartmentAsync:U}=ie(),H=ne(),[I,v]=s.useState([]),[c,R]=s.useState(),[d,i]=s.useState(),[V,r]=s.useState(!1),[Y,g]=s.useState(!1),[A,u]=s.useState(!1),[$,C]=s.useState(!0),[y,D]=s.useState(),[q,n]=s.useState(),[J,L]=s.useState([]),O=s.useRef(),k=async e=>{try{if(r(!0),!c)return;const{message:t}=await p({departmentId:e,secretaryId:c.id});o.success(t),h()}finally{r(!1)}},z=async e=>{try{r(!0);const{message:t}=await U(e);o.success(t),h()}finally{r(!1)}},h=async()=>{try{if(r(!0),!c)return;const{data:e}=await E({secretaryId:c.id});e&&L(e)}finally{r(!1)}},F=async e=>{await b({id:e,clinicId:S,officeId:f}),o.success("تغییر وضعیت با موفقیت انجام شد"),w()},K=e=>{n(e)},M=e=>D(e),w=async()=>{r(!0);const e={clinicId:S?+S:null,officeId:f?+f:null};try{const{data:t}=await G(e);if(!t)return;t.sort((a,l)=>a.customId&&l.customId?a.customId-l.customId:-1),v(t)}finally{r(!1)}},x=()=>{D(""),i(void 0),n(void 0),u(!1),C(!0),w()},W=async e=>{const{data:t}=await _(e);t&&(await p({secretaryId:t.secretaryId,departmentId:m}),o.success("اطلاعات منشی با موفقیت ثبت شد"),x())},X=async e=>{if(!d){o.info("اطلاعات منشی را وارد نمایید");return}await Q({...e,id:d.id}),c||await p({secretaryId:d.id,departmentId:m}),o.success("اطلاعات منشی با موفقیت ویرایش شد"),x()},Z=async e=>{try{const{data:t}=await j(e);if(t){const{data:a}=await E({secretaryId:t.id});if(a!=null&&a.some(l=>l.departmentId===m))H({content:`این کد ملی قبلا به نام " ${t.firstName} ${t.lastName} " به عنوان منشی در این مرکز ثبت شده است . آیا ادامه می دهید؟`}).then(()=>(u(!0),i(t),R(t),n(t.picture),g(!0))).catch(()=>{});else return u(!0),i(t),n(t.picture),g(!0)}}catch{const a=await T(e,void 0);if(a)return i(a),n(a.picture),g(!0)}finally{C(!1)}},ee=async()=>{try{if(!c)return;r(!0);const{data:e}=await P(c.id);if(!e)return;i(e),n(e.picture),C(!1)}catch(e){console.log("ERROR_IN_GET_SECRETARY_BY_ID",e)}finally{r(!1)}};s.useEffect(()=>{h()},[c]),s.useEffect(()=>{if(y===void 0||!I)return;const e=I.filter(t=>[t.firstName,t.lastName,t.mobile,t.phoneNumber,t.nationalCode].includes(y));v(e)},[y]),s.useEffect(()=>{A&&ee()},[A]);const te={loading:V,secretarires:I,searchQuery:y,picture:q,isEdit:A,centers:J,isDisabled:$,secretary:d,secretariesComponentRef:O,isRegistered:Y,setPicture:n,setSearchQueryHandler:M,setSecretary:i,GetSecretariesAsyncReq:w,resetHandler:x,addSecretary:W,editSecretary:X,searchSecretaryByNationalCode:Z,addPicture:K,setIsEdit:u,setSelectedSecretary:R,toggleSecretary:F,selectedSecretary:c,handlers:{handleAddCenter:k,handleDeleteCenter:z}};return se.jsx(N.Provider,{value:te,children:B})},fe=()=>s.useContext(N);export{le as S,fe as u};
