"use client"
import{a as K}from"./chunk-HOBM2JR6.mjs";import{a as D}from"./chunk-GVBIGY6N.mjs";import{a as B}from"./chunk-NWME2ANF.mjs";import{a as j}from"./chunk-SK6I25VZ.mjs";import{k as H}from"./chunk-BX5I7JME.mjs";import{a as V}from"./chunk-WMTXO7RT.mjs";import{a as L}from"./chunk-JW75YDRU.mjs";import{a as $}from"./chunk-JOLDVN3E.mjs";import{a as F}from"./chunk-IHTRL6TU.mjs";import{a as S}from"./chunk-EW7LM5VG.mjs";import{f as A,j as u}from"./chunk-KDP6L5T4.mjs";import{Command as g,CommandEmpty as ne,CommandInput as re,CommandItem as le}from"cmdk";import{ChevronDown as ie}from"lucide-react";import{isValidElement as ce,useCallback as me,useEffect as O,useRef as de,useState as p}from"react";import{Fragment as k,jsx as e,jsxs as l}from"react/jsx-runtime";function ue(s,o){return s===!0}function Ie({multiple:s,selected:o,setSelected:r,options:a,icon:f,placeholder:U="Select...",searchPlaceholder:Z="Search...",emptyState:M,createLabel:h,onCreate:x,buttonProps:i,shortcutHint:T,caret:q,side:G,open:J,onOpenChange:X,children:Y}){let I=ue(s,r),n=Array.isArray(o)?o:o?[o]:[],{isMobile:_}=B(),[ee,te]=p(!1),b=J??ee,v=X??te,[c,y]=p(""),[P,C]=p(!1),[z,se]=p(void 0),[oe,R]=p(!1),ae=t=>{var m;if(I){let N=n.some(({value:d})=>d===t.value);r(N?n.filter(({value:d})=>d!==t.value):[...n,t])}else r(n.length&&((m=n[0])==null?void 0:m.value)===t.value?null:t)},E=me(t=>[...t].sort((m,N)=>{let d=n.some(w=>w.value===m.value),W=n.some(w=>w.value===N.value);return d&&!W?-1:!d&&W?1:0}),[n]);return O(()=>{P&&(se(a&&E(a)),C(!1))},[P,a,E]),O(()=>{C(!0)},[a]),O(()=>{b||(y(""),C(!0))},[b]),e(K,{openPopover:b,setOpenPopover:v,align:"start",side:G,onWheel:t=>{t.stopPropagation()},content:e(j,{width:!_,height:!0,className:"rounded-[inherit]",style:{transform:"translateZ(0)"},transition:{ease:"easeInOut",duration:.1},children:l(g,{loop:!0,children:[l("div",{className:"flex items-center overflow-hidden rounded-t-lg border-b border-gray-200",children:[e(re,{placeholder:Z,value:c,onValueChange:y,className:"grow border-0 py-3 pl-4 pr-2 outline-none placeholder:text-gray-400 focus:ring-0 sm:text-sm",onKeyDown:t=>{(t.key==="Escape"||t.key==="Backspace"&&!c)&&(t.preventDefault(),t.stopPropagation(),v(!1))}}),T&&e("kbd",{className:"mr-2 hidden shrink-0 rounded bg-gray-200 px-2 py-0.5 text-xs font-light text-gray-500 md:block",children:T})]}),e(pe,{children:e(g.List,{className:u("flex w-full min-w-[100px] flex-col gap-1 p-1"),children:z!==void 0?l(k,{children:[z.map(t=>e(fe,{option:t,multiple:I,selected:n.some(({value:m})=>m===t.value),onSelect:()=>ae(t)},`${t.label}, ${t.value}`)),c.length>0&&x&&l(le,{className:u("flex cursor-pointer items-center gap-3 whitespace-nowrap rounded-md px-3 py-2 text-left text-sm text-gray-700","data-[selected=true]:bg-gray-100"),onSelect:async()=>{R(!0),await(x==null?void 0:x(c))&&(y(""),v(!1)),R(!1)},children:[oe?e(S,{className:"size-4 shrink-0"}):e(V,{className:"size-4 shrink-0"}),e("div",{className:"grow",children:(h==null?void 0:h(c))||`Create "${c}"`})]}),e(ne,{className:"flex h-12 items-center justify-center text-sm text-gray-500",children:M||"No matches"})]}):e(g.Loading,{children:e("div",{className:"flex h-12 items-center justify-center",children:e(S,{})})})})})]})}),children:e(H,{variant:"secondary",...i,className:u(i==null?void 0:i.className,"flex items-center gap-2"),textWrapperClassName:u(i==null?void 0:i.textWrapperClassName,"w-full flex items-center justify-start"),text:l(k,{children:[e("div",{className:"min-w-0 grow truncate text-left",children:Y||n.map(t=>t.label).join(", ")||U}),q&&e(ie,{className:"size-4 shrink-0 text-gray-400 transition-transform duration-75 group-data-[state=open]:rotate-180"})]}),icon:f?Q(f)?f:e(f,{className:"size-4"}):void 0})})}var pe=({children:s})=>{let o=de(null),{scrollProgress:r,updateScrollProgress:a}=D(o);return l(k,{children:[e("div",{className:"scrollbar-hide max-h-[min(50vh,190px)] w-screen overflow-y-scroll sm:w-auto",ref:o,onScroll:a,children:s}),e("div",{className:"pointer-events-none absolute bottom-0 left-0 hidden h-16 w-full bg-gradient-to-t from-white sm:block",style:{opacity:1-Math.pow(r,2)}})]})};function fe({option:s,onSelect:o,multiple:r,selected:a}){return l(g.Item,{className:u("flex cursor-pointer items-center gap-3 whitespace-nowrap rounded-md px-3 py-2 text-left text-sm","data-[selected=true]:bg-gray-100"),onSelect:o,value:s.label+(s==null?void 0:s.value),children:[r&&e("div",{className:"shrink-0 text-gray-600",children:a?e($,{className:"size-4 text-gray-600"}):e(F,{className:"size-4 text-gray-400"})}),l("div",{className:"flex grow items-center gap-1",children:[s.icon&&e("span",{className:"shrink-0 text-gray-600",children:Q(s.icon)?s.icon:e(s.icon,{className:"h-4 w-4"})}),A(s.label,48)]}),!r&&a&&e(L,{className:"size-4 shrink-0 text-gray-600"})]})}var Q=s=>ce(s);export{Ie as a};
