const findAddressComponent=(n,o)=>(n||[]).find((n=>{var t;return null===(t=n.types)||void 0===t?void 0:t.includes(o)})),findCity=n=>{var o,t;return null!==(t=null!==(o=findAddressComponent(n,"locality"))&&void 0!==o?o:findAddressComponent(n,"administrative_area_level_2"))&&void 0!==t?t:findAddressComponent(n,"postal_town")};export function getStreetAddress(n){var o,t;return n?[(null===(o=findAddressComponent(n,"street_number"))||void 0===o?void 0:o.longText)||"",(null===(t=findAddressComponent(n,"route"))||void 0===t?void 0:t.longText)||""].filter(Boolean).join(" "):""}const normalizeForMatch=n=>(n||"").replace(/\.$/,"").trim().normalize("NFD").replace(/[\u0300-\u036f]/g,"").toLowerCase(),findRegionMatch=(n,o)=>{if(!n||!(null==o?void 0:o.length))return;const t=o.find((o=>o.value===n.shortText));if(t)return t;const e=normalizeForMatch(n.longText);if(e){const n=o.find((n=>normalizeForMatch(n.label)===e));if(n)return n}},getState=(n,o)=>{var t,e,l;const i=null!==(t=findAddressComponent(n,"administrative_area_level_1"))&&void 0!==t?t:null,r=null!==(e=findAddressComponent(n,"administrative_area_level_2"))&&void 0!==e?e:null,d=null!==(l=findRegionMatch(i,o))&&void 0!==l?l:findRegionMatch(r,o);return d?{shortText:d.value,longText:d.label}:{longText:(null==i?void 0:i.longText)||null,shortText:null}};export function transformPlaceDetails(n,o){var t,e,l,i,r;return{line_2:(null===(t=findAddressComponent(n,"sublocality"))||void 0===t?void 0:t.shortText)||null,postal_code:(null===(e=findAddressComponent(n,"postal_code"))||void 0===e?void 0:e.shortText)||null,city:(null===(l=findCity(n))||void 0===l?void 0:l.longText)||null,state:(null===(i=getState(n,o))||void 0===i?void 0:i.shortText)||null,country:(null===(r=findAddressComponent(n,"country"))||void 0===r?void 0:r.shortText)||null}}export function getAddressLabels(n,o){var t,e;const l=null!==(t=findAddressComponent(n,"country"))&&void 0!==t?t:null;if(!l)return{country:null,state:null,city:null};const i=getState(n,o),r={country:l.longText||null,state:(null==i?void 0:i.longText)||null,city:(null===(e=findCity(n))||void 0===e?void 0:e.longText)||null};switch(null==l?void 0:l.shortText){case"US":return{...r,country:"USA",state:(null==i?void 0:i.shortText)||null};case"GB":return{...r,country:"UK"};default:return r}}async function fetchGeoLocation(){var n;return(await fetch(`https://www.googleapis.com/geolocation/v1/geolocate?key=${null===(n=null===window||void 0===window?void 0:window.scData)||void 0===n?void 0:n.google_map_api_key}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({considerIp:!0})})).json()}async function fetchCountryFromCoordinates(n,o){var t;return(await fetch(`https://maps.googleapis.com/maps/api/geocode/json?latlng=${n},${o}&key=${null===(t=null===window||void 0===window?void 0:window.scData)||void 0===t?void 0:t.google_map_api_key}`)).json()}export async function getCurrentUserCountryCode(){var n,o,t,e,l;if(!(null===(n=null===window||void 0===window?void 0:window.scData)||void 0===n?void 0:n.google_map_api_key))return null;const i=sessionStorage.getItem("surecart_user_country");if(i)return i;try{const n=await fetchGeoLocation();if(!(null==n?void 0:n.location))return null;const{lat:i,lng:r}=n.location,d=await fetchCountryFromCoordinates(i,r);if(null==d?void 0:d.error_message)return null;const s=(null===(l=null===(e=null===(t=null===(o=null==d?void 0:d.results)||void 0===o?void 0:o[0])||void 0===t?void 0:t.address_components)||void 0===e?void 0:e.find((n=>n.types.includes("country"))))||void 0===l?void 0:l.short_name)||null;return s&&sessionStorage.setItem("surecart_user_country",s),s}catch{return null}}