import{__rest as e,__assign as n,__spreadArray as r}from"tslib";import{jsxs as a,jsx as o}from"react/jsx-runtime";import{forwardRef as t,useState as l,useRef as i,useEffect as c,useMemo as s}from"react";import u from"../../styles/InputField.module.css.js";var d=t(function(t,d){var p=t.label,m=t.id,h=t.error,v=t.className,f=t.required,b=t.containerClassName,N=t.options,C=t.isMulti,y=void 0!==C&&C,g=t.isSearchable,A=void 0===g||g,k=t.value,I=t.onOptionChange,M=t.onChange,O=t.debounceTime,L=void 0===O?300:O,T=t.placeholder,w=t.disabled,S=t.onMenuScrollToBottom,x=t.isLoadingOptions,F=t.hasMoreOptions,j=void 0===F||F,B=e(t,["label","id","error","className","required","containerClassName","options","isMulti","isSearchable","value","onOptionChange","onChange","debounceTime","placeholder","disabled","onMenuScrollToBottom","isLoadingOptions","hasMoreOptions"]),W=l(!1),q=W[0],E=W[1],H=l(""),P=H[0],z=H[1],D=l(""),V=D[0],G=D[1],J=i(null),K=Array.isArray(N);c(function(){var e=setTimeout(function(){G(P)},L);return function(){return clearTimeout(e)}},[P,L]),c(function(){function e(e){J.current&&!J.current.contains(e.target)&&(E(!1),K&&!y&&k&&"object"==typeof k&&"label"in k?z(k.label):K&&!y&&z(""))}return document.addEventListener("mousedown",e),function(){return document.removeEventListener("mousedown",e)}},[K,y,k]),c(function(){K&&!y&&k&&"object"==typeof k&&"label"in k?z(k.label):!K||y||k||z("")},[k,K,y]);var Q=s(function(){return N?A&&V?N.filter(function(e){return e.label.toLowerCase().includes(V.toLowerCase())}):N:[]},[N,V,A]);return a("div",K?n({ref:J,className:"".concat(u.relativeWrapper," ").concat(b||"")},{children:[p&&a("label",n({className:u.label},{children:[f&&o("span",n({className:u.requiredAsterisk},{children:"*"})),p]})),a("div",n({className:"".concat(u.selectContainer," ").concat(w?u.selectContainerDisabled:u.selectContainerClickable," ").concat(h?u.selectContainerError:u.selectContainerSuccess," ").concat(v||""),onClick:function(){w||E(!q)}},{children:[a("div",n({className:u.selectedChips},{children:[y&&Array.isArray(k)&&k.map(function(e){return a("span",n({className:u.chip,onClick:function(e){return e.stopPropagation()}},{children:[e.label,!w&&o("button",n({type:"button",onClick:function(n){return function(e,n){if(e.stopPropagation(),y&&I){var r=Array.isArray(k)?k:[];I(r.filter(function(e){return e.value!==n}))}}(n,e.value)},className:u.chipCloseBtn},{children:"×"}))]}),e.value)}),o("input",{ref:d,type:"text",readOnly:!A||w,disabled:w,className:"".concat(u.selectInput," ").concat(A?"":u.caretTransparent),placeholder:y&&Array.isArray(k)&&k.length>0?"":T,value:P,onChange:function(e){A&&(z(e.target.value),E(!0))},onClick:function(){return!w&&E(!0)}})]})),!A&&o("div",n({className:u.chevronIconWrapper},{children:o("svg",n({className:"".concat(u.chevronIcon," ").concat(q?u.chevronIconOpen:""),fill:"none",viewBox:"0 0 24 24",stroke:"currentColor"},{children:o("path",{strokeLinecap:"round",strokeLinejoin:"round",strokeWidth:2,d:"M19 9l-7 7-7-7"})}))}))]})),q&&!w&&a("ul",n({onScroll:function(e){var n=e.currentTarget;n.scrollHeight-n.scrollTop-n.clientHeight<=20&&S&&!x&&S()},className:u.dropdownMenu},{children:[0===Q.length?x?null:o("li",n({className:u.noOptionsItem},{children:"No options found"})):Q.map(function(e){var t=y?Array.isArray(k)&&k.some(function(n){return n.value===e.value}):(null==k?void 0:k.value)===e.value;return o("li",n({onClick:function(n){n.stopPropagation(),function(e){if(I)if(y){var n=Array.isArray(k)?k:[],a=void 0;a=n.some(function(n){return n.value===e.value})?n.filter(function(n){return n.value!==e.value}):r(r([],n,!0),[e],!1),I(a),A&&z("")}else I(e),z(e.label),E(!1)}(e)},className:"".concat(u.optionItem," ").concat(t?u.optionItemSelected:"")},{children:a("div",n({className:u.optionContent},{children:[o("span",{children:e.label}),t&&o("span",{children:"✓"})]}))}),e.value)}),x&&o("li",n({className:u.loaderItem},{children:a("svg",n({className:u.spinIcon,fill:"none",viewBox:"0 0 24 24"},{children:[o("circle",{style:{opacity:.25},cx:"12",cy:"12",r:"10",stroke:"currentColor",strokeWidth:"4"}),o("path",{style:{opacity:.75},fill:"currentColor",d:"M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"})]}))})),!x&&!j&&Q.length>0&&o("li",n({className:u.endOfListItem},{children:"No more options"}))]})),h&&o("p",n({className:u.errorMessage},{children:h}))]}):n({className:"".concat(u.inputWrapper," ").concat(b||"")},{children:[p&&a("label",n({htmlFor:m,className:u.label},{children:[f&&o("span",n({className:u.requiredAsterisk},{children:"*"})),p]})),o("input",n({ref:d,id:m,value:k,onChange:M,disabled:w,className:"".concat(u.inputField," ").concat(h?u.inputFieldError:""," ").concat(w?u.inputFieldDisabled:""," ").concat(v||""),placeholder:T},B)),h&&o("p",n({className:u.errorMessage},{children:h}))]}))});d.displayName="InputField";export{d as default};
//# sourceMappingURL=InputField.js.map
