"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("../../../_virtual/jsx-runtime.js"),r=require("react"),ce=require("../../../node_modules/react-popper/lib/esm/usePopper.js"),pe=require("../../../hooks/useClickOutside.js"),de=require("../../../hooks/useKeyPress.js"),xe=require("../../../icons/AlertCircle.js"),fe=require("../../../icons/ChevronDown.js"),q=require("../../../utils/index.js"),o=require("../styles.js"),p=require("./styles.js"),he=require("./components/MultiValueList.js"),je=require("../checkbox/Checkbox.js");function me({variant:_,id:h,name:O,label:u,required:U,placeholder:g,hasError:j=!1,errorText:E,optionalText:z,prefix:C,assistText:R,options:D,onOpen:H,onClose:L,onFocus:M,onBlur:G,disabled:a=!1,maxHeight:J=300,touched:S,width:N,noMargin:B=!1,...m}){var K;const d=r.useRef(null),[n,Q]=r.useState(null),[X,Y]=r.useState(null),[Z,ee]=r.useState(0),[x,v]=r.useState(!1),[b,W]=r.useState(null),[k,te]=r.useState(!1),{value:s,multivalue:i,onChange:f}=m,[F,se]=r.useState(n==null?void 0:n.clientHeight);r.useEffect(()=>{te(typeof S>"u"?j:j&&S)},[j,S]);const{styles:ie,attributes:re,forceUpdate:T}=ce.usePopper(n,X,{placement:"bottom",modifiers:[{name:"offset",options:{offset:[0,2]}},{name:"preventOverflow",options:{padding:{top:8}}},{name:"flip",options:{padding:{top:8}}}]}),{containerRef:l}=pe.useOnClickOutside(()=>v(!1)),V=de.useKeyPress("Space",l),ne=()=>{a||(v(!0),H&&H())},y=()=>{a||(v(!1),W(null),L&&L())},ue=()=>x?y():ne();r.useEffect(()=>{d&&d.current&&(x?(d.current.focus(),M&&M()):d.current.blur())},[x]),r.useEffect(()=>{l&&l.current&&ee(l.current.clientWidth)},[(K=l.current)==null?void 0:K.clientWidth]);const $=e=>i?s.find(P=>P.value===e.value)?[...s.filter(P=>P.value!==e.value)]:[...s,e]:[];r.useEffect(()=>{V&&b&&(f(i?$(b):b),!i&&y())},[V]),r.useEffect(()=>{!i&&v(!1)},[a,s,D,j,E]);const ae=e=>{i&&f([...s.filter(c=>String(c.value)!==String(e))])},I=e=>{f(i?$(e):e),!i&&y()},oe=e=>()=>{W(e)};r.useEffect(()=>{setTimeout(()=>{(n==null?void 0:n.clientHeight)!==F&&i&&(se(n==null?void 0:n.clientHeight),typeof T=="function"&&T())})},[n==null?void 0:n.clientHeight,F,I]),!h&&u&&(h=`input-${q.slugify(u)}`);const w=q.getId(m["data-testid"]),A=q.getId(m["data-trackid"]),le=()=>i?t.jsxRuntimeExports.jsx(he.MultiValueList,{values:s,onRemove:ae,placeholder:g}):!i&&s.label?s.label:g;return t.jsxRuntimeExports.jsxs(o.Wrapper,{ref:l,"data-testid":w("dropdown"),"data-trackid":A("dropdown"),width:N,noMargin:B,children:[t.jsxRuntimeExports.jsxs(o.LabelWrapper,{disabled:a,hasLabel:!!(u!=null&&u.length),children:[u&&t.jsxRuntimeExports.jsx(o.Label,{htmlFor:h,disabled:a,children:u}),!U&&t.jsxRuntimeExports.jsx(o.Text,{children:z})]}),t.jsxRuntimeExports.jsxs(o.Border,{hasError:k,disabled:a,children:[!!C&&t.jsxRuntimeExports.jsx(p.DropdownPrefix,{"data-testid":w("prefix"),hasValue:!!(i?s.length>0:s.value),children:C}),t.jsxRuntimeExports.jsxs(p.SelectInput,{ref:Q,id:h,role:"button","data-testid":w(),"data-trackid":A(),"aria-haspopup":"true","aria-expanded":x,onClick:ue,variant:_,disabled:a,"aria-label":u?void 0:m["aria-label"],hasText:!!((i?s.length>0:s.label)||g),onBlur:G,children:[le(),t.jsxRuntimeExports.jsx("input",{ref:d,name:O,readOnly:!0}),t.jsxRuntimeExports.jsx(fe.default,{width:20,height:20})]})]}),x&&t.jsxRuntimeExports.jsx(p.OptionsPopper,{role:"listbox",ref:Y,width:Z,maxHeight:J,style:ie.popper,...re.popper,"data-testid":"options-dropdown",children:D.map(e=>!e.isHidden&&t.jsxRuntimeExports.jsxs(p.OptionItem,{tabIndex:0,role:"option",isSelected:i?s.some(c=>c.value===e.value):e.value===s.value,isDisabled:e.isDisabled,onClick:()=>!e.isDisabled&&I(e),onFocus:oe(e),"data-trackid":e["data-trackid"],"data-testid":e["data-testid"],"aria-label":e["aria-label"],isMultivalue:i,children:[i?t.jsxRuntimeExports.jsxs(t.jsxRuntimeExports.Fragment,{children:[t.jsxRuntimeExports.jsx(je.Checkbox,{name:O||"multi-select",value:String(e.value),checked:s.some(c=>c.value===e.value),onChange:()=>!e.isDisabled&&I(e),tabIndex:-1}),t.jsxRuntimeExports.jsx(p.OptionItemLabel,{isMultivalue:!0,children:e.label})]}):t.jsxRuntimeExports.jsx(p.OptionItemLabel,{children:e.label}),e.altText&&t.jsxRuntimeExports.jsx("div",{children:e.altText})]},String(e.value)))}),t.jsxRuntimeExports.jsxs(o.SupportingText,{noMargin:B,children:[!!E&&k&&!a&&t.jsxRuntimeExports.jsxs(o.ErrorMessage,{children:[t.jsxRuntimeExports.jsx(xe.default,{width:20,height:20}),E]}),!k&&(typeof R=="string"?t.jsxRuntimeExports.jsx(o.AssistMessage,{children:R}):R)]})]})}exports.Dropdown=me;
