import{__assign as t,__rest as e}from'./../../ext-esm/tslib/tslib.es6.js';import{jsxs as a,jsx as o}from"react/jsx-runtime";import{memo as i,useState as r,useEffect as n,useMemo as l,useCallback as s}from"react";import d from"classnames";import{makeStyles as p}from"@material-ui/core";import c from"../Search/Search.js";import{mapAssetGetRequest as u}from"../../clients/jsonApi/index.js";import"../../utils/index.js";import m from"../../effects/useLoadedData.js";import{jsonaDataFormatter as f}from"../../utils/jsonaDataFormatter.js";var v=p((function(t){return{listbox:{maxHeight:360},paper:{width:364},footer:{fontSize:12,color:t.palette.primary.text6,lineHeight:"16.872px",padding:"8px 16px"}}})),b=function(t){var e;return{data:f.deserialize(t.data),total:Number(null===(e=t.headers)||void 0===e?void 0:e.get("total"))||0}},g=[],h={},x={data:[],total:0},j=i((function(i){var p=i.selectedAssets,f=void 0===p?g:p,j=i.onChange,S=i.label,C=i.className,F=i.type,y=i.customRequestQueryParams,D=i.classes,O=void 0===D?{}:D,P=v(),A=m({loadFn:u(F,!0),initialData:x,getDataFromResponseFn:b,isCacheEnabled:!0}),I=A.isLoading,N=A.setIsLoading,z=A.data,H=A.loadData,L=z.data,R=z.total,E=r(""),_=E[0],k=E[1];n((function(){Q("",y)}),[]);var q=l((function(){return f.map((function(e){return t(t({},e),{id:e.id,label:e.name})}))}),[f]),w=l((function(){var e={};return(q.length?L.concat(q):L).forEach((function(a){e[a.id]=t(t({},a),{id:a.id,label:a.name})})),Object.values(e)}),[L,f,_]),Q=s((function(e,a){void 0===a&&(a={});var o=t(t(t(t({},h),{per_page:50}),a),{fields:["".concat(F,".name")].concat((null==a?void 0:a.fields)||[])});H(t({search:e},o))}),[]),T=function(){k(""),Q("",y)},B=l((function(){var e,o=0===(e=R-(null==L?void 0:L.length))?null:e;return o&&!I&&_?a("div",t({className:P.footer},{children:[o," additional items not displayed. Continue typing for more refined results."]})):null}),[L,I,!!_,R]),G=O.listbox,J=O.paper,K=e(O,["listbox","paper"]);return o(c,{value:q,onChange:function(e,a,o){if("reset"!==o){var i=a.map((function(e){return t(t({},e),{name:e.label})}));j(i)}},TextFieldProps:{label:S},className:C,classes:t({listbox:d(P.listbox,G),paper:d(P.paper,J)},K),loading:I,options:w,placeholder:"",multiple:!0,filterSelectedOptions:!1,AutocompleteProps:{forcePopupIcon:!0},isHiddenAllOption:!0,onInputChangeCallback:function(t,e){"reset"!==e&&""!==t?t.trimStart()!==_&&(k(t.trimStart()),N(!0),Q(t.trimStart(),y)):T()},onInputReset:T,paperFooter:B,getOptionSelected:function(t,e){return+t.id==+e.id},isPrioritizedSelectedOptions:!0})}));export{j as default};
