"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@babel/runtime/helpers/asyncToGenerator"),r=require("@babel/runtime/helpers/slicedToArray"),t=require("@babel/runtime/regenerator"),n=require("react"),u=require("lodash"),c=require("./getSortedIdsSignature.js");function a(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var i=a(e),s=a(r),l=a(t);exports.useWellReconciliation=function(e){var r=e.subjectWell,t=e.wells,a=e.offsetSettings,o=e.open,f=e.isAllWellsLoaded,d=e.fetchSearchedWells,p=e.setSelectedWells,v=e.hasUserSelectionEditsRef,h=n.useRef(null),b=n.useRef(null),S=n.useRef(0),g=n.useState(!1),m=s.default(g,2),x=m[0],w=m[1],k=n.useMemo((function(){return(null==a?void 0:a.selectedWellIds)||[]}),[null==a?void 0:a.selectedWellIds]),y=n.useMemo((function(){return c.getSortedIdsSignature(k)}),[k]),q=n.useCallback(function(){var e=i.default(l.default.mark((function e(n){var a,i,s,o,f,h;return l.default.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n===S.current&&!v.current){e.next=2;break}return e.abrupt("return",!1);case 2:if(a=new Map((null==t?void 0:t.map((function(e){return[e.id,e]})))||[]),!((i=k.filter((function(e){return!a.has(e)}))).length>0)){e.next=13;break}if(s="".concat(y,"|").concat(c.getSortedIdsSignature(i)),b.current===s){e.next=12;break}return b.current=s,e.next=10,d(i);case 10:e.sent.forEach((function(e){return a.set(e.id,e)}));case 12:i=k.filter((function(e){return!a.has(e)}));case 13:if(n===S.current&&!v.current){e.next=15;break}return e.abrupt("return",!1);case 15:return o=new Set(k),f=k.map((function(e){return a.get(e)})).filter((function(e){return void 0!==e})),h=new Set(f.map((function(e){return e.id}))),p((function(e){var t=e.filter((function(e){return o.has(e.id)&&!h.has(e.id)})),n=f.concat(t);r&&(n=n.concat(r));var c=u.uniqBy(n,"id"),a=new Set(c.map((function(e){return e.id})));return e.length===a.size&&e.every((function(e){return a.has(e.id)}))?e:c})),e.abrupt("return",0===i.length);case 20:case"end":return e.stop()}}),e)})));return function(r){return e.apply(this,arguments)}}(),[k,y,d,r,t,v,p]);return n.useEffect((function(){if(!o)return v.current=!1,h.current=null,b.current=null,S.current+=1,void w(!1);if(f&&!v.current&&h.current!==y){w(!1);var e=S.current+1;S.current=e;var r=!1,t=function(){var t=i.default(l.default.mark((function t(){var n;return l.default.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,q(e);case 3:if(n=t.sent,!r&&S.current===e){t.next=6;break}return t.abrupt("return");case 6:n&&(h.current=y),t.next=12;break;case 9:t.prev=9,t.t0=t.catch(0),console.error("Failed to reconcile selected wells from offset settings:",t.t0);case 12:return t.prev=12,r||S.current!==e||w(!0),t.finish(12);case 15:case"end":return t.stop()}}),t,null,[[0,9,12,15]])})));return function(){return t.apply(this,arguments)}}();return t(),function(){r=!0}}}),[o,f,y,q,v]),{isReconciled:x}};
//# sourceMappingURL=useWellReconciliation.js.map
