const n=(n,o)=>(n||[]).find((n=>{var l;return null===(l=n.types)||void 0===l?void 0:l.includes(o)})),o=o=>{var l,t;return null!==(t=null!==(l=n(o,"locality"))&&void 0!==l?l:n(o,"administrative_area_level_2"))&&void 0!==t?t:n(o,"postal_town")};function l(o){var l,t;return o?[(null===(l=n(o,"street_number"))||void 0===l?void 0:l.longText)||"",(null===(t=n(o,"route"))||void 0===t?void 0:t.longText)||""].filter(Boolean).join(" "):""}const t=n=>(n||"").replace(/\.$/,"").trim().normalize("NFD").replace(/[\u0300-\u036f]/g,"").toLowerCase(),i=(n,o)=>{if(!n||!(null==o?void 0:o.length))return;const l=o.find((o=>o.value===n.shortText));if(l)return l;const e=t(n.longText);if(e){const n=o.find((n=>t(n.label)===e));if(n)return n}},u=(o,l)=>{var t,e,u;const r=null!==(t=n(o,"administrative_area_level_1"))&&void 0!==t?t:null,a=null!==(e=n(o,"administrative_area_level_2"))&&void 0!==e?e:null,s=null!==(u=i(r,l))&&void 0!==u?u:i(a,l);return s?{shortText:s.value,longText:s.label}:{longText:(null==r?void 0:r.longText)||null,shortText:null}};function r(l,t){var e,i,r,a,s;return{line_2:(null===(e=n(l,"sublocality"))||void 0===e?void 0:e.shortText)||null,postal_code:(null===(i=n(l,"postal_code"))||void 0===i?void 0:i.shortText)||null,city:(null===(r=o(l))||void 0===r?void 0:r.longText)||null,state:(null===(a=u(l,t))||void 0===a?void 0:a.shortText)||null,country:(null===(s=n(l,"country"))||void 0===s?void 0:s.shortText)||null}}function e(l,t){var e,i;const r=null!==(e=n(l,"country"))&&void 0!==e?e:null;if(!r)return{country:null,state:null,city:null};const a=u(l,t),s={country:r.longText||null,state:(null==a?void 0:a.longText)||null,city:(null===(i=o(l))||void 0===i?void 0:i.longText)||null};switch(null==r?void 0:r.shortText){case"US":return{...s,country:"USA",state:(null==a?void 0:a.shortText)||null};case"GB":return{...s,country:"UK"};default:return s}}async function d(){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 v(n,o){var l;return(await fetch(`https://maps.googleapis.com/maps/api/geocode/json?latlng=${n},${o}&key=${null===(l=null===window||void 0===window?void 0:window.scData)||void 0===l?void 0:l.google_map_api_key}`)).json()}async function a(){var n,o,l,t,e;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 d();if(!(null==n?void 0:n.location))return null;const{lat:i,lng:u}=n.location,r=await v(i,u);if(null==r?void 0:r.error_message)return null;const a=(null===(e=null===(t=null===(l=null===(o=null==r?void 0:r.results)||void 0===o?void 0:o[0])||void 0===l?void 0:l.address_components)||void 0===t?void 0:t.find((n=>n.types.includes("country"))))||void 0===e?void 0:e.short_name)||null;return a&&sessionStorage.setItem("surecart_user_country",a),a}catch{return null}}export{e as a,l as b,a as g,r as t};