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