"use strict";var e=require("@babel/runtime/helpers/objectWithoutProperties"),t=require("@babel/runtime/helpers/defineProperty"),r=require("@babel/runtime/helpers/slicedToArray"),n=require("react"),i=require("classnames"),a=require("@material-ui/core"),o=require("../Search/Search.js"),s=require("../../clients/jsonApi/index.js");require("../../utils/index.js");var u=require("../../effects/useLoadedData.js"),l=require("react/jsx-runtime"),c=require("../../utils/jsonaDataFormatter.js");function p(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var d=p(e),f=p(t),m=p(r),b=p(i),h=["listbox","paper"];function v(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 g(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?v(Object(r),!0).forEach((function(t){f.default(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):v(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}var j="hidden",O="",y=a.makeStyles((function(e){return{listbox:{maxHeight:360},paper:{width:364},footer:{fontSize:12,color:e.palette.primary.text6,lineHeight:"16.872px",padding:"8px 16px"}}})),x=function(e){return c.jsonaDataFormatter.deserialize(e)},P=[],q={},S=n.memo((function(e){var t=e.companyIds,r=e.selectedAssets,i=void 0===r?P:r,a=e.onChange,c=e.label,p=e.className,f=e.type,v=e.requestQueryParams,S=void 0===v?q:v,w=e.classes,D=void 0===w?{}:w,I=e.assetsPerPage,A=void 0===I?150:I,C=y(),F=u({loadFn:s.mapAssetGetRequest(f),initialData:[],getDataFromResponseFn:x}),E=F.isLoading,T=F.setIsLoading,k=F.data,H=F.loadData,L=n.useState(""),M=m.default(L,2),N=M[0],R=M[1],z=n.useMemo((function(){var e=((null==k?void 0:k.length)>50?null==k?void 0:k.slice(0,51):k)||[];return{visibleAssetIdsSet:new Set(e.map((function(e){return e.id}))),assetsToShow:e}}),[k]),B=z.visibleAssetIdsSet,G=z.assetsToShow,Q=n.useMemo((function(){var e=i.map((function(e){return e.id})),t=new Set(e),r=G.reduce((function(e,r){return t.has(r.id)||e.push(g(g({},r),{},{value:r.id,id:r.id,label:r.name,group:O})),e}),[]),n=i.reduce((function(e,t){return e.push(g(g({},t),{},{value:t.id,id:t.id,label:t.name,group:B.has(t.id)||!N?O:j})),e}),[]);return{selectedOptions:n,options:n.concat(r)}}),[k,i,!!N]),W=Q.options,_=Q.selectedOptions;n.useEffect((function(){H(g({company:null!=t&&t.length?t:void 0,per_page:A,search:N,fields:["".concat(f,".name")]},S))}),[t,N]);var J=n.useMemo((function(){var e,t=0===(e=k.length-G.length)?null:e;return t&&!E&&N?l.jsxs("div",{className:C.footer,children:[t," additional items not displayed. Continue typing for more refined results."]}):null}),[k,E,!!N]),K=D.listbox,U=D.paper,V=d.default(D,h);return l.jsx(o,{value:_,onChange:function(e,t,r){if("reset"!==r)if(N){var n={};t.forEach((function(e){var t=e.value,r=e.label;n[t]={id:t,name:r,group:O}})),i.forEach((function(e){B.has(e.id)||(n[e.id]=e)})),a(Object.values(n))}else{var o=t.map((function(e){return g(g({},e),{},{id:e.value,name:e.label,group:O})}));a(o)}},TextFieldProps:{label:c},className:p,classes:g({listbox:b.default(C.listbox,K),paper:b.default(C.paper,U)},V),loading:E,options:W,getIsInputChangeDisabled:function(e,t,r){return"reset"===r},placeholder:"",multiple:!0,filterSelectedOptions:!1,filterOptions:function(e){return e.filter((function(e){return"hidden"!==e.group}))},AutocompleteProps:{forcePopupIcon:!0},isHiddenAllOption:!0,onInputChangeCallback:function(e,t){"reset"!==t&&e.trim()!==N&&(R(e.trim()),T(!0))},onInputReset:function(){R(""),T(!0)},paperFooter:J,isClearInputOnBlur:!0})}));module.exports=S;
