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