import{u as I,r as o,j as s,b as R,t as f,i as S}from"./index-iQt3bUih.js";import{d as C}from"./gameData-CvtDsga6.js";function K({catalog:m,value:l,onChange:b,max:x,placeholder:D,renderMeta:r,closeOnSelect:E=!1,ariaLabel:z}){I();const[n,w]=o.useState(""),[L,a]=o.useState(!1),[h,c]=o.useState(0),p=o.useRef(null),g=l.map(e=>m.find(t=>t.id===e)??{id:e,name:e}),j=l.length>=x,u=o.useMemo(()=>{const e=n.trim().toLowerCase(),t=n.trim();return m.filter(i=>{var N,k,y,v;return l.includes(i.id)?!1:e?i.name.toLowerCase().includes(e)||i.id.toLowerCase().includes(e)||((N=i.zh)==null?void 0:N.includes(t))||((k=i["zh-CN"])==null?void 0:k.includes(t))||((y=i.zhCN)==null?void 0:y.includes(t))||((v=i.ja)==null?void 0:v.includes(t)):!0}).slice(0,60)},[m,n,l]);o.useEffect(()=>{const e=t=>{p.current&&!p.current.contains(t.target)&&a(!1)};return document.addEventListener("mousedown",e),()=>document.removeEventListener("mousedown",e)},[]);const d=e=>{const t=e.trim();!t||l.includes(t)||l.length>=x||(b([...l,t]),w(""),c(0),E&&a(!1))},F=e=>b(l.filter(t=>t!==e));return s.jsxs("div",{className:"relative flex flex-col gap-1.5",ref:p,children:[g.length>0&&s.jsx("div",{className:"flex flex-wrap gap-1.5",children:g.map(e=>s.jsxs("span",{className:"inline-flex max-w-full items-center gap-1.5 rounded-full border-2 border-line bg-card-soft py-0.5 pr-1 pl-2 text-xs",children:[r==null?void 0:r(e),s.jsx("span",{className:"truncate font-bold text-ink",children:C(e)}),s.jsx("button",{type:"button",className:"shrink-0 text-ink-muted transition hover:text-berry",onClick:()=>F(e.id),"aria-label":f("移除"),children:s.jsx(R,{className:"size-3.5"})})]},e.id))}),!j&&s.jsxs(s.Fragment,{children:[s.jsx("input",{className:S+" w-full",value:n,"aria-label":z,placeholder:D??f("搜尋名稱或直接輸入 ID…"),onChange:e=>{w(e.target.value),a(!0),c(0)},onFocus:()=>a(!0),onKeyDown:e=>{e.key==="ArrowDown"?(e.preventDefault(),c(t=>Math.min(t+1,u.length-1))):e.key==="ArrowUp"?(e.preventDefault(),c(t=>Math.max(t-1,0))):e.key==="Enter"?(e.preventDefault(),u[h]?d(u[h].id):n.trim()&&d(n.trim())):e.key==="Escape"&&a(!1)}}),n.trim()&&!m.some(e=>e.id===n.trim())&&s.jsx("button",{type:"button",className:"absolute right-2 bottom-2 text-xs font-bold text-pal",onClick:()=>d(n.trim()),children:f("用此 ID")}),L&&u.length>0&&s.jsx("div",{className:"absolute top-full right-0 left-0 z-20 mt-1 max-h-64 overflow-y-auto rounded-xl border-2 border-line bg-card shadow-(--shadow-cute)",children:u.map((e,t)=>s.jsxs("button",{type:"button",className:`flex w-full items-center gap-2 px-3 py-1.5 text-left transition ${t===h?"bg-card-soft":"hover:bg-card-soft"}`,onMouseEnter:()=>c(t),onClick:()=>d(e.id),children:[r==null?void 0:r(e),s.jsx("span",{className:"min-w-0 flex-1 truncate text-sm font-bold",children:C(e)}),s.jsx("span",{className:"max-w-[45%] shrink-0 truncate font-mono text-xs text-ink-muted",children:e.id})]},e.id))})]}),j&&s.jsx("p",{className:"text-xs text-ink-muted",children:f("已達上限({n})",{n:x})})]})}export{K as M};
