import{getAddressLabels,transformPlaceDetails,getStreetAddress}from"../../../../functions/google-maps";import{getCountryRegions}from"../../../../functions/address";const HTML_ESCAPE_MAP={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#39;"},escapeHtml=e=>e.replace(/[&<>"']/g,(e=>HTML_ESCAPE_MAP[e])),escapeRegex=e=>e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");export function highlightMatch(e,o){if(!e||!o)return escapeHtml(e||"");const l=escapeHtml(e),n=o.split(/\s+/).filter((e=>e)).map(escapeRegex);if(0===n.length)return l;try{const e=new RegExp(`(${n.join("|")})`,"gi");return l.replace(e,"<strong>$1</strong>")}catch(e){return l}}export async function fetchAddressSuggestions(e,o,l,n){var t,s;const a=await fetch("https://places.googleapis.com/v1/places:searchText",{method:"POST",headers:{"Content-Type":"application/json","X-Goog-Api-Key":null===(t=null===window||void 0===window?void 0:window.scData)||void 0===t?void 0:t.google_map_api_key,"X-Goog-FieldMask":"places.id,places.displayName,places.types,places.primaryType,places.primaryTypeDisplayName,places.addressComponents"},body:JSON.stringify({textQuery:e,pageSize:5,regionCode:o}),signal:n}),i=await a.json();if(null===(s=null==i?void 0:i.error)||void 0===s?void 0:s.message)throw new Error(i.error.message);return((null==i?void 0:i.places)||[]).map((o=>{var n,t,s,a;const{city:i,state:d,country:r}=getAddressLabels((null==o?void 0:o.addressComponents)||[],l);return{displayName:null!==(t=null===(n=null==o?void 0:o.displayName)||void 0===n?void 0:n.text)&&void 0!==t?t:e,fullDisplayName:[null!==(a=null===(s=null==o?void 0:o.displayName)||void 0===s?void 0:s.text)&&void 0!==a?a:e,i,d,r].filter(Boolean).join(", "),placeId:null==o?void 0:o.id,addressComponents:(null==o?void 0:o.addressComponents)||null}}))}export function buildReplacementAddressFromPlace(e,o,l){var n,t,s,a,i,d;if(!(null===(n=null==e?void 0:e.addressComponents)||void 0===n?void 0:n.length))throw new Error("Place details not found.");const r=transformPlaceDetails(e.addressComponents,o);return{name:null!=l?l:null,line_1:getStreetAddress(e.addressComponents)||e.displayName||"",line_2:null!==(t=r.line_2)&&void 0!==t?t:null,city:null!==(s=r.city)&&void 0!==s?s:null,state:null!==(a=r.state)&&void 0!==a?a:null,postal_code:null!==(i=r.postal_code)&&void 0!==i?i:null,country:null!==(d=r.country)&&void 0!==d?d:null}}export async function fetchPlaceDetails(e,o,l,n){var t;const s=o.find((o=>o.placeId===e));if(!(null==s?void 0:s.addressComponents))throw new Error("Place details not found.");const{addressComponents:a}=s,i=(null===(t=a.find((e=>{var o;return null===(o=e.types)||void 0===o?void 0:o.includes("country")})))||void 0===t?void 0:t.shortText)||null,d=(null==l?void 0:l.country)!==i?await getCountryRegions(i):n;return{updatedAddress:buildReplacementAddressFromPlace(s,d,l.name),updatedRegions:d}}