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AAI;QACF,MAAM,iBAAiB,GAAG,MAAM,gBAAgB,EAAE,CAAC;QACnD,IAAI,EAAC,iBAAiB,aAAjB,iBAAiB,uBAAjB,iBAAiB,CAAE,QAAQ,CAAA,EAAE;YAChC,OAAO,IAAI,CAAC;SACb;QAED,MAAM,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,iBAAiB,CAAC,QAAQ,CAAC;QAChD,MAAM,WAAW,GAAG,MAAM,2BAA2B,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;QAEhE,IAAI,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,aAAa,EAAE;YAC9B,OAAO,IAAI,CAAC;SACb;QAED,MAAM,WAAW,GAAG,CAAA,MAAA,MAAA,MAAA,MAAA,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,OAAO,0CAAG,CAAC,CAAC,0CAAE,kBAAkB,0CAAE,IAAI,CAAC,SAAS,IAAI,SAAS,CAAC,KAAK,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC,0CAAE,UAAU,KAAI,IAAI,CAAC;QAC9I,IAAI,WAAW,EAAE;YACf,cAAc,CAAC,OAAO,CAAC,uBAAuB,EAAE,WAAW,CAAC,CAAC;SAC9D;QACD,OAAO,WAAW,CAAC;KACpB;IAAC,MAAM;QACN,OAAO,IAAI,CAAC;KACb;AACH;;;;;;;","names":[],"sources":["src/functions/google-maps.ts"],"sourcesContent":["/**\n * Internal dependencies.\n */\nimport { Address, GoogleMapAddressComponents } from 'src/types';\n\n/**\n * Helper function to find an address component by type.\n */\nconst findAddressComponent = (addressComponents: Array<GoogleMapAddressComponents>, type: string): GoogleMapAddressComponents | undefined => {\n return (addressComponents || []).find(component => component.types?.includes(type));\n};\n\n/**\n * Find the city from address components using a fallback chain.\n *\n * Google Places API returns the city under different component types depending on the country:\n * - `locality` — most countries (e.g. US, CA, AU)\n * - `administrative_area_level_2` — Brazil (município), parts of Italy, etc.\n * - `postal_town` — UK, Sweden, and some other European countries\n */\nconst findCity = (addressComponents: Array<GoogleMapAddressComponents>): GoogleMapAddressComponents | undefined => {\n return (\n findAddressComponent(addressComponents, 'locality') ??\n findAddressComponent(addressComponents, 'administrative_area_level_2') ??\n findAddressComponent(addressComponents, 'postal_town')\n );\n};\n\n/**\n * Build a street address from address components (street_number + route).\n */\nexport function getStreetAddress(addressComponents: Array<GoogleMapAddressComponents> | null): string {\n if (!addressComponents) return '';\n const streetNumber = findAddressComponent(addressComponents, 'street_number')?.longText || '';\n const route = findAddressComponent(addressComponents, 'route')?.longText || '';\n return [streetNumber, route].filter(Boolean).join(' ');\n}\n\n/**\n * Normalize a string for fuzzy matching:\n * - strip trailing periods (Google abbreviates, e.g. \"Yuc.\" for \"Yucatán\")\n * - remove diacritics (Google may return \"Yucatan\" while we store \"Yucatán\")\n * - lowercase\n */\nconst normalizeForMatch = (value: string | null | undefined): string =>\n (value || '')\n .replace(/\\.$/, '')\n .trim()\n .normalize('NFD')\n .replace(/[\\u0300-\\u036f]/g, '')\n .toLowerCase();\n\n/**\n * Try to find a matching region from a Google address component.\n *\n * Match precedence:\n * 1. component.shortText === region.value (exact code match)\n * 2. component.longText ≈ region.label (case-insensitive label match)\n */\nconst findRegionMatch = (\n component: GoogleMapAddressComponents | null | undefined,\n regions: Array<{ value: string; label: string }>,\n): { value: string; label: string } | undefined => {\n if (!component || !regions?.length) return undefined;\n\n // 1. Exact code match (e.g. \"GO\" === \"GO\", \"RM\" === \"RM\").\n const byCode = regions.find(r => r.value === component.shortText);\n if (byCode) return byCode;\n\n // 2. Case-insensitive label match (e.g. \"Yucatán\" ≈ \"Yucatán\").\n const normalizedLong = normalizeForMatch(component.longText);\n if (normalizedLong) {\n const byLabel = regions.find(r => normalizeForMatch(r.label) === normalizedLong);\n if (byLabel) return byLabel;\n }\n\n return undefined;\n};\n\n/**\n * Get the state/province from address components using a fallback chain.\n *\n * Google Places maps states/provinces to different administrative levels depending on the country:\n * - `administrative_area_level_1` — most countries (US, BR, CA, AU, MX)\n * - `administrative_area_level_2` — Spain (province codes like \"B\"), Italy (province codes like \"RM\")\n *\n * For some countries (e.g. Mexico) the shortText is abbreviated (\"Yuc.\") while SureCart stores\n * ISO codes (\"YUC\"), so we also attempt a label-based match using the longText.\n */\nconst getState = (addressComponents: Array<GoogleMapAddressComponents>, regions: Array<{ value: string; label: string }>) => {\n const adminLevel1 = findAddressComponent(addressComponents, 'administrative_area_level_1') ?? null;\n const adminLevel2 = findAddressComponent(addressComponents, 'administrative_area_level_2') ?? null;\n\n // Try level 1 first (covers US, BR, CA, AU, MX, etc.), then fall back to level 2 (covers ES, IT).\n const region = findRegionMatch(adminLevel1, regions) ?? findRegionMatch(adminLevel2, regions);\n\n if (!region) {\n return {\n longText: adminLevel1?.longText || null, // for preview in the address suggestion.\n shortText: null,\n };\n }\n\n return {\n shortText: region.value,\n longText: region.label,\n };\n};\n\n/**\n * Transforms the place address components into an address object.\n */\nexport function transformPlaceDetails(addressComponents: Array<GoogleMapAddressComponents>, regions: Array<{ value: string; label: string }>): Address {\n return {\n line_2: findAddressComponent(addressComponents, 'sublocality')?.shortText || null,\n postal_code: findAddressComponent(addressComponents, 'postal_code')?.shortText || null,\n city: findCity(addressComponents)?.longText || null,\n state: getState(addressComponents, regions)?.shortText || null,\n country: findAddressComponent(addressComponents, 'country')?.shortText || null,\n };\n}\n\n/**\n * Transforms the place address components into an address object for display.\n */\nexport function getAddressLabels(\n addressComponents: Array<GoogleMapAddressComponents>,\n regions: Array<{ value: string; label: string }>,\n): { country: string | null; state: string | null; city: string | null } {\n const country = findAddressComponent(addressComponents, 'country') ?? null;\n if (!country) {\n return {\n country: null,\n state: null,\n city: null,\n };\n }\n\n const state = getState(addressComponents, regions);\n const labels = {\n country: country.longText || null,\n state: state?.longText || null,\n city: findCity(addressComponents)?.longText || null,\n };\n\n switch (country?.shortText) {\n case 'US':\n return {\n ...labels,\n country: 'USA',\n state: state?.shortText || null,\n };\n\n case 'GB':\n return {\n ...labels,\n country: 'UK',\n };\n\n default:\n return labels;\n }\n}\n\n/**\n * Fetch the user's geolocation using the Google Geolocation API.\n */\nasync function fetchGeoLocation() {\n const response = await fetch(`https://www.googleapis.com/geolocation/v1/geolocate?key=${window?.scData?.google_map_api_key}`, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify({\n considerIp: true,\n }),\n });\n return response.json();\n}\n\n/**\n * Fetch the country information based on latitude and longitude using the Google Geocode API.\n */\nasync function fetchCountryFromCoordinates(lat: number, lng: number) {\n const response = await fetch(`https://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${lng}&key=${window?.scData?.google_map_api_key}`);\n return response.json();\n}\n\n/**\n * Get the user's country code based on Google Geolocation and GeoCode APIs.\n * Caches the result in sessionStorage to avoid repeat API calls.\n */\nexport async function getCurrentUserCountryCode() {\n if (!window?.scData?.google_map_api_key) {\n return null;\n }\n\n const cached = sessionStorage.getItem('surecart_user_country');\n if (cached) return cached;\n\n try {\n const geoLocateResponse = await fetchGeoLocation();\n if (!geoLocateResponse?.location) {\n return null;\n }\n\n const { lat, lng } = geoLocateResponse.location;\n const countryData = await fetchCountryFromCoordinates(lat, lng);\n\n if (countryData?.error_message) {\n return null;\n }\n\n const countryCode = countryData?.results?.[0]?.address_components?.find(component => component.types.includes('country'))?.short_name || null;\n if (countryCode) {\n sessionStorage.setItem('surecart_user_country', countryCode);\n }\n return countryCode;\n } catch {\n return null;\n }\n}\n"],"version":3}