import{r,j as s}from"./vendor-react-wBh_31cN.js";import{a as O}from"./input-DFI7JniT.js";import{u as z,j as G}from"./vendor-redux-CJZrIb6x.js";import{$ as _,G as T,r as k}from"./index-dNe6i2rS.js";import{y as F}from"./alertManagerConfUtil-D4yp8Ca8.js";import{u as M,j as P,s as $}from"./alertUtil-rW93jnda.js";import{u as B}from"./useAlertProjectConfig-DHF-bkkD.js";import{C as I}from"./callout-BFSXVeMz.js";import{C as D,a as H,b as V,e as Y,c as L}from"./card-BeQtvmcJ.js";import{C as N}from"./combobox-QqI9aNup.js";import{I as Q}from"./icon-button-BZeFL7T7.js";import{aQ as X,Y as q,az as J}from"./vendor-icons-BUUmD6ht.js";import{c as K,u as U}from"./vendor-router-D1GqFWiT.js";const W=[{label:"Alert",value:"alert"},{label:"Credentials",value:"credentials"}],Z=({receivers:i,severityOptions:g,type:t,entityType:c,index:o,onRemove:A,alert:n,setAlert:l,showPassToAgent:p=!1})=>{const S=M()(`validation-${c}`),[E,y]=r.useState(),[d,j]=r.useState(),[v,R]=r.useState();r.useEffect(()=>{R(n.status),y(n.severity),j(n.receiver)},[n]);const f=r.useCallback(e=>a=>{l({...n,[e]:a},o)},[n,o,l]),w=r.useCallback(e=>{const a=e;l({...n,alertMode:a,passToAgent:a!=="alert"},o)},[n,o,l]),m=n.alertMode??"alert",b=m!=="credentials";return s.jsxs("div",{className:"mt-1.5 flex items-center gap-2",children:[p&&s.jsx(N,{className:"w-[180px]",label:"Purpose",options:W,value:m,onChange:e=>e&&w(e)}),b&&s.jsxs(s.Fragment,{children:[s.jsx(N,{className:"w-[330px]",label:"Trigger on",options:(t==="job"?P:S).map(e=>({label:e,value:e})),value:v,onChange:e=>e&&f("status")(e)}),s.jsx(X,{className:"size-4 shrink-0 text-muted-foreground"}),s.jsx(N,{className:"w-[230px]",label:"Severity",options:(g||[]).map(e=>({label:e,value:e})),value:E,onChange:e=>e&&f("severity")(e)})]}),s.jsx(N,{className:"flex-[4_0]",label:"Receiver",options:(i||[]).map(e=>({label:e,value:e})),value:d,disabled:!i||i.length<1,noResultsText:"You have to setup receivers first",onChange:e=>e&&f("receiver")(e)}),s.jsx(Q,{onClick:()=>{A(o)},"aria-label":"Remove",children:s.jsx(q,{className:"w-4 h-4"})})]})},he=({type:i,entityType:g="fg",alerts:t,setAlerts:c,error:o,isAgentJob:A=!1,className:n})=>{const{id:l}=K(),p=z($),[S,E]=r.useState([]),y=M()(`validation-${g}`),{canAddGlobalEvent:d}=B(),j=U(),{isAdmin:v}=_(),R=Object.keys(F).filter(e=>Number.isNaN(Number(e))),f=r.useCallback((e,a)=>{const u=t.map((h,x)=>x===a?e:h);c([...u])},[t,c]),w=()=>(t?t.filter(a=>{var u,h,x,C;return a.alertMode==="credentials"?((u=a.receiver)==null?void 0:u.length)>1:((h=a.status)==null?void 0:h.length)>1&&((x=a.severity)==null?void 0:x.length)>1&&((C=a.receiver)==null?void 0:C.length)>1}):[]).length===t.length,m=G();r.useEffect(()=>{m.alerts.fetch(+l),m.alerts.fetchReceivers(+l)},[m.alerts,l]),r.useEffect(()=>{var e;E((e=p.receivers)==null?void 0:e.map(a=>a.name))},[p]);const b=i==="job"?P.length:y.length;return s.jsxs(D,{className:n,children:[s.jsxs(H,{children:[s.jsx(V,{children:"Alerts"}),v&&d&&s.jsx(Y,{children:s.jsx(T,{variant:"outline",size:"sm",onClick:()=>{j(k.settings.alerts.list)},children:"Go to alert configuration"})})]}),s.jsxs(L,{children:[!v&&!d&&s.jsx("div",{className:"flex flex-row",children:s.jsx(I,{variant:"neutral",children:"To add alerts, a Hops Admin has to configure alerts channels first."})}),v&&!d&&s.jsx("div",{className:"flex flex-row",children:s.jsx(I,{variant:"neutral",children:s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("span",{children:"To add alerts, you need to configure alert channels first"}),s.jsx(T,{variant:"secondary",onClick:()=>{j(k.settings.alerts.list)},children:"Go to alerts configuration"})]})})}),d&&s.jsxs(s.Fragment,{children:[t&&t.length>0&&s.jsx("div",{children:s.jsxs("div",{className:"flex flex-col p-2.5 border border-border rounded-lg",children:[t.map((e,a)=>s.jsx(Z,{index:a,receivers:S,severityOptions:R,type:i,entityType:g,showPassToAgent:A,onRemove:u=>{if(t.length>0){const h=t.filter((x,C)=>C!==u);c(h)}},alert:e,setAlert:f},a)),o&&s.jsx(O,{intent:"error",children:o})]})}),s.jsxs(T,{variant:"outline",size:"sm",className:"mt-1.5",onClick:()=>{t.length<b&&c([...t,{}])},disabled:t.length>=b||!w(),children:[s.jsx(J,{className:"w-4 h-4"}),t&&t.length>0?"Add another alert":"Add alert"]})]})]})]})};export{he as A};
