import{j as n,r as o}from"./vendor-react-DgEFbQIi.js";import{G as I,I as R,a2 as K,a as z}from"./index-DFVuS8SP.js";import{A as B,u as U}from"./vendor-maps-bOuA10hf.js";import{f as Y}from"./city-coordinates-9wKWO0xx.js";const H=({value:r,onChange:l,onSelect:p,onSubmit:f,placeholder:_="e.g., Austin, TX",className:S,id:C,persistOnScroll:x=!1,inOverlay:i=!1,dropdownFooter:h,openOnFocus:a=!1})=>{const[d,t]=o.useState([]),[M,u]=o.useState(!1),[v,T]=o.useState(null),y=U("places"),E=o.useRef(null),w=o.useRef(null),N=o.useRef(null),P=o.useRef(null),G=o.useRef(null);o.useEffect(()=>{y&&(E.current=new y.AutocompleteService,N.current||(N.current=document.createElement("div")),w.current=new y.PlacesService(N.current),navigator.geolocation&&navigator.geolocation.getCurrentPosition(e=>{T({lat:e.coords.latitude,lng:e.coords.longitude})},()=>{}))},[y]),o.useEffect(()=>{const e=c=>{P.current&&!P.current.contains(c.target)&&u(!1)},s=()=>{x||u(!1)};return document.addEventListener("mousedown",e),window.addEventListener("scroll",s,!0),()=>{document.removeEventListener("mousedown",e),window.removeEventListener("scroll",s,!0)}},[x]);const D=o.useCallback(e=>{if(l(e),!e.trim()){t([]),u(a);return}if(E.current&&e.length>1){const s={input:e,types:["(cities)"],componentRestrictions:{country:"us"}};v&&typeof google<"u"&&google.maps&&(s.location=new google.maps.LatLng(v.lat,v.lng),s.radius=1e5),E.current.getPlacePredictions(s,(c,b)=>{if(b===google.maps.places.PlacesServiceStatus.OK&&c&&c.length>0){const j=c.map(m=>({description:m.description,placeId:m.place_id}));t(j)}else t([]);u(!0)})}},[l,v,a]),O=o.useCallback(e=>{l(e.description),u(!1),w.current&&w.current.getDetails({placeId:e.placeId,fields:["geometry","address_components","formatted_address"]},(s,c)=>{var b,j,m,L;if(c===google.maps.places.PlacesServiceStatus.OK&&((b=s==null?void 0:s.geometry)!=null&&b.location)&&(s!=null&&s.address_components)){const k=s.geometry.location,A=s.address_components,$=((j=A.find(g=>g.types.includes("locality")))==null?void 0:j.long_name)||((m=A.find(g=>g.types.includes("administrative_area_level_2")))==null?void 0:m.long_name)||"",q=((L=A.find(g=>g.types.includes("administrative_area_level_1")))==null?void 0:L.short_name)||"";p($,q,{lat:k.lat(),lng:k.lng()})}})},[l,p,w]),X=()=>{a&&u(!0),r.trim()&&d.length===0&&D(r)};return n.jsxs("div",{className:"relative",ref:P,children:[n.jsx(R,{ref:G,id:C,value:r,onChange:e=>D(e.target.value),onFocus:X,onKeyDown:e=>{e.key==="Enter"&&f&&(e.preventDefault(),f())},placeholder:_,className:S,autoComplete:"off"}),M&&(d.length>0||h)&&n.jsxs("div",{className:z("absolute mt-2 w-full rounded-xl border border-border bg-card shadow-lg","max-h-64 overflow-y-auto",i?"z-[130]":"z-50"),children:[d.map((e,s)=>n.jsxs("button",{type:"button",className:"w-full flex items-center gap-3 px-4 py-3 text-left hover:bg-muted transition-colors",onClick:()=>O(e),children:[n.jsx(K,{className:"h-5 w-5 text-muted-foreground flex-shrink-0"}),n.jsx("div",{className:"flex-1 min-w-0",children:n.jsx("div",{className:"text-sm font-medium text-foreground",children:e.description})})]},`${e.placeId}-${s}`)),h]})]})},J=({value:r,onChange:l,onSelect:p,onSubmit:f,placeholder:_="e.g., Austin, TX",className:S,id:C,inOverlay:x=!1,dropdownFooter:i})=>{const[h,a]=o.useState(!1),d=o.useMemo(()=>!r.trim()&&i?[]:Y(r),[r,i]);return n.jsxs("div",{className:"relative",onBlur:t=>{t.currentTarget.contains(t.relatedTarget)||a(!1)},children:[n.jsx(R,{id:C,value:r,onChange:t=>{l(t.target.value),a(!0)},onFocus:()=>a(!0),onKeyDown:t=>{t.key==="Enter"&&f&&(t.preventDefault(),f())},placeholder:_,className:S,autoComplete:"off"}),h&&(d.length>0||i)&&n.jsxs("div",{className:z("absolute mt-2 max-h-64 w-full overflow-y-auto rounded-xl border border-border bg-card shadow-lg",x?"z-[130]":"z-50"),children:[d.map(t=>n.jsxs("button",{type:"button",className:"flex w-full items-center gap-3 px-4 py-3 text-left transition-colors hover:bg-muted",onClick:()=>{l(t.label),p(t.city,t.state,{lat:t.lat,lng:t.lng}),a(!1)},children:[n.jsx(K,{className:"h-5 w-5 flex-shrink-0 text-muted-foreground"}),n.jsx("span",{className:"text-sm font-medium text-foreground",children:t.label})]},t.label)),i]})]})},V=r=>I?n.jsx(B,{apiKey:I,children:n.jsx(H,{...r})}):n.jsx(J,{...r});export{V as C};