import{t as b,d as y,r as p,_ as O,W as C,$ as A,j as e,c as d,v as F,I as k,x as S,a0 as _,a1 as B,w as N,a2 as w,a3 as H,H as R,a4 as G,C as I,y as v,F as V,E as Q,D as j,a as $,u as q}from"./index-CGCOPtAd.js";import{L}from"./common-D7_rxYMs.js";import{G as f,c as K,d as U,f as W,a as J,C as z,S as X}from"./field-B5157bDc.js";import{g as E}from"./model-kGPbuna_.js";const Y=y.div`
  margin-bottom: ${b.spacings.comfortable.medium};

  button + button {
    margin-left: ${b.spacings.comfortable.small};
  }
`,{useAppForm:Z}=K({fieldComponents:{Select:U},formComponents:{SubmitButton:X,CancelButton:z},fieldContext:W,formContext:J});function M(t){return{label:E(t),value:t}}function D({rmsData:t,isDialogOpen:r,setIsDialogOpen:i}){const n=({formReset:o})=>{o(),i(!1)},h=N(),{data:a,isPending:c}=w(H()),{mutate:u,isPending:T}=S({...G(),onSuccess:()=>{h.invalidateQueries({queryKey:V()})},onError:o=>{var s;if(((s=o.response)==null?void 0:s.status)===R){const P=I(o);console.error(P),v.error(P)}},meta:{errorPrefix:"Error saving the RMS project",preventDefaultErrorHandling:[R]}}),l=Z({defaultValues:{rmsPath:(t==null?void 0:t.path)??""},onSubmit:({value:o,formApi:s})=>{u({body:{path:o.rmsPath}},{onSuccess:()=>{v.info("Successfully set the RMS project"),n({formReset:s.reset})}})}}),x=(a==null?void 0:a.results)??[];function m(o){return x.some(s=>s.path===o)}const g=x.map(o=>M(o.path));return t?m(t.path)||g.unshift(M(t.path)):g.unshift({label:"(none)",value:""}),e.jsx(Q,{open:r,$minWidth:"32em",children:e.jsxs("form",{onSubmit:o=>{o.preventDefault(),o.stopPropagation(),l.handleSubmit()},children:[e.jsx(j.Header,{children:e.jsx(j.Title,{children:"RMS project"})}),e.jsx(j.Content,{children:e.jsx(l.AppField,{name:"rmsPath",validators:{onMount:()=>x.length===0?"Could not detect any RMS projects in the rms/model directory":t&&!m(t.path)?"Selected project does not exist":void 0,onChange:({value:o})=>m(o)?void 0:"Selected project does not exist"},children:o=>e.jsx(o.Select,{label:"RMS project",value:o.state.value,options:g,loadingOptions:c,onChange:s=>{o.handleChange(s)}})})}),e.jsxs(j.Actions,{children:[e.jsx(l.Subscribe,{selector:o=>[o.isDefaultValue,o.canSubmit],children:([o,s])=>e.jsx(l.SubmitButton,{label:"Save",disabled:o||!s,isPending:T,helperTextDisabled:"Value can be submitted when it has been changed and is valid"})}),e.jsx(l.CancelButton,{onClick:o=>{o.preventDefault(),n({formReset:l.reset})}})]})]})})}function ee({rmsData:t}){return e.jsx(k,{children:e.jsx("table",{children:e.jsxs("tbody",{children:[e.jsxs("tr",{children:[e.jsx("th",{children:"Project"}),e.jsx("td",{children:E(t.path)})]}),e.jsxs("tr",{children:[e.jsx("th",{children:"Version"}),e.jsx("td",{children:t.version})]})]})})})}function te({rmsData:t,projectReadOnly:r,setIsRmsProjectOpen:i,isRmsProjectOpen:n}){const[h,a]=p.useState(!1),c=S({..._(),onSuccess:()=>{i(!0)},onError:()=>{i(!1)},meta:{errorPrefix:"Error opening the RMS project"}}),u=S({...B(),onSuccess:()=>{i(!1)},meta:{errorPrefix:"Error closing the RMS project"}});return e.jsxs(e.Fragment,{children:[(t==null?void 0:t.path)&&(c.isPending?e.jsx(d,{children:"⏳ Opening the RMS project. This might take a while..."}):n?e.jsx(d,{children:"✅ The RMS project is open and data is accessible"}):e.jsx(d,{children:"⛔ The RMS project needs to be opened to access data"})),e.jsxs(Y,{children:[e.jsx(f,{label:"Select project",disabled:!!r||n,tooltipText:r?"Project is read-only":n?"Close the RMS project to select a new one":"",onClick:()=>{a(!0)}}),(t==null?void 0:t.path)&&e.jsxs(e.Fragment,{children:[e.jsx(f,{label:n?"Reload project":"Open project",isPending:c.isPending,variant:n?"outlined":"contained",onClick:()=>{c.mutate({})}}),n&&!c.isPending&&e.jsx(f,{label:"Close project",variant:"outlined",isPending:u.isPending,onClick:()=>{u.mutate({})}})]})]}),e.jsx(D,{rmsData:t,isDialogOpen:h,setIsDialogOpen:a})]})}function oe({rmsData:t,projectReadOnly:r}){const[i,n]=p.useState(O(sessionStorage,C,"boolean"));return p.useEffect(()=>{A(sessionStorage,C,i)},[i]),e.jsxs(e.Fragment,{children:[e.jsxs(d,{children:["The following is the main RMS project located in the ",e.jsx("i",{children:"rms/model"})," ","directory. The version is detected automatically:"]}),t?e.jsx(ee,{rmsData:t}):e.jsx(F,{children:"No RMS project information found in the project."}),e.jsx(te,{rmsData:t,projectReadOnly:r,setIsRmsProjectOpen:n,isRmsProjectOpen:i})]})}function ne(){var r;const t=q();return t.status&&t.data?e.jsx(oe,{rmsData:t.data.config.rms,projectReadOnly:!(((r=t.lockStatus)==null?void 0:r.is_lock_acquired)??!1)}):e.jsx(d,{children:"Project not set."})}const ce=function(){return e.jsxs(e.Fragment,{children:[e.jsx($,{children:"RMS"}),e.jsx(p.Suspense,{fallback:e.jsx(L,{}),children:e.jsx(ne,{})})]})};export{ce as component};
