"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@babel/runtime/helpers/asyncToGenerator"),r=require("@babel/runtime/regenerator"),t=require("react"),n=require("lodash"),i=require("../AdvancedSearch/constants.js"),s=require("../../../utils/doesWellPassFilters.js");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");var u=require("../../../api/wells/fetchWellsByRigIds.js");require("../../../constants.js"),require("../../../api/shared/dataFormatter.js"),require("@babel/runtime/helpers/slicedToArray"),require("../../../../../utils/index.js");var a=require("../../../../../utils/notificationToasts.js");function l(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var c=l(e),o=l(r);exports.NO_WELLS_MATCH_FILTERS_MESSAGE="No wells match current filters.",exports.useAdvancedSearchHandler=function(e){var r=e.filters,l=e.wells,f=e.maxOffsetWellNumber,d=e.includeHidden,h=e.fetchSearchedWells,b=e.setIsTableLoading,p=e.setSelectedWells,q=e.setLoadingAssetIds,m=e.setCurrentSelectionZone,x=e.markUserEdit,g=e.searchGenerationRef,v=t.useCallback(function(){var e=c.default(o.default.mark((function e(t,c,v,j){var k,S,w,A,E,y,I,_,C,R,T,L,N,W,F,G,H,M;return o.default.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(x(),m(null),k=g.current,S=function(){return g.current!==k},w=[],A=null==r?void 0:r.companyId,E=Array.isArray(A)&&A.length>0?A:null,y=v!==i.SEARCH_CATEGORIES.well.name?s.EXCLUDE_RIG_NAME_FILTER:void 0,I=function(e){return s.doesWellPassVisibilityAndFilters(e,r,d,y)},v!==i.SEARCH_CATEGORIES.well.name){e.next=13;break}w=c,e.next=40;break;case 13:if(j&&0!==j.length){e.next=15;break}return e.abrupt("return");case 15:return b(!0),e.prev=16,e.next=19,u.fetchWellsByRigIds(j,E);case 19:if(_=e.sent,!S()){e.next=22;break}return e.abrupt("return");case 22:if(!t){e.next=30;break}if(C=_.filter(I),0!==(w=C.map((function(e){return e.id}))).length){e.next=28;break}return a.showInfoNotification("No wells match current filters."),e.abrupt("return");case 28:e.next=31;break;case 30:w=_.map((function(e){return e.id}));case 31:e.next=37;break;case 33:return e.prev=33,e.t0=e.catch(16),console.error("Failed to fetch wells for rigs:",e.t0),e.abrupt("return");case 37:return e.prev=37,b(!1),e.finish(37);case 40:if(0!==w.length){e.next=42;break}return e.abrupt("return");case 42:if(R=new Set(w),T=(l||[]).filter((function(e){return R.has(e.id)&&(!t||I(e))})),L=[],!(t&&T.length<w.length)){e.next=63;break}if(N=new Set(T.map((function(e){return e.id}))),!((W=w.filter((function(e){return!N.has(e)}))).length>0)){e.next=63;break}return q((function(e){return e.concat(W)})),e.next=52,h(W);case 52:if(F=e.sent,G=new Set(W),H=function(e){var r=e.filter((function(e){return!G.has(e)}));return r.length===e.length?e:r},!S()){e.next=58;break}return q(H),e.abrupt("return");case 58:if(L=F.filter(I),q(H),0!==T.length||0!==L.length){e.next=63;break}return a.showInfoNotification("No wells match current filters."),e.abrupt("return");case 63:if(!S()){e.next=65;break}return e.abrupt("return");case 65:t?(M=T.concat(L),p((function(e){var r=new Set(e.map((function(e){return e.id}))),t=M.filter((function(e){return!r.has(e.id)})).slice(0,Math.max(0,f-e.length));return t.length>0?e.concat(t):e}))):p((function(e){return n.uniq(e.filter((function(e){var r=e.id;return!R.has(r)})))}));case 66:case"end":return e.stop()}}),e,null,[[16,33,37,40]])})));return function(r,t,n,i){return e.apply(this,arguments)}}(),[r,l,f,d,h,b,p,q,m,x,g]);return v};
//# sourceMappingURL=useAdvancedSearchHandler.js.map
