const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=["assets/Helper-DM8_S2CX.js","assets/rolldown-runtime-Dd_uD5pT.js","assets/AxiosHelper-BVIPb78C.js","assets/react-BatatxyT.js"])))=>i.map(i=>d[i]);
import{i as e}from"./rolldown-runtime-Dd_uD5pT.js";import{t}from"./react-BatatxyT.js";import{t as n}from"./preload-helper-Czpn1I53.js";import{s as r,t as i}from"./utils-BO1RPuol.js";import{t as a}from"./chevron-down-Bbo_Qa5h.js";import{n as o,r as s,t as c}from"./popover-sxZyaQ0Q.js";import{n as l}from"./Helper-DM8_S2CX.js";import{Ko as u,ko as d,vo as f}from"./index-CsuVxiRQ.js";var p=e(t(),1),m=r();function h({url:e,params:t,mapOption:r=e=>({value:e.id,label:e.name}),value:h,onValueChange:g,onSelect:_,placeholder:v=`Select...`,disabled:y,id:b,className:x,pageSize:S=10,initialLabel:C}){let[w,T]=(0,p.useState)(!1),[E,D]=(0,p.useState)(``),[O,k]=(0,p.useState)([]),[A,j]=(0,p.useState)([]),[M,N]=(0,p.useState)(!1),[P,F]=(0,p.useState)(1),[I,L]=(0,p.useState)(!0),[R,z]=(0,p.useState)(C||``),B=(0,p.useRef)(null),V=(0,p.useRef)(null),H=(0,p.useRef)(void 0),U=(0,p.useRef)(0),W=(0,p.useRef)({}),G=(0,p.useRef)(h);(0,p.useEffect)(()=>{C&&h&&(G.current=h)},[C]),(0,p.useEffect)(()=>{if(!h){z(``);return}if(C&&h===G.current){z(C),W.current[h]=C;return}if(W.current[h]){z(W.current[h]);return}let i=O.find(e=>e.value===h);if(i){z(i.label),W.current[h]=i.label;return}let a=/^[0-9a-f]{8}-[0-9a-f]{4}-/.test(h),o=/^\d+$/.test(h);if(!a&&!o){W.current[h]=h,z(h);return}let s=!0;return n(async()=>{let{getRowData:e,getParamsList:t}=await import(`./Helper-DM8_S2CX.js`).then(e=>e.t);return{getRowData:e,getParamsList:t}},__vite__mapDeps([0,1,2,3])).then(({getRowData:n,getParamsList:i})=>{n(`${e}${h}/`).then(e=>{if(!s)return;let t=e?.data??e;if(t&&t.id){let e=r(t).label;W.current[h]=e,z(e)}else throw Error(`No valid item returned`)}).catch(()=>{s&&i(e,{...t,page:`1`,page_size:`50`}).then(e=>{if(!s)return;let t=(Array.isArray(e)?e:e?.results||[]).find(e=>String(r(e).value)===String(h));if(t){let e=r(t).label;W.current[h]=e,z(e)}else W.current[h]=h,z(h)}).catch(()=>{s&&(W.current[h]=h,z(h))})})}),()=>{s=!1}},[h,e,C]);let K=(0,p.useCallback)(async(n,i,a=!1)=>{let o=++U.current;N(!0);try{let s=await l(e,{...t,page:String(i),page_size:String(S),...n?{search:n}:{}});if(o!==U.current)return;let c=Array.isArray(s)?s:s?.results||[],u=Array.isArray(s)?s.length:s?.count??c.length,d=c.map(r);k(e=>a?[...e,...d]:d),j(e=>a?[...e,...c]:c),L(a?O.length+d.length<u:d.length<u)}catch{if(o!==U.current)return;a||(k([]),j([])),L(!1)}finally{o===U.current&&N(!1)}},[e,t,S,r]);(0,p.useEffect)(()=>{w&&(D(``),F(1),L(!0),K(``,1),setTimeout(()=>B.current?.focus(),0))},[w]);let q=e=>{D(e),clearTimeout(H.current),H.current=setTimeout(()=>{F(1),L(!0),K(e,1)},300)};return(0,m.jsxs)(c,{open:w,onOpenChange:y?void 0:T,children:[(0,m.jsx)(s,{asChild:!0,children:(0,m.jsxs)(`button`,{type:`button`,id:b,disabled:y,role:`combobox`,"aria-expanded":w,className:i(`flex w-full items-center justify-between gap-2 rounded-md border border-input bg-transparent px-3 py-2 text-sm h-9 whitespace-nowrap shadow-xs transition-[color,box-shadow] outline-none focus-visible:border-primary focus-visible:ring-[3px] focus-visible:ring-primary/20 disabled:cursor-not-allowed disabled:opacity-50 dark:bg-input/30 dark:hover:bg-input/50`,!R&&`text-muted-foreground`,x),children:[(0,m.jsx)(`span`,{className:`truncate`,children:R||v}),(0,m.jsx)(a,{className:`size-4 shrink-0 opacity-50`})]})}),(0,m.jsxs)(o,{className:`p-0 max-h-72 flex flex-col w-[var(--radix-popover-trigger-width)]`,children:[(0,m.jsxs)(`div`,{className:`flex items-center gap-2 border-b border-border/60 px-3 py-2`,children:[(0,m.jsx)(f,{className:`size-4 shrink-0 text-muted-foreground`}),(0,m.jsx)(`input`,{ref:B,value:E,onChange:e=>q(e.target.value),placeholder:`Search...`,className:`flex-1 bg-transparent text-sm outline-none placeholder:text-muted-foreground`}),M&&(0,m.jsx)(d,{className:`size-3.5 animate-spin text-muted-foreground`})]}),(0,m.jsxs)(`div`,{ref:V,className:`overflow-y-auto max-h-56`,onScroll:()=>{let e=V.current;if(!(!e||M||!I)&&e.scrollTop+e.clientHeight>=e.scrollHeight-20){let e=P+1;F(e),K(E,e,!0)}},children:[O.length===0&&!M?(0,m.jsx)(`div`,{className:`py-4 text-center text-sm text-muted-foreground`,children:`No results found`}):O.map(e=>(0,m.jsxs)(`button`,{type:`button`,className:i(`relative flex w-full cursor-pointer items-center gap-2 py-2.5 pr-8 pl-3 text-sm border-b border-border/40 last:border-b-0 transition-colors hover:bg-primary/10 hover:text-primary`,h===e.value&&`bg-primary text-primary-foreground`),onClick:()=>{g?.(e.value);let t=A.find(t=>r(t).value===e.value);t&&_?.(t),z(e.label),W.current[e.value]=e.label,T(!1)},children:[(0,m.jsx)(`span`,{className:`truncate`,children:e.label}),h===e.value&&(0,m.jsx)(`span`,{className:`absolute right-2 flex size-3.5 items-center justify-center`,children:(0,m.jsx)(u,{className:`size-4`})})]},e.value)),M&&O.length>0&&(0,m.jsx)(`div`,{className:`py-2 text-center text-xs text-muted-foreground`,children:`Loading more...`})]})]})]})}export{h as t};