import{r as l,j as e,K as Q,w as K,x as E,M as N,N as _,O as v,y as H,E as A,D as c,e as U,A as O,Q as R,R as W,F as I,S as $,U as V,V as w,W as J,X as L,c as T}from"./index-CGCOPtAd.js";import{c as X,I as q,T as Y,f as Z,a as ee,C as B,S as te,u as se,N as re,b as oe}from"./field-B5157bDc.js";const{useAppForm:ne}=X({fieldComponents:{RecentProjectSelect:ie,TextField:Y},formComponents:{ConfirmInitProjectDialog:ce,SubmitButton:te,CancelButton:B},fieldContext:Z,formContext:ee});function ae({projectReadOnly:n,closeDialog:a,isDialogOpen:o}){const[i,t]=l.useState(!1),[u,h]=l.useState(!0),[y,d]=l.useState(""),[k,P]=l.useState(""),[j,x]=l.useState(""),C=[403,404,409],b=({formReset:r})=>{d(""),P(""),x(""),r(),a()},p=K(),{mutate:z,isPending:M}=E({...W(),onSuccess:()=>{p.invalidateQueries({queryKey:I()}),p.invalidateQueries({queryKey:$()}),p.invalidateQueries({queryKey:V()}),p.invalidateQueries({queryKey:v()}),w(sessionStorage,J)},meta:{preventDefaultErrorHandling:C}}),{data:G}=N(_()),s=ne({defaultValues:{projectPath:"",recentProjectPath:""},onSubmit:({value:r,formApi:g})=>{const m=j==="recentProjectPath"?r.recentProjectPath:r.projectPath;z({body:{path:m}},{onSuccess:()=>{H.info(`Successfully set project ${m}`),b({formReset:g.reset})},onError:F=>{var D;const S=((D=F.response)==null?void 0:D.data).detail,f=F.status;if(f&&C.includes(f)){if(f===404&&S===`No .fmu directory found at ${m}`){t(!0);return}j==="recentProjectPath"?d(S):j==="projectPath"&&P(S),f===404&&S===`Path ${m} does not exist`&&(p.invalidateQueries({queryKey:v()}),j==="recentProjectPath"&&(g.resetField("recentProjectPath"),x("projectPath")))}}})}});return l.useEffect(()=>{P("")},[s.state.values.projectPath]),e.jsx(A,{open:o,$minWidth:"40em",children:e.jsxs("form",{onSubmit:r=>{r.preventDefault(),r.stopPropagation(),s.handleSubmit()},children:[e.jsx(c.Header,{children:e.jsx(c.Title,{children:"Select project"})}),e.jsxs(c.CustomContent,{children:[e.jsx(s.AppField,{name:"recentProjectPath",listeners:{onBlur:()=>{d("")},onChange:()=>{x("recentProjectPath"),s.handleSubmit()}},children:r=>e.jsx(r.RecentProjectSelect,{recentProjects:G.recent_project_directories,helperText:y})}),e.jsx(U,{}),e.jsx(s.AppField,{name:"projectPath",listeners:{onBlur:()=>{P("")},onChange:()=>{x("projectPath")}},children:r=>e.jsx(q,{color:"error",helperProps:{text:k,icon:e.jsx(O,{data:R,size:18})},children:e.jsx(r.TextField,{label:"Alternatively, enter a path to the project",setSubmitDisabled:h})})}),e.jsx(s.AppForm,{children:e.jsx(s.ConfirmInitProjectDialog,{isOpen:i,closeDialog:()=>{t(!1)},valueSource:j})})]}),e.jsx(c.Actions,{children:e.jsxs(s.AppForm,{children:[e.jsx(s.SubmitButton,{label:"Select",disabled:u||n,isPending:M,helperTextDisabled:n?"Project is read-only":void 0}),e.jsx(s.CancelButton,{onClick:()=>{b({formReset:s.reset})}})]})})]})})}function ie({recentProjects:n,helperText:a}){const o=se(),i=n.length===0;return e.jsx(q,{color:"error",helperProps:{text:a,icon:e.jsx(O,{data:R,size:18})},children:e.jsx(re,{label:"Select from recent projects",id:"recent-projects",size:5,disabled:i,value:i?[]:[o.state.value],onChange:t=>{o.handleChange(t.target.value)},onBlur:()=>{o.handleBlur()},multiple:!0,children:n.map(t=>e.jsx("option",{value:t,children:t},t))})})}function ce({isOpen:n,closeDialog:a,valueSource:o}){const i=K(),{mutate:t}=E({...L(),onSuccess:()=>{i.invalidateQueries({queryKey:I()}),i.invalidateQueries({queryKey:v()})}}),u=oe(),h=o==="projectPath"?u.store.state.values.projectPath:u.store.state.values.recentProjectPath,y=d=>{t({body:{path:d}},{onSuccess:()=>{a(),u.handleSubmit()},onError:()=>{a()}})};return e.jsxs(A,{open:n,children:[e.jsx(c.Header,{children:e.jsx(c.Title,{children:"Initialize project"})}),e.jsxs(c.CustomContent,{children:[e.jsxs(T,{bold:!0,children:[" ",h," "]}),e.jsxs(T,{children:["This project needs to be initialized to use FMU settings.",e.jsx("br",{}),"Would you like to initialize?"]})]}),e.jsxs(c.Actions,{children:[e.jsx(Q,{onClick:()=>{y(h)},children:"OK"}),e.jsx(B,{onClick:a})]})]})}function de({projectReadOnly:n}){const[a,o]=l.useState(!1),i=()=>{o(!0)},t=()=>{o(!1)};return e.jsxs(e.Fragment,{children:[e.jsx(Q,{onClick:i,children:"Select project"}),e.jsx(ae,{projectReadOnly:n,closeDialog:t,isDialogOpen:a})]})}export{de as P};
