"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@babel/runtime/helpers/slicedToArray"),r=require("@babel/runtime/helpers/defineProperty"),t=require("@babel/runtime/helpers/asyncToGenerator"),n=require("@babel/runtime/regenerator"),u=require("react"),i=require("lodash"),s=require("../../../../utils/isAssetVisibleToUser.js"),a=require("../../../../hooks/useIsAdmin.js"),l=require("./useAssetsData.utils.js"),c=require("../../../../utils/doesWellPassFilters.js");function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var f=o(e),p=o(r),d=o(t),b=o(n);function y(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function h(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?y(Object(t),!0).forEach((function(r){p.default(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):y(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}var m=function(e,r){return"well"!==e.type||s.isAssetVisibleToUser(e,r)},g=function(){var e=d.default(b.default.mark((function e(r,t,n,u,s){var a,o,f,p,d,y,m,g,v;return b.default.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=r.filter((function(e){return"well"===e.type&&l.isFiniteNumber(e.redirectAssetId)})),o=i.uniq(t.map((function(e){return Number(e.asset_id)}))),e.next=4,l.fetchRigWellsMap(o,s);case 4:if(f=e.sent,p=r.map((function(e){if("rig"!==e.type)return e;var r=f.get(Number(e.asset_id))||[];return l.computeRigCheckedState(e,r,u)})),l.hasActiveWellFilters(n)){e.next=8;break}return e.abrupt("return",l.groupByType(p));case 8:return d=i.uniq(a.map((function(e){return Number(e.redirectAssetId)}))),e.next=11,l.fetchWellDataMap(d);case 11:return y=e.sent,m=new Set(d.filter((function(e){var r=y.get(e);return r&&c.doesWellPassFilters(r,n)}))),g=new Set(o.filter((function(e){return(f.get(e)||[]).some((function(e){return c.doesWellPassFilters(e,n,c.EXCLUDE_RIG_NAME_FILTER)}))}))),v=r.filter((function(e){return"well"!==e.type||m.has(Number(e.redirectAssetId))})).map((function(e){if("rig"!==e.type)return e;var r=Number(e.asset_id),t=f.get(r)||[];if(!g.has(r)){var i=0===t.length?"This rig has no wells":"No wells match current filters";return h(h({},e),{},{checked:!1,disabledReason:i})}var s=t.filter((function(e){return c.doesWellPassFilters(e,n,c.EXCLUDE_RIG_NAME_FILTER)}));return l.computeRigCheckedState(e,s,u)})),e.abrupt("return",l.groupByType(v));case 16:case"end":return e.stop()}}),e)})));return function(r,t,n,u,i){return e.apply(this,arguments)}}();exports.useAssetsFilter=function(e,r,t,n,i,s){var c,o=u.useState([]),p=f.default(o,2),y=p[0],v=p[1],w=u.useState(!1),O=f.default(w,2),j=O[0],A=O[1],F=a.useIsAdmin()&&null!==(c=null==n?void 0:n.showHiddenWells)&&void 0!==c&&c;return u.useEffect((function(){var u=!1,a=function(){var a=d.default(b.default.mark((function a(){var c,o,f,p,d,y,w;return b.default.wrap((function(a){for(;;)switch(a.prev=a.next){case 0:if(c=e.map((function(e){return h(h({},e),{},{checked:"rig"!==e.type&&t.includes(e.redirectAssetId)})})),o=null==n?void 0:n.rigName,f=Array.isArray(o)&&o.length>0,0!==(p=c.filter((function(e){return(!r||e.type===r)&&(!("rig"===e.type&&f&&!o.includes(e.name))&&!!m(e,F))}))).length){a.next=7;break}return u||(v([]),A(!1),s(0)),a.abrupt("return");case 7:if(0!==(d=p.filter((function(e){return"rig"===e.type&&l.isFiniteNumber(e.asset_id)}))).length||l.hasActiveWellFilters(n)){a.next=11;break}return u||(y=l.groupByType(p),v(y),A(!1),s(l.countFilteredItems(y))),a.abrupt("return");case 11:return A(!0),a.prev=12,a.next=15,g(p,d,n,t,i);case 15:w=a.sent,u||(v(w),s(l.countFilteredItems(w))),a.next=23;break;case 19:a.prev=19,a.t0=a.catch(12),console.error(a.t0),u||v([]);case 23:return a.prev=23,u||A(!1),a.finish(23);case 26:case"end":return a.stop()}}),a,null,[[12,19,23,26]])})));return function(){return a.apply(this,arguments)}}();return a(),function(){u=!0}}),[e,r,t,n,i,s,F]),{searchResult:y,isFiltering:j}};
//# sourceMappingURL=useAssetsFilter.js.map
