import{d as f,k as v,r as g,K as _,H as m,B as y,ac as h,o as A,a as b,q as k}from"./app-bWy6VYs1.js";import{l as w}from"./googleMapsLoader-CN5raH6p.js";function u(e,r,a=!1){const o=e.find(d=>d.types.includes(r));return o?(a?o.short_name:o.long_name)?.trim()??"":""}function V(e,r,a){const o=u(e,"street_number"),d=u(e,"route"),s=[o,d].filter(Boolean).join(" ").trim(),l=u(e,"locality")||u(e,"postal_town")||u(e,"administrative_area_level_2")||u(e,"sublocality")||u(e,"neighborhood"),i=u(e,"country");return{streetAddress:s||a?.trim()||r.split(",")[0]?.trim()||r,city:l,country:i,formattedAddress:r}}async function B(e,r,a){const d=await(await w(r)).importLibrary("places"),s=new d.Autocomplete(e,{fields:["formatted_address","address_components","geometry","name"],types:["address"],componentRestrictions:{country:"za"}}),l=s.addListener("place_changed",()=>{const i=s.getPlace(),p=i.formatted_address?.trim();if(!p)return;const t=V(i.address_components??[],p,i.name);a(t)});return()=>{l.remove(),typeof google<"u"&&google.maps?.event&&google.maps.event.clearInstanceListeners(s)}}const C=["id","value","placeholder","autocomplete"],M=f({__name:"AddressAutocompleteInput",props:{modelValue:{},city:{},country:{},placeholder:{},inputClass:{},googleMapsApiKey:{},id:{},autocomplete:{}},emits:["update:modelValue","update:city","update:country"],setup(e,{emit:r}){const a=e,o=r,d=v(),s=g(null);let l;const i=()=>a.googleMapsApiKey?.trim()||d.props.google_maps_api_key?.trim()||"";async function p(){l?.(),l=void 0;const t=s.value,n=i();if(!(!t||!n))try{l=await B(t,n,c=>{o("update:modelValue",c.streetAddress),a.city!==void 0&&o("update:city",c.city),a.country!==void 0&&o("update:country",c.country)})}catch{}}return _(()=>{m(()=>{p()})}),y(()=>[i(),s.value],()=>{m(()=>{p()})}),y(()=>a.modelValue,t=>{const n=s.value;if(!n||document.activeElement===n)return;const c=t??"";n.value!==c&&(n.value=c)}),h(()=>{l?.(),l=void 0}),(t,n)=>(A(),b("input",{id:t.id,ref_key:"inputRef",ref:s,type:"text",value:t.modelValue,placeholder:t.placeholder,autocomplete:t.autocomplete??"street-address",class:k(t.inputClass),onInput:n[0]||(n[0]=c=>o("update:modelValue",c.target.value))},null,42,C))}});export{M as _};
