import{r,j as t}from"./index-DiwDH9aE.js";import{C as I}from"./chevron-down-4ep2ftiY.js";import{X as K}from"./x-D4vgwdOf.js";import{S as M}from"./search-h1vL1q1e.js";const X=({onSearch:w,onLoadSelected:g,renderOption:T,getDisplayText:B,initialItem:f=null,value:n,onChange:v,placeholder:D="Seleccionar...",searchPlaceholder:S="Buscar...",error:$,disabled:x=!1,minSearchLength:c=3,manualSearch:a=!1})=>{const[i,m]=r.useState(!1),[l,p]=r.useState(""),[j,d]=r.useState([]),[N,E]=r.useState(!1),[o,u]=r.useState(null),[b,k]=r.useState(!1),y=r.useRef(null);r.useEffect(()=>{if(l.length<c){d([]);return}if(a&&!b)return;const e=setTimeout(async()=>{E(!0);try{const s=await w(l);d(s||[])}catch(s){console.error("Error en búsqueda:",s),d([])}finally{E(!1),k(!1)}},a?0:500);return()=>clearTimeout(e)},[l,c,w,a,b]),r.useEffect(()=>{if(n==null){u(null);return}if(f&&f.id===n&&!o){u(f);return}n&&(!o||o.id!==n)&&g&&(async()=>{try{const s=await g(n);s&&u(s)}catch(s){console.error("Error cargando item seleccionado:",s)}})()},[n,o,g,f]),r.useEffect(()=>{const e=h=>{h.key==="Escape"&&i&&m(!1)},s=h=>{y.current&&!y.current.contains(h.target)&&m(!1)};return i&&(document.addEventListener("keydown",e),document.addEventListener("mousedown",s)),()=>{document.removeEventListener("keydown",e),document.removeEventListener("mousedown",s)}},[i]);const O=e=>{u(e),v(e.id),m(!1),p(""),d([])},R=e=>{e.stopPropagation(),u(null),v(null),p(""),d([])},C=()=>{l.length>=c&&k(!0)};return t.jsxs("div",{className:"relative",ref:y,children:[t.jsxs("div",{className:"relative",children:[t.jsxs("button",{type:"button",onClick:()=>!x&&m(!i),disabled:x,className:`w-full px-3 py-2 text-sm border rounded-lg text-left flex items-center justify-between transition-colors ${$?"border-red-500 focus:ring-red-500":"border-gray-300 focus:ring-blue-500"} ${x?"bg-gray-100 cursor-not-allowed":"bg-white hover:border-gray-400"}`,children:[t.jsx("span",{className:o?"text-gray-900":"text-gray-500",children:o?B(o):D}),t.jsx(I,{className:`w-4 h-4 text-gray-500 transition-transform ${i?"transform rotate-180":""}`})]}),o&&!x&&t.jsx("button",{type:"button",onClick:R,className:"absolute right-8 top-1/2 -translate-y-1/2 p-1 hover:bg-gray-100 rounded transition-colors","aria-label":"Limpiar selección",children:t.jsx(K,{className:"w-4 h-4 text-gray-500"})})]}),i&&t.jsxs("div",{className:"absolute z-50 mt-1 w-full bg-white border border-gray-300 rounded-lg shadow-lg max-h-80 overflow-hidden",children:[t.jsx("div",{className:"p-2 border-b",children:t.jsxs("div",{className:"relative",children:[t.jsx(M,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-gray-400"}),t.jsx("input",{type:"text",value:l,onChange:e=>p(e.target.value),onKeyDown:e=>{e.key==="Enter"&&a&&(e.preventDefault(),C())},placeholder:S,className:`w-full ${a?"pr-20":"pr-3"} pl-9 py-2 text-sm border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500`,autoFocus:!0}),a&&t.jsx("button",{type:"button",onClick:C,disabled:l.length<c||N,className:"absolute right-2 top-1/2 -translate-y-1/2 px-3 py-1 text-xs bg-blue-600 text-white rounded hover:bg-blue-700 disabled:bg-gray-400 disabled:cursor-not-allowed transition-colors",children:"Buscar"})]})}),t.jsx("div",{className:"overflow-y-auto max-h-60",children:N?t.jsx("div",{className:"p-4 text-center text-sm text-gray-500",children:"Buscando..."}):j.length>0?j.map(e=>t.jsx("button",{type:"button",onClick:()=>O(e),className:"w-full px-4 py-2 text-left text-sm hover:bg-blue-50 transition-colors focus:bg-blue-50 focus:outline-none",children:T(e)},e.id)):l.length>=c?t.jsx("div",{className:"p-4 text-center text-sm text-gray-500",children:a&&!b?'Presione "Buscar" para ver resultados':"No se encontraron resultados"}):t.jsxs("div",{className:"p-4 text-center text-sm text-gray-500",children:["Escribe al menos ",c," caracteres para buscar"]})})]})]})};export{X as S};
