import t from"@babel/runtime/helpers/asyncToGenerator";import e from"@babel/runtime/regenerator";import"lodash";import"@babel/runtime/helpers/toConsumableArray";import"@babel/runtime/helpers/defineProperty";import"uuid";import"lodash/set";import"lodash/get";import"lodash/unset";import"moment";import"../../../../clients/jsonApi/index.js";import"../../../../clients/subscriptions/constants.js";import"../../../../clients/subscriptions.v1.js";import"../../../../clients/subscriptions/subscriptions-client.factory.js";import"../../../../clients/api/apiCore.js";import"../../../../clients/constants.js";import"../../../../clients/sso/auth0.js";import"../../../../clients/sso/oauth2.js";import"../../../../clients/api/rateLimiter.js";import"../../constants.js";import"../../api/shared/dataFormatter.js";import"@babel/runtime/helpers/slicedToArray";import{resyncIdasWells as r}from"../../api/external-offsets/resyncIdasWells.js";import{IdasResyncStatus as n}from"./idas-resync.types.js";import{getIdasResyncChannel as s}from"./idasResyncChannel.js";import{subscribeToIdasResync as i}from"./subscribeToIdasResync.js";import"../../../../utils/index.js";import{showErrorNotification as o,showSuccessNotification as a}from"../../../../utils/notificationToasts.js";var u={multiline:!0,autoHideDuration:1e4},c=function(){},l=function(l){var p,f=null,m=null,d=!1,v={assetId:null,currentUser:void 0,loadExternalOffsets:(p=t(e.mark((function t(){return e.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.abrupt("return",void 0);case 1:case"end":return t.stop()}}),t)}))),function(){return p.apply(this,arguments)})},h=function(t){var e;l.setState({status:t,isResyncInFlight:(e=t,e===n.TRIGGERED||e===n.RUNNING)})},b=function(){var t;null===(t=m)||void 0===t||t(),m=null},y=function(){f=null},j=function(){b(),y()},I=function(t){d&&(o(t,u),h(n.FAILED))},x=function(t,e){d&&t===f&&(y(),a(e,u),h(n.SUCCEEDED))},g=function(){var r=t(e.mark((function t(r,n){return e.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,v.loadExternalOffsets();case 3:return t.prev=3,x(r,n),t.finish(3);case 6:case"end":return t.stop()}}),t,null,[[0,,3,6]])})));return function(t,e){return r.apply(this,arguments)}}(),R=function(t,e){t===f&&(j(),I(e||"Re-sync wells from IDAS failed"))},E=function(){var n=t(e.mark((function t(n){var s,i;return e.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(null!=(s=v.currentUser)&&s.id){t.next=4;break}return R(n,void 0),t.abrupt("return");case 4:return t.prev=4,t.next=7,r(s,n);case 7:"failed"===(null==(i=t.sent)?void 0:i.state)&&R(n,i.fail_reason),t.next=14;break;case 11:t.prev=11,t.t0=t.catch(4),R(n,t.t0 instanceof Error?t.t0.message:void 0);case 14:case"end":return t.stop()}}),t,null,[[4,11]])})));return function(t){return n.apply(this,arguments)}}(),w=function(){var t=v,e=t.assetId,r=t.currentUser;if(e&&null!=r&&r.id&&null===f){var o=e;f=o;var a=s(o);m=i(a,(function(t){return function(t,e){d&&t===f&&(b(),"success"===(null==e?void 0:e.status)?g(t,e.message):(y(),I((null==e?void 0:e.message)||"Re-sync wells from IDAS failed")))}(o,t)})),h(n.RUNNING),E(o)}};return{activate:function(){d=!0,l.setState({triggerResync:w})},deactivate:function(){d=!1,j(),l.setState({triggerResync:c})},update:function(t){null!==f&&t.assetId!==f&&l.getState().isResyncInFlight&&(j(),h(n.INITIAL)),v=t}}};export{l as createIdasResyncController};
//# sourceMappingURL=createIdasResyncController.js.map
