import e from"@babel/runtime/helpers/asyncToGenerator";import t from"@babel/runtime/helpers/slicedToArray";import r from"@babel/runtime/helpers/defineProperty";import n from"@babel/runtime/helpers/toConsumableArray";import a from"@babel/runtime/regenerator";import{useState as s,useEffect as o,useRef as i}from"react";import{isEmpty as p}from"lodash";import{PER_PAGE as c}from"../constants.js";import"uuid";import"lodash/set";import"lodash/get";import"lodash/unset";import"moment";import{mapAssetGetRequest as u,getAssets as l}from"../../../clients/jsonApi/index.js";import"../../../clients/subscriptions.js";import"../../../clients/api/apiCore.js";import"../../../clients/constants.js";import"../../../clients/sso/auth0.js";import"../../../clients/sso/oauth2.js";function m(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function f(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?m(Object(n),!0).forEach((function(t){r(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):m(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}var d=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[];return{sort:"name",order:"asc",fields:["".concat(e,".name"),"".concat(e,".asset_id")].concat(n(t))}},y=function(e){var t,r,n;return f(f({},e.attributes),{},{id:+e.id,programId:+(null===(t=e.relationships)||void 0===t||null===(r=t.program)||void 0===r||null===(n=r.data)||void 0===n?void 0:n.id)})};function b(n){var i=n.appKey,p=n.assetType,c=n.assetId,l=n.parentAssetType,m=n.parentAssetId,b=n.queryParams,h=void 0===b?null:b,v=s([]),g=t(v,2),j=g[0],O=g[1],x=s(!0),w=t(x,2),P=w[0],k=w[1];return o((function(){function t(){return(t=e(a.mark((function e(){var t;return a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,u(p)(f(f(f({},d(p)),{},r({per_page:1e4},l,m),i&&{app_key:i}),h));case 2:t=e.sent,O(t.data.map(y)),k(!1);case 5:case"end":return e.stop()}}),e)})))).apply(this,arguments)}k(!0),function(){t.apply(this,arguments)}()}),[p,c,l,m,i]),{assets:j,loading:P}}function h(r){var n=r.appKey,m=r.assetType,b=r.companyId,h=r.currentValue,v=r.isMultiple,g=r.parentAssetId,j=r.parentAssetType,O=r.scrollPage,x=r.searchText,w=r.setCurrentOption,P=r.queryParams,k=r.assetFields,A=s([]),T=t(A,2),I=T[0],_=T[1],D=s(!1),C=t(D,2),q=C[0],E=C[1],K=i(),S=i();return o((function(){function t(){return(t=e(a.mark((function e(){var t;return a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,u(m)(f(f({},d(m,k)),{},{ids:v?h:[h]},P));case 2:if(t=e.sent,!p(t.data)){e.next=7;break}return e.next=6,l(f(f({},d(m,k)),{},{ids:v?h:[h]},P));case 6:t=e.sent;case 7:w((function(){if(v)return t.data.map(y);var e=t.data[0];return e?y(e):null}));case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}(v?h.length:h)&&function(){t.apply(this,arguments)}()}),[]),o((function(){function t(){return(t=e(a.mark((function e(){var t,r,s,o,i;return a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,E(!0),t={per_page:c,page:O},j&&g&&(t[j]=g),b&&(t.company=b),x&&(t.search=x),n&&(t.app_key=n),r=u(m)(f(f(f({},d(m,k)),t),P)),S.current=r,e.next=11,r;case 11:if(s=e.sent,S.current===r){e.next=14;break}return e.abrupt("return");case 14:o=s.data.map(y),i=K.current!==m||1===O,_(i?o:function(e){return e.concat(o)}),K.current=m,e.next=23;break;case 20:e.prev=20,e.t0=e.catch(0),console.error(e.t0);case 23:E(!1);case 24:case"end":return e.stop()}}),e,null,[[0,20]])})))).apply(this,arguments)}!function(){t.apply(this,arguments)}()}),[m,j,g,b,x,O,n]),{assets:I,loading:q}}export{h as useAssetAutoCompleteSelectData,b as useAssetSelectData};
