"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("lodash"),r=require("../Search/Search.js"),o=require("../../clients/jsonApi/index.js");require("../../utils/index.js");var l=require("../../effects/useLoadedData.js"),u=require("../../utils/jsonaDataFormatter.js");function d(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var c=d(i),p=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"}}})),f=function(e){var t;return{data:u.jsonaDataFormatter.deserialize(e.data),total:Number(null===(t=e.headers)||void 0===t?void 0:t.get("total"))||0}},m=[],g={},_={data:[],total:0},v=a.memo((function(i){var s=i.selectedAssets,u=void 0===s?m:s,d=i.onChange,v=i.label,b=i.className,h=i.type,x=i.customRequestQueryParams,j=i.classes,q=void 0===j?{}:j,S=p(),y=l({loadFn:o.mapAssetGetRequest(h,!0),initialData:_,getDataFromResponseFn:f,isCacheEnabled:!0}),C=y.isLoading,F=y.setIsLoading,D=y.data,A=y.loadData,O=D.data,P=D.total,I=a.useState(""),N=I[0],R=I[1];a.useEffect((function(){E("",x)}),[]);var k=a.useMemo((function(){return u.map((function(t){return e.__assign(e.__assign({},t),{id:t.id,label:t.name})}))}),[u]),z=a.useMemo((function(){var t={};return(k.length?O.concat(k):O).forEach((function(a){t[a.id]=e.__assign(e.__assign({},a),{id:a.id,label:a.name})})),Object.values(n.sortBy(t,"label"))}),[O,u,N]),E=a.useCallback((function(t,a){void 0===a&&(a={});var i=e.__assign(e.__assign(e.__assign(e.__assign({},g),{per_page:50}),a),{fields:["".concat(h,".name")].concat((null==a?void 0:a.fields)||[])});A(e.__assign({search:t},i))}),[]),H=function(){R(""),E("",x)},L=a.useMemo((function(){var e,a=0===(e=P-(null==O?void 0:O.length))?null:e;return a&&!C&&N?t.jsxs("div",{className:S.footer,children:[a," additional items not displayed. Continue typing for more refined results."]}):null}),[O,C,!!N,P]),M=q.listbox,w=q.paper,B=e.__rest(q,["listbox","paper"]);return t.jsx(r.Search,{value:k,onChange:function(t,a,i){if("reset"!==i){var s=a.map((function(t){return e.__assign(e.__assign({},t),{name:t.label})}));d(s)}},TextFieldProps:{label:v},className:b,classes:e.__assign({listbox:c.default(S.listbox,M),paper:c.default(S.paper,w)},B),loading:C,options:z,placeholder:"",multiple:!0,filterSelectedOptions:!1,AutocompleteProps:{forcePopupIcon:!0},isHiddenAllOption:!0,onInputChangeCallback:function(e,t){"reset"!==t&&""!==e?e.trimStart()!==N&&(R(e.trimStart()),F(!0),E(e.trimStart(),x)):H()},onInputReset:H,paperFooter:L,getOptionSelected:function(e,t){return+e.id==+t.id},isPrioritizedSelectedOptions:!0})}));module.exports=v;
