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