"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("../../../_virtual/jsx-runtime.js"),r=require("react"),de=require("../../../node_modules/react-popper/lib/esm/usePopper.js"),je=require("../../../hooks/useClickOutside.js"),Ee=require("../../../hooks/useDebounce.js"),W=require("../../../hooks/useKeyPress.js"),me=require("../../../icons/AlertCircle.js"),Re=require("../../../icons/Close.js"),we=require("../../../icons/Search.js"),ve=require("../../../utils/index.js"),Se=require("../dropdown/styles.js"),n=require("../styles.js"),ge=require("./components/SearchOptions.js"),R=require("./styles.js"),qe=require("./utils.js");function ye({variant:G,id:D,className:H,name:I,label:l,required:J,optionalText:N,errorText:b,assistText:w,value:j,placeholder:Q,width:X,hasError:v=!1,options:c,debounceDelay:Y=500,noResultsText:Z,onChange:L,onOpen:k,onClose:$,onFocus:A,onBlur:B,onTextChange:F,disabled:s=!1,maxHeight:T=300,touched:S,...g}){var z;const p=r.useRef(null),[ee,te]=r.useState(null),[re,se]=r.useState(null),[ne,ie]=r.useState(0),[E,q]=r.useState(!1),{containerRef:h}=je.useOnClickOutside(()=>q(!1)),[i,d]=r.useState(-1),[o,f]=r.useState(""),[y,K]=r.useState(j),[u,m]=r.useState(c),a=Ee.useDebounce(o,Y),M=W.useKeyPress("ArrowDown",p),V=W.useKeyPress("ArrowUp",p),U=W.useKeyPress("Enter",p),[P,oe]=r.useState(!1);r.useEffect(()=>{oe(typeof S>"u"?v:v&&S)},[v,S]);const{styles:ue,attributes:ae}=de.usePopper(ee,re,qe.popperOptions),C=()=>{s||(q(!0),k&&k())},le=()=>{s||(q(!1),$&&$())},_=e=>{e.label&&!e.isDisabled&&u.length>0&&(le(),f(e.label),K(e),L(e),d(-1))},ce=e=>{E||C();const x=e.target.value;x===""&&m(c),f(x),F&&F(x)},pe=()=>{!E&&o===""&&C(),A&&A()},fe=e=>{s||(e.preventDefault(),m(c),f(""),L(null),K(null),C())},xe=()=>{o!==""&&u.length>=1&&!y?f(u[0].label):y||f(""),B&&B()},he=()=>o.length>0||y!==null;r.useEffect(()=>{j&&f(j.label)},[j]),r.useEffect(()=>{h&&h.current&&ie(h.current.clientWidth)},[(z=h.current)==null?void 0:z.clientWidth]),r.useEffect(()=>{if(a&&a!==""){const e=c.filter(x=>a.length>1?x.label.toLowerCase().includes(a.toLowerCase()):x.label.toLowerCase().startsWith(a.toLowerCase()));m(e),d(0)}else m(c)},[a,c]),r.useEffect(()=>{if(u.length>0&&(M&&i<u.length-1&&d(i+1),V&&i>=1&&d(i-1),U&&i>=0)){const e=u[i];_(e)}},[M,V,U]);const O=ve.getId(g["data-testid"]);return t.jsxRuntimeExports.jsxs(n.Wrapper,{className:H,"data-testid":O("searchable-dropdown"),"data-trackid":g["data-trackid"],"aria-label":g["aria-label"]||l,ref:h,onClick:()=>{var e;return p&&((e=p.current)==null?void 0:e.focus())},width:X,children:[t.jsxRuntimeExports.jsxs(n.LabelWrapper,{disabled:s,hasLabel:!!(l!=null&&l.length),children:[l&&t.jsxRuntimeExports.jsx(n.Label,{htmlFor:D||`input-${I}`,disabled:s,children:l}),!J&&t.jsxRuntimeExports.jsx(n.Text,{children:N})]}),t.jsxRuntimeExports.jsxs(n.Border,{disabled:s,hasError:P,"aria-haspopup":"true","aria-expanded":E,ref:te,children:[t.jsxRuntimeExports.jsx(R.IconWrapper,{children:t.jsxRuntimeExports.jsx(we.default,{width:24,height:24})}),t.jsxRuntimeExports.jsx(R.InputWrapper,{children:t.jsxRuntimeExports.jsx(R.Input,{id:D||`input-${I}`,ref:p,type:"text","data-testid":O(),variant:G,disabled:s,placeholder:Q,"aria-haspopup":"true",autoComplete:"off",value:o,onChange:ce,onBlur:xe,onFocus:pe})}),he()&&t.jsxRuntimeExports.jsx(R.CloseWrapper,{disabled:s,onMouseDown:fe,children:t.jsxRuntimeExports.jsx(Re.default,{width:24,height:24})})]}),E&&!s&&t.jsxRuntimeExports.jsx(Se.OptionsPopper,{role:"listbox","data-testid":O("popper"),ref:se,width:ne,maxHeight:T,style:ue.popper,...ae.popper,children:t.jsxRuntimeExports.jsx(ge.SearchOptions,{searchOptions:u,noResultsText:Z,value:o===""?o:a,focusElement:i,setFocusElement:d,onClick:_})}),t.jsxRuntimeExports.jsxs(n.SupportingText,{children:[!!b&&P&&!s&&t.jsxRuntimeExports.jsxs(n.ErrorMessage,{children:[t.jsxRuntimeExports.jsx(me.default,{}),b]}),!P&&(typeof w=="string"?t.jsxRuntimeExports.jsx(n.AssistMessage,{children:w}):w)]})]})}exports.SearchableDropdown=ye;
