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