import{j as n,r as s}from"./vendor-react-CVHocMAO.js";import{I as K,O as M,F as z}from"./index-DXdAv5n3.js";import{A as W,u as X}from"./vendor-maps-BX5dXCaZ.js";const G="AIzaSyATWdF6OVbFX1X_OuYCMZdxWZi7ggnhRWI",T=({value:a,onChange:i,onSelect:_,placeholder:E="e.g., Austin, TX",className:I,id:P,persistOnScroll:S=!1,inOverlay:A=!1})=>{const[p,v]=s.useState([]),[L,r]=s.useState(!1),[d,R]=s.useState(null),u=X("places"),x=s.useRef(null),f=s.useRef(null),h=s.useRef(null),w=s.useRef(null),O=s.useRef(null);s.useEffect(()=>{u&&(x.current=new u.AutocompleteService,h.current||(h.current=document.createElement("div")),f.current=new u.PlacesService(h.current),navigator.geolocation&&navigator.geolocation.getCurrentPosition(e=>{R({lat:e.coords.latitude,lng:e.coords.longitude})},()=>{}))},[u]),s.useEffect(()=>{const e=o=>{w.current&&!w.current.contains(o.target)&&r(!1)},t=()=>{S||r(!1)};return document.addEventListener("mousedown",e),window.addEventListener("scroll",t,!0),()=>{document.removeEventListener("mousedown",e),window.removeEventListener("scroll",t,!0)}},[S]);const b=s.useCallback(e=>{if(i(e),!e.trim()){v([]),r(!1);return}if(x.current&&e.length>1){const t={input:e,types:["(cities)"],componentRestrictions:{country:"us"}};d&&typeof google<"u"&&google.maps&&(t.location=new google.maps.LatLng(d.lat,d.lng),t.radius=1e5),x.current.getPlacePredictions(t,(o,m)=>{if(m===google.maps.places.PlacesServiceStatus.OK&&o&&o.length>0){const g=o.map(c=>({description:c.description,placeId:c.place_id}));v(g),r(!0)}else v([]),r(!1)})}},[i,d]),N=s.useCallback(e=>{i(e.description),r(!1),f.current&&f.current.getDetails({placeId:e.placeId,fields:["geometry","address_components","formatted_address"]},(t,o)=>{var m,g,c,j;if(o===google.maps.places.PlacesServiceStatus.OK&&((m=t==null?void 0:t.geometry)!=null&&m.location)&&(t!=null&&t.address_components)){const C=t.geometry.location,y=t.address_components,F=((g=y.find(l=>l.types.includes("locality")))==null?void 0:g.long_name)||((c=y.find(l=>l.types.includes("administrative_area_level_2")))==null?void 0:c.long_name)||"",D=((j=y.find(l=>l.types.includes("administrative_area_level_1")))==null?void 0:j.short_name)||"";_(F,D,{lat:C.lat(),lng:C.lng()})}})},[i,_,f]),k=()=>{a.trim()&&p.length===0&&b(a)};return n.jsxs("div",{className:"relative",ref:w,children:[n.jsx(K,{ref:O,id:P,value:a,onChange:e=>b(e.target.value),onFocus:k,placeholder:E,className:I,autoComplete:"off"}),L&&p.length>0&&n.jsx("div",{className:z("absolute mt-2 w-full rounded-xl border border-border bg-card shadow-lg","max-h-64 overflow-y-auto",A?"z-[130]":"z-50"),children:p.map((e,t)=>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:()=>N(e),children:[n.jsx(M,{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}-${t}`))})]})},q=a=>n.jsx(W,{apiKey:G,children:n.jsx(T,{...a})});export{q as C};