import{__assign as t,__awaiter as e,__generator as r}from'./../ext-esm/tslib/tslib.es6.js';import{useState as n,useEffect as s}from"react";import{v4 as i}from"uuid";import{isEmpty as a,groupBy as u}from"lodash";import o from"../utils/resolveWellHubSlug.js";import{isDevOrQAEnv as l}from"../utils/env.js";import{getWellnessAlerts as d,getWellnessRuleSettings as c,getWCURuleMapping as m}from"../components/StatusBadge/api.js";import{isResolved as f,findUpdatedFields as v,hasOnlyLastCheckedAtKey as p,getIconType as g,getSortedWellnessAlerts as b,parseWellnessAlertFromJson as I,getNewWellnessAlerts as D,getLinkToDQPage as h}from"../components/StatusBadge/utils.js";import{ALERT_STATUSES as j,DATA_QUALITY_CHECK_PASSED as y,ALERT_STATES as S,ENABLING_ON_QA_APP_IDS_LIST as N,ENABLING_ON_PROD_APP_IDS_LIST as A,METADATA as O}from"../components/StatusBadge/constants.js";import{socketClient as R}from"../clients/index.js";var w=function(t,e,r){return void 0===r&&(r=""),{provider:O.recordProvider,dataset:e,assetId:t,event:r}};function _(t,e,r){var i=n([]),u=i[0],o=i[1];return s((function(){if(!a(t.filter((function(t){return t})))){var e=function(t){var e=t.data.map(I);o(e)},r=t.map((function(t){return R.subscribe(w(t,O.recordCollections.wellnessAlerts),{onDataReceive:e})})),n=t.map((function(t){return R.subscribe(w(t,O.recordCollections.wellnessAlerts,"update"),{onDataReceive:e})}));return function(){r.forEach((function(t){return t()})),n.forEach((function(t){return t()}))}}}),[JSON.stringify(t),e,r]),u}var B=function(O){var R=O.asset,w=O.multiRigAssets,B=void 0===w?[]:w,E=O.appId,T=O.dashboards,k=O.isDCApp,G=O.maximized,H=n({}),L=H[0],x=H[1],C=n(""),M=C[0],P=C[1],V=_(R?[R.id]:B.map((function(t){return t.id})),E,G);return s((function(){if(L.alertsData&&!a(V)){var e=!0,r=Object.keys(L.alertsData).reduce((function(r,n){var s;return t(t({},r),((s={})[n]=L.alertsData[n].map((function(r){var n=V.find((function(t){return(null==t?void 0:t.ruleId)===r.alert.ruleId&&(null==t?void 0:t.assetId)===r.alert.assetId}));if(!n)return r;if([j.FLAGGED,j.INVESTIGATION,j.ON_HOLD].includes(n.status))return r;var s=t(t({},n),{emptyMessage:f(n.status)&&y}),i=f(n.status);if(r.alert.id===n.id){var a=v(r.alert,n);return p(a)&&(e=!1),t(t({},r),{isResolved:i,alert:s})}return t(t({},r),{isResolved:i,issuesNum:n.state===S.issue?r.issuesNum+1:0,missingNum:n.state===S.missing?r.missingNum+1:0,alert:s})})),s))}),{}),n=Object.keys(r);P(g(n,r)),x((function(s){return t(t({},s),{alertsData:b(n,r),hasSubscriptionsData:e})}))}}),[V]),s((function(){var n=l?N:A,s={id:null==R?void 0:R.id,name:null==R?void 0:R.name},a=s.id?[s]:B;a.length&&(n.includes(E)||k)?function(n){return e(void 0,void 0,void 0,(function(){var e,s,a,l,f,v,p,S,N,A,O,R,w;return r(this,(function(r){switch(r.label){case 0:return[4,m(E)];case 1:return e=r.sent(),[4,c(null===(O=e[0])||void 0===O?void 0:O.rule_id)];case 2:return(s=r.sent()).length?[4,o(T)]:[2];case 3:return a=r.sent(),l=n.map((function(t){return t.id})),f=s.map((function(t){var e=t.data;return null==e?void 0:e.rule_id})),[4,d(l,f).then((function(t){return t.map((function(t){return I(t)})).filter(Boolean)}))];case 4:return v=r.sent(),p=Object.values(u(v,"ruleId")),S=D({assetsList:n,alertsGroupByRuleId:p,wellnessRuleSettings:s,wellHubSlug:a}),N=n.reduce((function(e,r){var n,u=r.id;return t(t({},e),((n={})[r.name]=s.map((function(t){var n,s=null===(n=e[r.name])||void 0===n?void 0:n.find((function(e){var r;return e.alert.ruleId===(null===(r=t.data)||void 0===r?void 0:r.rule_id)}));return s||{alert:{assetId:u,category:t.data.customer_category||t.data.category,status:j.RESOLVED,emptyMessage:y},id:i(),issuesNum:0,missingNum:0,isResolved:!0,name:t.data.customer_name||t.data.name,linkToDQPage:h({assetId:u,wellHubSlug:a})}})),n))}),t({},S)),A=Object.keys(N),P(g(A,N)),x({alertsData:b(A,N),lastTimestamp:null===(w=null===(R=N[A[0]][0])||void 0===R?void 0:R.alert)||void 0===w?void 0:w.statusUpdatedAt}),[2]}}))}))}(a).catch(console.error):(x({}),P(""))}),[null==R?void 0:R.id,B.length]),{wellnessAlerts:L,statusBadgeIconType:M,setWellnessAlerts:function(e){return x((function(r){return t(t({},r),e)}))}}};export{_ as useAlertsSubscription,B as useWellnessAlerts};
