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