import{r as t,j as s}from"./vendor-react-DgEFbQIi.js";import{G as F,L as z,I as $,a as G,a2 as R,X as V}from"./index-DFVuS8SP.js";import{A as Z,u as U}from"./vendor-maps-bOuA10hf.js";const W=({id:r,label:j,compact:m,value:u,onChangeText:a,onResolved:c,onClearResolved:o,resolved:d,placeholder:x="Start typing an address…",className:q,disabled:M,inOverlay:B,resolvedMessage:C,suppressUnresolvedWarning:S})=>{const h=U("places"),E=t.useRef(null),P=t.useRef(null),_=t.useRef(null),[k,w]=t.useState([]),[X,p]=t.useState(!1),D=t.useRef(null),Y=!M&&u.trim().length>0;t.useEffect(()=>{h&&(E.current=new h.AutocompleteService,_.current||(_.current=document.createElement("div")),P.current=new h.PlacesService(_.current))},[h]),t.useEffect(()=>{const e=l=>{D.current&&!D.current.contains(l.target)&&p(!1)};return document.addEventListener("mousedown",e),()=>document.removeEventListener("mousedown",e)},[]);const N=t.useRef(!1);t.useEffect(()=>{var g,i;if(!h)return;if(d){N.current=!1;return}if(!S){N.current=!1;return}if(N.current)return;const e=u.trim();if(!e)return;N.current=!0;const l=typeof window<"u"?(i=(g=window.google)==null?void 0:g.maps)==null?void 0:i.Geocoder:void 0;if(!l)return;new l().geocode({address:e,region:"us"},(y,I)=>{var A;if(I==="OK"&&y&&y.length>0){const b=y[0],f=(A=b.geometry)==null?void 0:A.location,O=typeof(f==null?void 0:f.lat)=="function"?f.lat():void 0,K=typeof(f==null?void 0:f.lng)=="function"?f.lng():void 0;c({formattedAddress:b.formatted_address||e,placeId:b.place_id||void 0,lat:typeof O=="number"?O:void 0,lng:typeof K=="number"?K:void 0})}})},[h,d,S,u,c]);const H=t.useCallback(e=>{if(a(e),d&&o(),!e.trim()||!E.current){w([]),p(!1);return}const l={input:e,componentRestrictions:{country:"us"}};E.current.getPlacePredictions(l,(n,g)=>{g===google.maps.places.PlacesServiceStatus.OK&&n&&n.length>0?(w(n.map(i=>({description:i.description,placeId:i.place_id}))),p(!0)):(w([]),p(!1))})},[a,o,d]),J=t.useCallback(()=>{a(""),o(),w([]),p(!1)},[a,o]),Q=t.useCallback(e=>{if(a(e.description),p(!1),w([]),!P.current){c({formattedAddress:e.description,placeId:e.placeId});return}P.current.getDetails({placeId:e.placeId,fields:["formatted_address","geometry","place_id"]},(l,n)=>{var g,i,y,I;if(n===google.maps.places.PlacesServiceStatus.OK&&l){const A=(i=(g=l.geometry)==null?void 0:g.location)==null?void 0:i.lat(),b=(I=(y=l.geometry)==null?void 0:y.location)==null?void 0:I.lng();c({formattedAddress:l.formatted_address||e.description,placeId:l.place_id||e.placeId,lat:typeof A=="number"?A:void 0,lng:typeof b=="number"?b:void 0})}else c({formattedAddress:e.description,placeId:e.placeId})})},[a,c]);return s.jsxs("div",{className:G(m?"space-y-1":"space-y-2",q),ref:D,children:[!m&&s.jsxs(s.Fragment,{children:[s.jsx(z,{htmlFor:r,className:"text-base font-semibold",children:j}),s.jsx("p",{className:"text-xs text-muted-foreground",children:"Choose a verified address so your host knows where to provide the service."})]}),s.jsxs("div",{className:"relative",children:[s.jsx($,{id:r,value:u,disabled:M,"aria-label":m?x:void 0,onChange:e=>H(e.target.value),onFocus:()=>u.trim().length>1&&k.length>0&&p(!0),placeholder:x,autoComplete:"off",className:G("h-11 rounded-xl pl-10 pr-10",d&&"border-primary/50")}),s.jsx(R,{className:"pointer-events-none absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground"}),Y&&s.jsx("button",{type:"button","aria-label":"Clear address",className:"absolute right-3 top-1/2 -translate-y-1/2 text-muted-foreground hover:text-foreground",onMouseDown:e=>e.preventDefault(),onClick:J,children:s.jsx(V,{className:"h-4 w-4"})}),X&&k.length>0&&s.jsx("div",{className:G("absolute z-50 mt-1 max-h-56 w-full overflow-y-auto rounded-xl border border-border bg-card py-1 shadow-lg",B&&"z-[130]"),children:k.map((e,l)=>s.jsxs("button",{type:"button",className:"flex w-full items-start gap-2 px-3 py-2.5 text-left text-sm hover:bg-muted",onMouseDown:n=>n.preventDefault(),onClick:()=>Q(e),children:[s.jsx(R,{className:"mt-0.5 h-4 w-4 shrink-0 text-muted-foreground"}),s.jsx("span",{className:"min-w-0",children:e.description})]},`${e.placeId}-${l}`))})]}),d?s.jsx("p",{className:"text-xs font-medium text-primary",children:C??"Address verified — you can change it by typing a new search."}):u.trim()&&!S?s.jsx("p",{className:"text-xs text-amber-800",children:"Select an address from the list to continue."}):null]})},ee=({eventLocation:r,onEventLocationChange:j,onInputTextChange:m,suppressUnresolvedWarning:u,...a})=>{const[c,o]=t.useState(()=>(r==null?void 0:r.formattedAddress)??"");return t.useEffect(()=>{o((r==null?void 0:r.formattedAddress)??"")},[r==null?void 0:r.formattedAddress]),F?s.jsx(Z,{apiKey:F,children:s.jsx(W,{...a,value:c,onChangeText:d=>{o(d),m==null||m(d)},resolved:!!(r!=null&&r.placeId||(r==null?void 0:r.lat)!=null&&(r==null?void 0:r.lng)!=null),suppressUnresolvedWarning:u,onClearResolved:()=>j(null),onResolved:d=>j(d)})}):s.jsxs("div",{className:"space-y-2",children:[s.jsx(z,{className:"text-base font-semibold",children:a.label}),s.jsx($,{value:c,onChange:d=>{const x=d.target.value;o(x),j(x.trim()?{formattedAddress:x.trim()}:null)},placeholder:"Enter full street address",className:"h-11 rounded-xl"}),s.jsx("p",{className:"text-xs text-muted-foreground",children:"Add Google Maps API key for address suggestions. Until then, type the full address."})]})};export{ee as E};