import{t as f,d as $,ad as se,ae as re,j as e,V as G,_ as q,af as ie,r as M,P as k,v as P,E as U,G as R,F as H,ag as ae,ah as le,J as O,K as F,M as E,N as L,O as I,Q as J,ai as ce,S as X,U as _,X as Y,W as N,D as z,aj as de,ak as me,a9 as Q,al as he,x as ue,y as pe,am as K,an as ge,ao as xe,ap as fe,aq as je,a2 as be,ar as Se,as as ve,q as ye}from"./index-R8nKh668.js";import{C as Ce,L as _e}from"./common-DNCanLEj.js";import{b as D,d as Pe,c as ee,f as te,a as ne,S as $e}from"./field-DUWpRflK.js";import{g as oe}from"./model-DZaPEALd.js";import{i as we}from"./string-C0DRWuDM.js";import"./NativeSelect-BAMm44wb.js";const Re=$.div`
  margin-bottom: ${f.spacings.comfortable.medium};

  button + button {
    margin-left: ${f.spacings.comfortable.small} !important;
  }
`,Me=$(se)`
  max-height: ${({$maxHeight:t})=>t};
  pointer-events: ${({$disablePointerEvents:t})=>t?"none":"auto"};

  display: flex;
  flex-direction: column;
`,Ee=$.div.attrs(({$numStratColumns:t})=>({style:{gridTemplateColumns:`minmax(max-content, 2fr) repeat(${t}, 3fr)`}}))`
  display: grid;
  font-weight: ${f.typography.table.cell_header.fontWeight};
  font-size: ${f.typography.table.cell_text.fontSize};

  div {
    padding: ${f.spacings.comfortable.x_small} ${f.spacings.comfortable.small};

    &:nth-child(2) {    
      display: flex;
      justify-content: center;
      grid-column: 2 / -1;
    }
  }
`,Ie=$(re).attrs(t=>({style:{gridTemplateRows:`repeat(${t.$numRows}, 14px)`,gridTemplateColumns:`minmax(max-content, 2fr) repeat(${t.$numStratColumns}, 3fr)`}}))`
  display: grid;
  overflow: auto;
  padding: ${f.spacings.comfortable.medium} ${f.spacings.comfortable.small};
`,Te=$.div`
  padding: ${f.spacings.comfortable.x_small};

  button {
    width: 100%;
    height: 100%;
    display: block;

    background-color: ${f.colors.infographic.primary__moss_green_21.rgba}; 
    &:hover {
      background-color: ${f.colors.infographic.primary__moss_green_34.rgba};
    }

    &.unselected {
      background-color: ${f.colors.interactive.disabled__fill.rgba};
      &:hover {
        background-color: ${f.colors.infographic.primary__moss_green_21.rgba};
      }
    }
  }
`,ke=$.div`
  grid-column: 1;

  button {
    height: 100%;  
    width: 100%;
    padding: ${f.spacings.comfortable.x_small};
    background-color: ${f.colors.ui.background__default.hex};

    span {
      justify-content: flex-start;
    }

    &:disabled:not(.orphan) {    
      background-color: ${f.colors.ui.background__default.hex};
      color: ${f.colors.text.static_icons__default.hex};
    }
    &.unselected:not(:hover) {
      color: ${f.colors.interactive.disabled__text.hex};
    }
  }
`,Fe=$.div`
  grid-column: 1 / -1;
  border-bottom: 1px ${f.colors.ui.background__overlay.hex};
  border-bottom-style: ${({$lineStyle:t})=>t};
`;function A(t,n){return t.findIndex(o=>o.name===n)}function Ae(t,n){return t.sort((o,i)=>{const d=A(n,o.name),a=A(n,i.name);return d-a})}const T=(t,n)=>t.filter(o=>!n.find(i=>i.name===o.name)).map(o=>o.name);function ze(t,n,o,i){const d=D();return{removeItems:(m,h)=>{const b=new Set(h),s=(m==="horizons"?t:n).filter(x=>!b.has(x.name));d.setFieldValue(m,s)},addItems:(m,h)=>{const b=m==="horizons"?o:i,j=m==="horizons"?t:n,s=new Set(j.map(r=>r.name)),x=new Map(b.map(r=>[r.name,r])),y=h.filter(r=>!s.has(r)).flatMap(r=>x.get(r)??[]);if(y.length){const r=Ae([...j,...y],b);d.setFieldValue(m,r)}},addAll:()=>{d.setFieldValue("horizons",o),d.setFieldValue("zones",i)},removeAll:()=>{d.setFieldValue("horizons",[]),d.setFieldValue("zones",[])}}}function He(t,n){const i=new Map;t.forEach(a=>{const l=A(n,a.top_horizon_name),g=A(n,a.base_horizon_name),p=[];for(let m=l;m<g;m++)p.push(m);i.set(a.name,{horizonIndices:p,rowStart:l,rowEnd:g,gridColumn:1})});const d=new Map;return Array.from(i.values()).sort((a,l)=>a.horizonIndices.length-l.horizonIndices.length).forEach(a=>{let l=1,g=!1;for(;!g;){d.has(l)||d.set(l,new Set);const p=d.get(l);if(!p)throw new Error("Invalid stratigraphy grid state");a.horizonIndices.some(h=>p.has(h))?l++:(a.horizonIndices.forEach(h=>{p.add(h)}),a.gridColumn=l,g=!0)}}),i}function Be(t,n){return e.jsxs(e.Fragment,{children:[t.name,e.jsx("br",{}),"Top: ",t.top_horizon_name,e.jsx("br",{}),"Base: ",t.base_horizon_name,e.jsx("br",{}),n?"Zone does not exist in RMS":""]})}function Oe(t,n,o){return e.jsxs(e.Fragment,{children:[t.name,e.jsx("br",{}),"Type: ",t.type,e.jsx("br",{}),n?"Horizon does not exist in RMS":o?"Horizon is used by one or more zones":""]})}function B({horizons:t,zones:n,unselectedHorizonNames:o,unselectedZoneNames:i,orphanHorizonNames:d,orphanZoneNames:a,onZoneClick:l,onHorizonClick:g,maxHeight:p}){const m=l??g,h=He(n,t),b=Math.max(1,t.length*2),j=Math.max(1,...Array.from(h.values(),c=>c.gridColumn)),s=new Set(d),x=new Set(a),y=new Set(o),r=new Set(i),S=new Set(n.filter(c=>!r.has(c.name)).flatMap(c=>[c.top_horizon_name,c.base_horizon_name]));return e.jsxs(Me,{$disablePointerEvents:!m,$maxHeight:p,children:[e.jsxs(Ee,{$numStratColumns:j,children:[e.jsx("div",{children:"Horizons"}),e.jsx("div",{children:"Zones"})]}),e.jsxs(Ie,{$numStratColumns:j,$numRows:b,children:[t.map((c,C)=>{const u=C*2+1,v=s.has(c.name),w=S.has(c.name),V=y.has(c.name);return e.jsxs(e.Fragment,{children:[e.jsx(ke,{style:{gridRow:`${u} / span 2`},children:e.jsx(G,{title:Oe(c,v,w),children:e.jsx(q,{className:v?"orphan":V?"unselected":"",onClick:()=>g==null?void 0:g(c,V),variant:"ghost",color:v?"danger":"primary",disabled:w&&!v,children:c.name})})}),e.jsx(Fe,{$lineStyle:c.type.startsWith("interpreted")?"solid":"dashed",style:{gridRow:u}})]},c.name)}),n.map(c=>{const C=h.get(c.name);if(!C)return null;const u=x.has(c.name),v=r.has(c.name);return e.jsx(Te,{style:{gridRow:`${C.rowStart*2+2} / ${C.rowEnd*2+2}`,gridColumn:C.gridColumn+1},children:e.jsx(G,{title:Be(c,u),children:e.jsx(q,{className:u?"orphan":v?"unselected":"",onClick:()=>l==null?void 0:l(c,v),variant:"ghost",color:u?"danger":"secondary",children:c.name})})},c.name)})]})]})}const Ne=$.div`
  display: grid;
  grid-template-columns: 1fr 1fr;
  column-gap: ${f.spacings.comfortable.xx_large};

  > div {
    overflow: auto;
  }

  h4 {
    text-decoration: underline;
  }
`,W=$.div`
  margin-bottom: ${f.spacings.comfortable.medium};

  button + button {
    margin-left: ${f.spacings.comfortable.small} !important;
  }
`,Ve=$(ie)`
  margin-top: ${f.spacings.comfortable.medium};
`,{useAppForm:Ge}=ee({fieldContext:te,formContext:ne,fieldComponents:{},formComponents:{StratigraphyEditor:Ke,CancelButton:N,SubmitButton:Y}});function qe({setConfirmAction:t,onConfirm:n,confirmAction:o}){const i=()=>{t("")};return e.jsxs(de,{open:!!o,$minWidth:"25em",children:[e.jsx(_.Header,{children:"Confirm action"}),e.jsxs(_.CustomContent,{children:[e.jsx(P,{children:o==="add"?"This will add all available stratigraphy to the project.":"This will remove all stratigraphy from the project."}),e.jsx(P,{$marginBottom:"0",children:"Do you want to continue? "})]}),e.jsxs(_.Actions,{children:[e.jsx(R,{label:"Ok",onClick:()=>{n(),i()}}),e.jsx(N,{onClick:i})]})]})}function Qe({orphanZoneNames:t,orphanHorizonNames:n}){return e.jsxs(Ve,{children:[e.jsx(P,{children:"There are horizons or zones in the project stratigraphy that are no longer available in RMS. They need to be removed before data can be saved."}),e.jsxs(z,{children:[n.length>0&&e.jsx(z.Item,{children:n.join(", ")}),t.length>0&&e.jsx(z.Item,{children:t.join(", ")})]})]})}function Ke({availableHorizons:t,availableZones:n}){const o=D(),[i,d]=M.useState(""),a=o.getFieldValue("horizons"),l=o.getFieldValue("zones"),{removeItems:g,addItems:p,addAll:m,removeAll:h}=ze(a,l,t,n),b=T(n,l),j=T(t,a),s=u=>{p("horizons",[u.name])},x=u=>{p("zones",[u.name]),p("horizons",[u.top_horizon_name,u.base_horizon_name])},y=u=>{g("horizons",[u.name]);const v=l.filter(w=>w.top_horizon_name===u.name||w.base_horizon_name===u.name).map(w=>w.name);g("zones",v)},r=u=>{g("zones",[u.name])},S=T(l,n),c=T(a,t),C=c.length>0||S.length>0;return M.useEffect(()=>{o.setErrorMap({onChange:C?["Orphan horizons or zones present"]:void 0})},[o,C]),e.jsxs(Ne,{children:[e.jsxs("div",{children:[e.jsx(k,{$variant:"h4",children:"Project stratigraphy"}),e.jsx(B,{maxHeight:"55vh",horizons:a,zones:l,orphanHorizonNames:c,orphanZoneNames:S,onZoneClick:u=>{r(u)},onHorizonClick:u=>{y(u)}}),C&&e.jsx(Qe,{orphanZoneNames:S,orphanHorizonNames:c}),e.jsx(W,{children:e.jsx(R,{label:"Remove all",variant:"outlined",disabled:!a.length&&!l.length,onClick:()=>{d("remove")}})})]}),e.jsxs("div",{children:[e.jsx(k,{$variant:"h4",children:"Available RMS stratigraphy"}),e.jsx(B,{maxHeight:"55vh",horizons:t,zones:n,unselectedHorizonNames:j,unselectedZoneNames:b,onZoneClick:(u,v)=>{v?x(u):r(u)},onHorizonClick:(u,v)=>{v?s(u):y(u)}}),e.jsx(W,{children:e.jsx(R,{variant:"outlined",label:"Add all",disabled:a.length===t.length&&l.length===n.length,onClick:()=>{d("add")}})}),e.jsx(P,{children:"💡 Click on horizons or zones to add or remove them from the project stratigraphy."})]}),e.jsx(qe,{confirmAction:i,setConfirmAction:d,onConfirm:i==="add"?m:h})]})}function We({projectHorizons:t,projectZones:n,projectReadOnly:o,isDialogOpen:i,closeDialog:d,isRmsProjectOpen:a}){const{data:l}=H({...ae(),enabled:a}),{data:g}=H({...le(),enabled:a}),p=O(),m=F({...ce(),onSuccess:()=>{p.refetchQueries({queryKey:J()})},onError:r=>{var S;if(((S=r.response)==null?void 0:S.status)===E){const c=L(r);console.error(c),I.error(c)}},meta:{errorPrefix:"Error updating project stratigraphy",preventDefaultErrorHandling:[E]}}),h=Ge({defaultValues:{zones:n,horizons:t},onSubmit:({value:r,formApi:S})=>{o||b({formValue:r,formSubmitCallback:j,formReset:S.reset})}}),b=({formValue:r,formSubmitCallback:S,formReset:c})=>{m.mutate({body:r},{onSuccess:C=>{S({message:C.message,formReset:c}),d()}})},j=({message:r,formReset:S})=>{I.info(r),S()},{confirmCloseDialogOpen:s,handleCloseRequest:x,handleConfirmCloseDecision:y}=Pe({formContext:h,isOpen:i,closeDialog:d,isReadOnly:o});return e.jsxs(e.Fragment,{children:[e.jsx(Ce,{isOpen:s,handleConfirmCloseDecision:y}),e.jsx(X,{open:i,isDismissable:!0,onClose:x,$minWidth:"60em",$maxWidth:"",children:e.jsxs("form",{onSubmit:r=>{r.preventDefault(),r.stopPropagation(),h.handleSubmit()},children:[e.jsx(_.Header,{children:"Set project stratigraphy"}),e.jsx(_.CustomContent,{children:e.jsx(h.AppForm,{children:e.jsx(h.Subscribe,{selector:r=>r.values,children:()=>e.jsx(h.StratigraphyEditor,{availableHorizons:l??[],availableZones:g??[]})})})}),e.jsxs(_.Actions,{children:[e.jsx(h.Subscribe,{selector:r=>[r.isDefaultValue,r.canSubmit],children:([r,S])=>e.jsx(h.SubmitButton,{label:"Save",disabled:r||!S||o,isPending:m.isPending,helperTextDisabled:o?"Project is read-only":"Form can be submitted when the values have changed"})}),e.jsx(h.CancelButton,{onClick:r=>{r.preventDefault(),x()}})]})]})})]})}function Ze({rmsData:t,projectReadOnly:n,isRmsProjectOpen:o}){const[i,d]=M.useState(!1),a=(t==null?void 0:t.horizons)??[],l=(t==null?void 0:t.zones)??[],g=()=>{d(!1)},p=()=>{d(!0)};return e.jsxs(e.Fragment,{children:[e.jsx(k,{$variant:"h3",children:"Stratigraphy"}),e.jsx(P,{children:"The following is the model stratigraphy stored in the project, this can be a subset or the full RMS stratigraphy. It is only the stored stratigraphy that will be possible to map to official stratigraphic names."}),a.length?e.jsx(B,{horizons:a,zones:l}):e.jsx(U,{children:"No stratigraphy information currently stored in the project."}),e.jsx(R,{label:a.length?"Edit":"Add",disabled:n||!o,tooltipText:n?"Project is read-only":o?void 0:"RMS project is not open",onClick:p}),e.jsx(We,{projectHorizons:a,projectZones:l,projectReadOnly:n,isDialogOpen:i,closeDialog:g,isRmsProjectOpen:o})]})}const{useAppForm:Ue}=ee({fieldComponents:{Select:$e},formComponents:{SubmitButton:Y,CancelButton:N},fieldContext:te,formContext:ne});function Z(t){return{label:oe(t),value:t}}function Le({rmsData:t,isDialogOpen:n,setIsDialogOpen:o}){const i=({formReset:s})=>{s(),o(!1)},d=O(),{data:a,isPending:l}=H(Se()),{mutate:g,isPending:p}=F({...ve(),onSuccess:()=>{d.invalidateQueries({queryKey:J()})},onError:s=>{var x;if(((x=s.response)==null?void 0:x.status)===E){const y=L(s);console.error(y),I.error(y)}},meta:{errorPrefix:"Error saving the RMS project",preventDefaultErrorHandling:[E]}}),m=Ue({defaultValues:{rmsPath:(t==null?void 0:t.path)??""},onSubmit:({value:s,formApi:x})=>{g({body:{path:s.rmsPath}},{onSuccess:()=>{I.info("Successfully set the RMS project"),i({formReset:x.reset})}})}}),h=(a==null?void 0:a.results)??[];function b(s){return h.some(x=>x.path===s)}const j=h.map(s=>Z(s.path));return t?b(t.path)||j.unshift(Z(t.path)):j.unshift({label:"(none)",value:""}),e.jsx(X,{open:n,$minWidth:"32em",children:e.jsxs("form",{onSubmit:s=>{s.preventDefault(),s.stopPropagation(),m.handleSubmit()},children:[e.jsx(_.Header,{children:e.jsx(_.Title,{children:"RMS project"})}),e.jsx(_.Content,{children:e.jsx(m.AppField,{name:"rmsPath",validators:{onMount:()=>h.length===0?"Could not detect any RMS projects in the rms/model directory":t&&!b(t.path)?"Selected project does not exist":void 0,onChange:({value:s})=>b(s)?void 0:"Selected project does not exist"},children:s=>e.jsx(s.Select,{label:"RMS project",value:s.state.value,options:j,loadingOptions:l,onChange:x=>{s.handleChange(x)}})})}),e.jsxs(_.Actions,{children:[e.jsx(m.Subscribe,{selector:s=>[s.isDefaultValue,s.canSubmit],children:([s,x])=>e.jsx(m.SubmitButton,{label:"Save",disabled:s||!x,isPending:p,helperTextDisabled:"Value can be submitted when it has been changed and is valid"})}),e.jsx(m.CancelButton,{onClick:s=>{s.preventDefault(),i({formReset:m.reset})}})]})]})})}function Je({rmsData:t}){return e.jsx(pe,{children:e.jsx("table",{children:e.jsxs("tbody",{children:[e.jsxs("tr",{children:[e.jsx("th",{children:"Project"}),e.jsx("td",{children:oe(t.path)})]}),e.jsxs("tr",{children:[e.jsx("th",{children:"Version"}),e.jsx("td",{children:t.version})]})]})})})}function Xe({rmsData:t,projectReadOnly:n,setIsRmsProjectOpen:o,isRmsProjectOpen:i}){const d=O(),[a,l]=M.useState(!1),g=t&&we(t.version,K)?K:void 0,p=F({...fe(),onSuccess:()=>{o(!0),d.invalidateQueries({queryKey:ge()}),d.invalidateQueries({queryKey:xe()})},onError:(h,b)=>{var j,s;if(o(!1),(j=b.body)!=null&&j.version){p.mutate({});return}if(((s=h.response)==null?void 0:s.status)===E){const x=h.response.data.detail;console.error(x),I.error(x)}},meta:{errorPrefix:"Error opening the RMS project",preventDefaultErrorHandling:[E]}}),m=F({...je(),onSuccess:()=>{o(!1)},meta:{errorPrefix:"Error closing the RMS project"}});return e.jsxs(e.Fragment,{children:[(t==null?void 0:t.path)&&(p.isPending?e.jsx(P,{children:"⏳ Opening the RMS project. This might take a while..."}):i?e.jsx(P,{children:"✅ The RMS project is open and data is accessible"}):e.jsx(P,{children:"⛔ The RMS project needs to be opened to access data"})),e.jsxs(Re,{children:[e.jsx(R,{label:t!=null&&t.path?"Change project":"Select project",disabled:!!n||i||p.isPending,tooltipText:n?"Project is read-only":i?"Close the RMS project to select a new one":"",onClick:()=>{d.invalidateQueries({queryKey:be()}),l(!0)}}),(t==null?void 0:t.path)&&e.jsxs(e.Fragment,{children:[e.jsx(R,{label:i?"Reload project":"Open project",isPending:p.isPending,disabled:m.isPending,variant:i?"outlined":"contained",onClick:()=>{p.mutate({body:g?{version:g}:void 0})}}),i&&!p.isPending&&e.jsx(R,{label:"Close project",variant:"outlined",isPending:m.isPending,onClick:()=>{m.mutate({})}})]})]}),e.jsx(Le,{rmsData:t,isDialogOpen:a,setIsDialogOpen:l})]})}function Ye({rmsData:t,projectReadOnly:n}){const[o,i]=M.useState(me(sessionStorage,Q,"boolean"));return M.useEffect(()=>{he(sessionStorage,Q,o)},[o]),e.jsxs(e.Fragment,{children:[e.jsxs(P,{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(Je,{rmsData:t}):e.jsx(U,{children:"No RMS project information found in the project."}),e.jsx(Xe,{rmsData:t,projectReadOnly:n,setIsRmsProjectOpen:i,isRmsProjectOpen:o}),e.jsx(ue,{}),e.jsx(Ze,{rmsData:t,projectReadOnly:n,isRmsProjectOpen:o})]})}function De(){var n;const t=ye();return t.status&&t.data?e.jsx(Ye,{rmsData:t.data.config.rms,projectReadOnly:!(((n=t.lockStatus)==null?void 0:n.is_lock_acquired)??!1)}):e.jsx(P,{children:"Project not set."})}const it=function(){return e.jsxs(e.Fragment,{children:[e.jsx(k,{children:"RMS"}),e.jsx(M.Suspense,{fallback:e.jsx(_e,{}),children:e.jsx(De,{})})]})};export{it as component};
