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