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