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