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