var _a;import{addQueryArgs}from"@wordpress/url";export const STATE_INCLUDED_COUNTRIES=["AU","BR","CA","CH","ES","HK","IE","IN","IT","JP","MY","MX","US"];export const POSTAL_CODE_EXCLUDED_COUNTRIES=["HK"];export const CITY_EXCLUDED_COUNTRIES=["SG"];import countries from"./countries.json";export const hasPostal=t=>!POSTAL_CODE_EXCLUDED_COUNTRIES.includes(t);export const hasCity=t=>!CITY_EXCLUDED_COUNTRIES.includes(t);export const hasState=t=>STATE_INCLUDED_COUNTRIES.includes(t);export const hasCompleteAddress=t=>{const{country:o,state:a}=t;return!!hasRequiredFields(t)&&(!hasState(o)||hasCorrectState(o,a))};export const hasCorrectState=(t,o)=>((null==countries?void 0:countries[t])||[]).some((({value:t})=>t===o));export const hasRequiredFields=({city:t,country:o,line_1:a,postal_code:e,state:s})=>!(!o||hasPostal(o)&&!e||hasCity(o)&&!t||hasState(o)&&!s||!a);const lang=(null===(_a=window.scData)||void 0===_a?void 0:_a.locale)||navigator.language||(null===navigator||void 0===navigator?void 0:navigator.browserLanguage)||(navigator.languages||["en"])[0];let cachedCountryChoices=null;const countryDetailsCache=new Map;export const countryChoices=async()=>{var t,o,a,e;if(cachedCountryChoices)return cachedCountryChoices;const s=addQueryArgs("https://api.surecart.com/v1/public/atlas",{locale:lang}),n=await fetch(s),r=await n.json(),l=null===(t=(null==r?void 0:r.data)||[])||void 0===t?void 0:t.map((({code:t,name:o})=>({value:t,label:o})));return cachedCountryChoices=(null===(e=null===(a=null===(o=null===window||void 0===window?void 0:window.wp)||void 0===o?void 0:o.hooks)||void 0===a?void 0:a.applyFilters)||void 0===e?void 0:e.call(a,"surecart_address_countries",l))||l,cachedCountryChoices};export const getCountryDetails=async t=>{if(countryDetailsCache.has(t))return countryDetailsCache.get(t);const o=addQueryArgs(`https://api.surecart.com/v1/public/atlas/${t}`,{locale:lang}),a=await fetch(o),e=await a.json();return countryDetailsCache.set(t,e),e};export const getCountryRegions=async t=>{if(!t)return[];const o=await getCountryDetails(t);return((null==o?void 0:o.states)||[]).map((t=>({value:t.code,label:t.name})))};export const isAddressComplete=t=>(null==t?void 0:t.country)&&(null==t?void 0:t.line_1)&&(!hasPostal(t.country)||(null==t?void 0:t.postal_code))&&(!hasCity(t.country)||(null==t?void 0:t.city))&&(!hasState(t.country)||(null==t?void 0:t.state));export const isAddressCompleteEnough=t=>(null==t?void 0:t.country)&&(!hasPostal(t.country)||(null==t?void 0:t.postal_code))&&(!hasCity(t.country)||(null==t?void 0:t.city))&&(!hasState(t.country)||(null==t?void 0:t.state));