"use strict";(globalThis.webpackChunk_openmrs_esm_patient_conditions_app=globalThis.webpackChunk_openmrs_esm_patient_conditions_app||[]).push([[3595],{13595:(e,t,n)=>{n.r(t),n.d(t,{default:()=>h});var r=n(1343),a=n.n(r),i=n(53373),o=n.n(i),l=n(72339),s=n(80824),u=n(81160),c=n(46195),d=n(11920),m=n(14863),f=n(24145),p=n(87087),b=n(5238);function v(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}function g(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function y(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var n=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=n){var r,a,i=[],o=!0,l=!1;try{for(n=n.call(e);!(o=(r=n.next()).done)&&(i.push(r.value),!t||i.length!==t);o=!0);}catch(e){l=!0,a=e}finally{try{o||null==n.return||n.return()}finally{if(l)throw a}}return i}}(e,t)||function(e,t){if(e){if("string"==typeof e)return v(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(n):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?v(e,t):void 0}}(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}const h=function(e){var t,n,i,v=e.closeWorkspace,h=e.groupProps.patientUuid,C=e.workspaceProps,E=C.condition,S=C.formContext,A=(0,l.useTranslation)().t,T="tablet"===(0,m.useLayoutType)(),k=(0,f.AS)(h).conditions,w=y((0,r.useState)(!1),2),D=w[0],N=w[1],_=y((0,r.useState)(null),2),j=_[0],U=_[1],O=y((0,r.useState)(null),2),z=O[0],F=O[1],I="editing"===S,W=null==k?void 0:k.find((function(e){return(null==e?void 0:e.id)===(null==E?void 0:E.id)})),q=function(e,t){var n="creating"===e,r=c.z.string().refine((function(e){return!n||!!e}),{message:t("clinicalStatusRequired","A clinical status is required")}),a=c.z.string().refine((function(e){return!n||!!e}),{message:t("conditionRequired","A condition is required")});return c.z.object({abatementDateTime:c.z.date().optional().nullable(),clinicalStatus:r,conditionName:a,onsetDateTime:c.z.date().nullable().refine((function(e){return e<=new Date}),{message:t("onsetDateCannotBeInTheFuture","Onset date cannot be in the future")})})}(S,A),$={abatementDateTime:I&&(null==W?void 0:W.abatementDateTime)?new Date(null==W?void 0:W.abatementDateTime):null,conditionName:"",clinicalStatus:I&&null!==(n=null==W||null===(t=W.clinicalStatus)||void 0===t?void 0:t.toLowerCase())&&void 0!==n?n:"",onsetDateTime:I&&(null==W?void 0:W.onsetDateTime)?new Date(null==W?void 0:W.onsetDateTime):null},P=(0,s.mN)({mode:"all",resolver:(0,u.u)(q),defaultValues:$}),R=P.formState.isDirty,x=(0,r.useCallback)((function(){v({discardUnsavedChanges:!0})}),[v]);return a().createElement(m.Workspace2,{title:A("recordCondition","Record condition"),hasUnsavedChanges:R},a().createElement(s.Op,P,a().createElement(d.lVW,{className:b.A.form,onSubmit:P.handleSubmit((function(){N(!0)}),(function(){return N(!1)}))},a().createElement(p.default,{closeWorkspaceWithSavedChanges:x,conditionToEdit:E,isEditing:I,isSubmittingForm:D,patientUuid:h,setErrorCreating:U,setErrorUpdating:F,setIsSubmittingForm:N}),a().createElement("div",null,j?a().createElement("div",{className:b.A.errorContainer},a().createElement(d.jeF,{className:b.A.error,role:"alert",kind:"error",lowContrast:!0,title:A("errorCreatingCondition","Error creating condition"),subtitle:null==j?void 0:j.message})):null,z?a().createElement("div",{className:b.A.errorContainer},a().createElement(d.jeF,{className:b.A.error,role:"alert",kind:"error",lowContrast:!0,title:A("errorUpdatingCondition","Error updating condition"),subtitle:null==z?void 0:z.message})):null,a().createElement(d.Te$,{className:o()((i={},g(i,b.A.tablet,T),g(i,b.A.desktop,!T),i))},a().createElement(d.$nd,{className:b.A.button,kind:"secondary",onClick:function(){return v()}},A("cancel","Cancel")),a().createElement(d.$nd,{className:b.A.button,disabled:D,kind:"primary",type:"submit"},D?a().createElement(d.OuH,{className:b.A.spinner,description:A("saving","Saving")+"..."}):a().createElement("span",null,A("saveAndClose","Save & close"))))))))}}}]);