import{Z as v,r as g,j as m,F as y,D as w,Y as S,a as j,m as k,_ as C,$ as P}from"./entry.client-CV0PzNS2.js";import{u as N}from"./Select-CiUaCRv7.js";import{p as $}from"./parseQueryParams-Bd6KFXLi.js";const E=d=>{const{className:t,id:s}=d,[c,n]=v(),[r,u]=g.useState(c.get(s)??void 0),f=a=>{u(a.target.value)},o=a=>{a.key==="Enter"&&l()},l=g.useCallback(()=>{r&&r!==(c.get(s)??void 0)&&n(s,r,!0),!r&&c.get(s)&&n(s,void 0,!0)},[r]),i=g.useCallback(()=>{c.get("q")?n(s,void 0,!0):u("")},[s,c,n]);return m.jsxs("div",{className:`flex flex-1 items-center rounded-md border border-gray-300 transition-all duration-200 focus-within:border-accent-500 z-10 h-10 ${t}`,children:[m.jsx("input",{type:"text",className:"block h-full w-full min-w-0 flex-1 border-0 bg-transparent py-2 outline-none focus:shadow-none focus:ring-0 sm:text-sm px-3",placeholder:"Cerca per codice/titolo...",maxLength:100,onChange:f,onKeyDown:o,value:r}),m.jsx("button",{type:"button",onClick:i,className:`transition-colors cursor-pointer flex flex-col items-center justify-center px-4 text-gray-500 hover:text-gray-200 ${r?"":"hidden"}`,children:m.jsx(y,{name:"fa-solid fa-xmark"})}),m.jsx("button",{onClick:l,className:"flex flex-col items-center justify-center bg-accent-500 transition-colors hover:bg-accent-700 cursor-pointer px-4 text-overAccent hover:text-overAccent rounded-tr-md rounded-br-md h-10",children:m.jsx(y,{name:"fa-solid fa-magnifying-glass"})})]})},Q=d=>{const t=w(),{url:s,filters:c,filters_url:n,page:r}=t.route,{navigateTo:u}=S(),f=s.split("?")[0].replace(/\/$/,""),o=g.useCallback(i=>{const a=$(s,i),p=a?f.concat(a):null;p&&u(p,["route"])},[s,f,u]),l=g.useCallback(i=>{const a=new RegExp(`\\/page\\/${r}(\\?|$)`),h=(n[d]?n[d]:n.default).split("/").indexOf(`:${d}:`),x=f.replace(a,"").split("/");i&&(h===-1||x.length<=h?x.push(i.slug):x[h]=i.slug);let b=x.join("/");s.split("?")[1]&&(b=b.concat("?"+s.split("?")[1])),u(b,["route"])},[r,n,d,f,s,u]);return{filters:c,setFilters:l,setQueryParams:o}},I=d=>{const{id:t,className:s,label:c,changeUrl:n=!1,filterKey:r=""}=d,{setQueryParams:u,setFilters:f}=Q(r),{filters:o,filters_selected:l}=w().route,i=N(),a=g.useMemo(()=>j(o[t],e=>e.slug===l[t])[0],[o,t,l]),p=g.useMemo(()=>({label:(a==null?void 0:a.name)||o[t][0].name,value:l[t]}),[o,t,a==null?void 0:a.name,l]),h=g.useMemo(()=>k(o[t],e=>({label:e.name,value:e.slug})),[o,t]),x=e=>{n?f({slug:e??l[t].slug,name:l[t].name}):u({[t]:e??o[t][0].name})},b={...i,control:e=>({...e,border:0,boxShadow:"none",background:"transparent"}),indicatorSeparator:e=>({...e,background:"transparent"}),menu:e=>({...e,width:"max-content",minWidth:"100%",right:0}),menuPortal:e=>({...e,zIndex:100})};return m.jsxs("div",{className:`flex flex-row items-center border border-gray-300 rounded-md pl-3 w-full md:w-auto h-10 ${s}`,children:[m.jsx("span",{className:"text-gray-500 mr-1 contents text-sm whitespace-nowrap flex-1",children:c}),m.jsx(C,{Component:P,className:"!border-none w-full lg:w-auto text-right",isSearchable:!1,menuPosition:"absolute",onChange:e=>{x((e==null?void 0:e.value)??null)},defaultValue:p,menuPortalTarget:document.body,styles:b,isMulti:!1,options:h})]})};export{E as I,I as S};