var __defProp=Object.defineProperty,__defNormalProp=(t,e,n)=>e in t?__defProp(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n,__publicField=(t,e,n)=>__defNormalProp(t,"symbol"!=typeof e?e+"":e,n);!function(){"use strict";function t(t){try{return JSON.parse(t),!0}catch(e){return!1}}class e extends HTMLElement{constructor(){super(),__publicField(this,"HOST","https://xapps-geo-ca956fdeab0c.herokuapp.com"),__publicField(this,"MAIN_CSS","native-geo-redirects.min.css"),__publicField(this,"SHOW_RULES",{load:"everyload",cookie:"cookie",session:"session"}),__publicField(this,"VARS",{KEY:"ngr-session",CLOSED_KEY:"ngr-closed",REDIRECTED_KEY:"ngr-popup-redirected",DOMAIN_REDIRECT_KEY:"ngr-domain-redirect"}),__publicField(this,"OLD_ICON_PATH","https://ngr-app.herokuapp.com/public/images/earth-americas-solid.svg"),__publicField(this,"OLD_STICKY_PATH","https://ngr-app.herokuapp.com/public/images/sticky-logo.png"),this.userGeoData,this.modal=null,this.testMode=!1,this.marketCountries=null,this.domainRedirection=!1,this.flagSrc=this.dataset.flagSrc,this.hasMarkets=this.dataset.markets,this.widgetIcon=this.dataset.widgetIcon,this.shopifyTemplate=this.dataset.shopifyTemplate,this.lng=this.dataset.lng||window.Shopify.locale,this.widgetStickyIcon=this.dataset.widgetStickyIcon,this.shopifytemplateDir=this.dataset.shopifyTemplateDir,this.userLocale=this.dataset.lng||window.Shopify.locale,this.customOpenerIconSrc=this.dataset.customOpenerIconSrc,this.template=this.querySelector("#NGR-Redirects-Template"),this.stylesheet=document.querySelector(`[href*='${this.MAIN_CSS}']`)}async connectedCallback(){var t,e,n,o;if(this.checkTestMode(),this.clearLocalData(),!this.stylesheet)return void console.error("[NGR APP]: Stylesheet not found. Contact app support.");const i=await this.getData();if(!i||!i&&!i.status||!i.data)return void console.error("[NGR APP]: Store data not found. Contact app support.");const s=await this.getGeo();if(!s||!(null==s?void 0:s.country))return void console.error("[NGR APP]: GEO Location not detected. Contact app support.");this.userGeoData=s;const{redirects:l,configs:r}=i.data;if(!r||!(null==l?void 0:l.length))return void console.warn("[NGR APP]: Store data issue or plan not picked yet or no widget redirects configured.");if(this.validatePageShow(r))return;if(this.topBarMoveTop(null==(t=null==r?void 0:r.basicConfigs)?void 0:t.type))return;const a=this.widget(l,r);if(!a)return void console.warn("[NGR APP]: Widget not found. Contact app support.");const c=this.genCustomStyles(r),d=this.attachShadow({mode:"closed"});d.appendChild(this.stylesheet),c&&d.appendChild(c),d.appendChild(a),this.customOpenerIcon(),this.widgetEvents(d,null==(e=null==r?void 0:r.basicConfigs)?void 0:e.show,null==(n=null==r?void 0:r.basicConfigs)?void 0:n.plan);if(this.querySelector("[data-ngr-widget-code]"))try{const t=(null==(o=null==r?void 0:r.basic_configs)?void 0:o.show)===this.SHOW_RULES.cookie?this.getCookie(this.VARS.CLOSED_KEY):sessionStorage.getItem(this.VARS.CLOSED_KEY);showWidgetCustomCode(s,this.openModal.bind(this),t)}catch(u){console.error("[NGR APP]: Widget editor custom code error ",u)}else await this.widgetLogic(d,null==r?void 0:r.basicConfigs);this.displayWidget()}checkTestMode(){(window.location.search.includes("ngr-test")||"#ngr-test"===window.location.hash||"true"===this.dataset.testMode&&window.Shopify.designMode)&&(this.testMode=!0)}clearLocalData(){"#ngr-clear-user-data"===window.location.hash&&(this.deleteCookie(this.VARS.KEY),this.deleteCookie(this.VARS.CLOSED_KEY),sessionStorage.removeItem(this.VARS.KEY),sessionStorage.removeItem(this.VARS.CLOSED_KEY))}async getData(){var t;const e=null==(t=null==window?void 0:window.Shopify)?void 0:t.shop,n=`${this.HOST}/api/shop?shop=${e}`;return await fetch(n).then(t=>t.json())}async getGeo(){var e,n,o,i,s;const l=this.getCookie(this.VARS.KEY);if(l&&t(l)&&!this.testMode)return JSON.parse(l);const r=null==window?void 0:window.ngr_countries_window,a=await fetch("/browsing_context_suggestions.json").then(t=>t.json());if(!r||!a)return console.error("[NGR APP]: User GEO location or countries list not detected. Contact app support please.");const c=null==(e=null==a?void 0:a.detected_values)?void 0:e.country.handle,d={country_name:(null==(o=null==(n=null==a?void 0:a.detected_values)?void 0:n.country)?void 0:o.name)||(null==(i=null==a?void 0:a.detected_values)?void 0:i.country_name),country:c,continent:null==(s=r[c])?void 0:s.continent};return this.setCookie(this.VARS.KEY,JSON.stringify(d),7),window[this.VARS.KEY]=d,d}validatePageShow({allowedPages:t,hideOnAllowedPages:e}){if(t&&"null"!==t&&t.length){const n=t.filter(t=>t.includes("http")),o=window.location.origin+window.location.pathname,i=t.includes("all")||t.includes(this.shopifyTemplate)||n.includes(o)||"customers"===this.shopifytemplateDir&&t.join(",").includes("account pages");if(e&&i||!e&&!i)return console.info("[NGR-APP]: Widget is not allowed on this page. Check app configs."),!0}}topBarMoveTop(t){return"topbar"===t&&!this.hasAttribute("data-moved")&&(this.setAttribute("data-moved",""),document.body.insertBefore(this,document.body.firstChild),!0)}widget(t,e){var n,o;if(!t||!t.length||!e)return;const{basicConfigs:i,advancedConfigs:s,plan:l}=e;this.domainRedirection=(null==i?void 0:i.domain_redirection)||!1;const r=null==(o=null==(n=this.template)?void 0:n.content)?void 0:o.cloneNode(!0),a=r.querySelector("[data-ngr-modal]");return this.modal=a,2===l&&s&&""!==(null==s?void 0:s.html_id)&&a.setAttribute("id",null==s?void 0:s.html_id),this.builder(r,i,t,l)}genCustomStyles(t){if(!t)return;const{basicConfigs:e,advancedConfigs:n,plan:o}=t,i=document.createElement("style");let s="";if(e&&!(null==n?void 0:n.disable_basic_css)&&3!==o){const{modalBgColor:t,modalTextColor:n,modalBorderColor:o,buttonsBgColor:i,buttonsColor:l,font:r}=e;s+=`\n .ngr-modal{\n ${r&&""!=r&&"inherit"!==r&&"undefined"!==r?"font-family:'"+r+"', sans-serif;":""}\n }\n .ngr-modal__content{\n ${""!=t?"background-color:"+t+";":""}\n ${""!=n?"color:"+n+";":""}\n ${""!=o?"border-color:"+o+";":""}\n }\n .ngr-modal__close{\n ${""!=t?"background-color:"+t+" !important;":""}\n ${""!=n?"color:"+n+";":""}\n }\n .ngr-redirects__link{\n ${""!=i?"background-color:"+i+";":""}\n ${""!=l?"border-color:"+l+";":""}\n ${""!=l?"color:"+l+";":""}\n }\n @media screen and (min-width: 768px) {\n .ngr-redirects__link:hover{\n ${""!=l?"background-color:"+l+";":""}\n ${""!=i?"border-color:"+i+";":""}\n ${""!=i?"color:"+i+";":""}\n }\n }\n .ngr-modal__content .ngr-select{\n ${""!=i?"background-color:"+i+";":""}\n }\n .ngr-modal__content .ngr-select__label{\n ${""!=l?"color:"+l+";":""}\n ${""!=l?"border-color:"+l+";":""}\n }\n .ngr-select::before{\n ${""!=l?"background-color:"+l+";":""}\n }\n `}return n&&""!==(null==n?void 0:n.css)&&2===o&&(s+=null==n?void 0:n.css),i.textContent=s.replace(/\n/g,"").replace(/ +/g,""),i}customOpenerIcon(){var t;const e=document.querySelectorAll("[href$='#ngr-open']");if(this.customOpenerIconSrc&&""!==this.customOpenerIconSrc&&(null==e?void 0:e.length)){const n=this.builOpenerIconElement(this.customOpenerIconSrc);if(!n)return;for(let o=0;o{o.closeModal(i,e)}),t.addEventListener("click",t=>{if(t){const n=t.target.closest("[data-ngr-button]");n&&(l(t),r(t),a()&&(t.preventDefault(),setTimeout(()=>{n.href&&(window.location.href=n.href)},50))),function(t){t.target.closest("[data-ngr-toggle]")&&(t.preventDefault(),o.toggleModal(i,e))}(t)}}),a()&&(t.addEventListener("touchstart",t=>{const e=t.target.closest("[data-ngr-button]");e&&(e.style.opacity="0.8",e.style.transform="scale(0.98)")},{passive:!0}),t.addEventListener("touchend",t=>{const e=t.target.closest("[data-ngr-button]");e&&(e.style.opacity="",e.style.transform="",0===t.touches.length&&(t.preventDefault(),l(t),r(t),setTimeout(()=>{e.href&&(window.location.href=e.href)},10)))},{passive:!1})),document.addEventListener("click",t=>{var e,n;if(t){((null==(e=t.target)?void 0:e.closest("[data-ngr-open]"))||(null==(n=t.target)?void 0:n.closest("[href$='#ngr-open']")))&&(t.preventDefault(),o.openModal())}})}async widgetLogic(e,n){var o,i,s,l,r;if(!n||!e)return console.error("[NGR APP]: Configs or shadowRoot list not found. Contact app support please.");const{automaticShow:a,geo:c,show:d}=n;if(this.testMode)this.openModal();else if(a)if(window.location.href.includes(this.VARS.REDIRECTED_KEY))this.setClosedCookieOrSession(d);else{if(c){const e=this.getCookie(this.VARS.KEY);if(e&&t(e)){const t=JSON.parse(e);return void this.widgetBehaviour(n,t)}const a=await this.getCountriesJSON(),c=await fetch("/browsing_context_suggestions.json").then(t=>t.json());if(!a||!c)return console.error("[NGR APP]: User GEO location or countries list not detected. Contact app support please.");const d=null==(o=null==c?void 0:c.detected_values)?void 0:o.country.handle,u={country_name:(null==(s=null==(i=null==c?void 0:c.detected_values)?void 0:i.country)?void 0:s.name)||(null==(l=null==c?void 0:c.detected_values)?void 0:l.country_name),country:d,continent:null==(r=a[d])?void 0:r.continent};return this.setCookie(this.VARS.KEY,JSON.stringify(u),7),window[this.VARS.KEY]=u,void this.widgetBehaviour(n,u)}this.widgetBehaviour(n)}}widgetBehaviour(t,e){const n=null==t?void 0:t.show;if(n===this.SHOW_RULES.load&&!e)return this.openModal();const{location:o,countries:i,continents:s,reverseGeo:l,geo:r,automaticShow:a}=t,{country:c,continent:d}=e||{},u=t=>{if(t===this.SHOW_RULES.load)this.openModal();else{(t===this.SHOW_RULES.cookie?this.getCookie(this.VARS.CLOSED_KEY):sessionStorage.getItem(this.VARS.CLOSED_KEY))||this.openModal()}};!r&&a?u(n):"country"===o?l?(null==i?void 0:i.includes(c))&&u(n):(null==i?void 0:i.includes(c))||u(n):l?(null==s?void 0:s.includes(d))&&u(n):(null==s?void 0:s.includes(d))||u(n)}builder(t,e,n,o){var i,s;if(!t)return;const{title:l,title_locales:r,text:a,text_locales:c,topbarSticky:d,stickyOpener:u,stickyVerticalPosition:h,icon:g,iconWidth:p,stickyToggleIcon:m,type:f,showFlag:v,layout:w,dropdownPlaceholder:b,dropdownPlaceholder_locales:S,custom_rell_attr:_,forward_url_params:y}=e,C=t.querySelector("[data-ngr-modal-content]");C&&C.classList.add(w);const E=this.getUserData(),A=(null==E?void 0:E.country_name)||(null==(i=null==window?void 0:window.ngr_countries_window[null==E?void 0:E.country])?void 0:i.name),O=null==(s=null==window?void 0:window.ngr_countries_window[null==E?void 0:E.country])?void 0:s.name;let k=l,L=a,R=b;2===o&&(k=this.getTranslation(l,r),L=this.getTranslation(a,c),R=this.getTranslation(b,S)),A&&(L=L.replace(/\[\[country\]\]/g,A)),O&&(L=L.replace(/\[\[country_eng\]\]/g,O));const T=t.querySelector("[data-ngr-close]"),P=this.buildStickyIconElement(m,u,null==E?void 0:E.country),I=this.buildIconElement(g,p),$=this.buildTitleElement(k),D=this.buildTextElement(L,o),x=this.buildRedirectsElement(n,E,w,R,_,y),M=this.buildFlagElement(v,null==E?void 0:E.country),N=t.querySelector("[data-ngr-redirects]");return N?("topbar"===f&&(this.modal.classList.add("top-bar"),d&&(this.style.top="0",this.style.position="sticky",this.style.zIndex="999999999999")),"sticky"===f&&(this.modal.classList.add("sticky-bar"),this.modal.style.top=h+"%",setTimeout(()=>{this.modal.classList.add("transition")},2500)),P&&"sticky"===f&&T&&(T.removeAttribute("data-ngr-close"),T.setAttribute("data-ngr-toggle",""),T.innerHTML="",T.appendChild(P)),x&&N.appendChild(x),I&&N.insertAdjacentElement("beforebegin",I),$&&N.insertAdjacentElement("beforebegin",$),D&&N.insertAdjacentElement("beforebegin",D),M&&N.insertAdjacentElement("beforebegin",M),t):void 0}buttonAnalytics(){var t;const e=null==(t=null==window?void 0:window.Shopify)?void 0:t.shop,n=`${this.HOST}/api/analytics/buttons/?shop=${e}`;fetch(n)}builOpenerIconElement(t){if(t&&""!==t)return`geo location icon`}buildStickyIconElement(t,e,n){var o;t="geo"===e&&n?null==(o=this.flagSrc)?void 0:o.replace(/\b([a-z]+)\.svg/,`${n.toLowerCase()}.svg`):"default"===t||t===this.OLD_STICKY_PATH?this.widgetStickyIcon:t;const i=document.createElement("img");return i.loading="lazy",i.alt="Geo location toggler",i.width=100,i.height=100,i.src=t,i}buildIconElement(t,e=100,n="Redirects Icon"){if(!t||""===t)return;const o=document.createElement("img");o.loading="lazy",o.alt=n,o.width="auto"===e?100:e,o.height=100,o.src="default"===t||t===this.OLD_ICON_PATH?this.widgetIcon:t,"auto"===e&&o.classList.add("ngr-modal__icon-img-auto");const i=document.createElement("div");return i.classList.add("ngr-modal__icon"),i.appendChild(o),i}buildTitleElement(t){if(!t||""===t)return;const e=document.createElement("h2");return e.classList.add("ngr-modal__title"),e.textContent=t,e}buildTextElement(t,e){if(!t||""===t||3===e||"


"===t||"


"===t)return;const n=document.createElement("div");return n.classList.add("ngr-modal__text"),n.innerHTML=t,n}buildRedirectsElement(t,e,n,o,i,s){var l,r,a,c;if(!t||!(null==t?void 0:t.length))return;const d=["/products","/collections","/search","/cart","/checkout","/account","/blogs","/pages"];function u(t){return t.replace(/(^\w+:|^)\/\//,"").replace(/^www\./,"").replace(/[?#].*$/,"").replace(/\/$/,"")}function h(t,e,n){const o=new URL(t);if(e&&(!function(t){var e,n;const o=new URL(t).pathname,i=/^\/[a-z]{2,3}(-[a-z]{2,3})?\/?$/i.test(o),s=(null==window?void 0:window.location.pathname)+"/"===(null==(n=null==(e=null==window?void 0:window.Shopify)?void 0:e.routes)?void 0:n.root);return i&&!s}(t)?o.pathname=e:o.pathname+=function(t){for(let e of d){let n=t.indexOf(e);if(-1!==n)return t.substring(n)}return t}(e)),n){const t=function(t){return t.replace(/([?&])preview_theme_id=\d+(&?)/,(t,e,n)=>"?"===e&&n?"?":e)}(window.location.search);o.search=t}return o}function g(t,e){if(!t||"-"===t)return;const n=document.createElement("img");return n.loading="lazy",n.alt=e,n.width=50,n.height=50,n.src=t,n}function p(t,e,n,o,i,s,l){if(o&&i){if("dropdown"===t){const t=document.createElement("option");return t.value=i,t.text=o,t.setAttribute("data-ngr-button",""),t.setAttribute("data-icon",e),s&&t.setAttribute("data-isClose",""),t}{const t=document.createTextNode(o),e=document.createElement("li");e.classList.add("ngr-redirects__item");const r=document.createElement("a");return r.href=i,r.classList.add("ngr-redirects__link"),r.setAttribute("data-ngr-button",""),n&&r.appendChild(n),r.appendChild(t),s&&r.setAttribute(s,""),l&&""!==l&&r.setAttribute("rel",l),e.appendChild(r),e}}}const m=document.createDocumentFragment();for(let v=0;v1&&t.classList.add("ngr-redirects__single"),t.appendChild(n),t}}(n,o,m,t.length)}buildFlagElement(t,e){if(!t||!e||""===e||!this.flagSrc||""===this.flagSrc)return;const n=this.flagSrc.replace(/\b([a-z]+)\.svg/,`${e.toLowerCase()}.svg`);console.log("flagSrc",n);const o=document.createElement("div");o.classList.add("ngr-modal__flag");const i=document.createElement("img");return i.loading="lazy",i.alt=e,i.width=100,i.height=50,i.src=n,o.appendChild(i),o}async getCountriesJSON(){const t=localStorage.getItem("ngr_countries");if(t)return JSON.parse(t);const e=`${this.HOST}/api/countries`;try{const t=await fetch(e).then(t=>t.json());return localStorage.setItem("ngr_countries",JSON.stringify({...t})),t}catch(n){return!1}}getLabel(t){return t?t.locales&&t.locales[this.lng]||t.label:""}getTranslation(t,e){return t&&(e&&e[this.lng]||t)||""}displayWidget(){this.style.display="initial"}openModal(t){const e=t||this.modal;e&&e.setAttribute("data-open","")}closeModal(t,e){const n=t||this.modal;n&&(n.removeAttribute("data-open"),this.setClosedCookieOrSession(e))}setClosedCookieOrSession(t){t&&t===this.SHOW_RULES.session?sessionStorage.setItem(this.VARS.CLOSED_KEY,1):t&&t===this.SHOW_RULES.cookie&&this.setCookie(this.VARS.CLOSED_KEY,1,7)}toggleModal(t,e){const n=t||this.modal;n&&(n.hasAttribute("data-open")?this.closeModal(t,e):this.openModal(t))}getUserData(){return JSON.parse(this.getCookie(this.VARS.KEY))||(null==window?void 0:window[this.VARS.KEY])}stickyPosition(t,e=50){t.style.top=e+"%"}setCookie(t,e,n){var o="";if(n){var i=new Date;i.setTime(i.getTime()+24*n*60*60*1e3),o="; expires="+i.toUTCString()}document.cookie=t+"="+(encodeURIComponent(e)||"")+o+"; path=/"}getCookie(t){for(var e=t+"=",n=document.cookie.split(";"),o=0;ot.selected),this.setAttribute("tabindex","0"),this.renderSelect(),this.addEventListener("click",this.onClick)}renderSelect(){this.select.setAttribute("hidden",!0);const t=document.createElement("ul");this.currentOption=document.createElement("div"),this.currentOption.classList.add("ngr-select__label"),this.currentOption.textContent=this.placeholder,this.options.forEach((e,n)=>{var o;const i=document.createElement("li"),s=document.createElement("a"),l=document.createElement("span"),r=null==e?void 0:e.getAttribute("data-icon");let a=null;r&&""!==r&&(a=document.createElement("img"),a.setAttribute("src",e.getAttribute("data-icon")),a.setAttribute("alt",e.textContent),a.setAttribute("width","50px"),a.setAttribute("height","50px"),a.setAttribute("loading","lazy")),l.textContent=e.textContent,s.setAttribute("href",e.value),s.setAttribute("data-ngr-button","");const c=e.value.replace(/(^\w+:|^)\/\//,"").replace(/[?#].*$/,"").replace(/\/$/,"");(null==(o=null==window?void 0:window.location)?void 0:o.hostname)===c&&s.setAttribute("data-ngr-toggle",""),i.setAttribute("data-index",n),i.setAttribute("tabindex","0"),a&&s.appendChild(a),s.appendChild(l),i.appendChild(s),e.disabled&&i.setAttribute("disabled",!0),e.hidden&&i.setAttribute("hidden",!0),e.selected&&(this.$selectedOption=i),t.appendChild(i)}),this.select.parentElement.append(this.currentOption,t),this.toggleAttribute("data-custom-select-selected",!this.select.value.includes("reset"))}onClick(t){this.toggleAttribute("open");const e=t.target;e.hasAttribute("data-href")&&(this.$selectedOption=e,this.currentOption.innerHTML=this.$selectedOption.querySelector("a").innerHTML,this.select.selectedIndex=this.$selectedOption.getAttribute("data-index"),this.select.dispatchEvent(new Event("change",{bubbles:!0})),this.toggleAttribute("data-custom-select-selected",!this.select.value.includes("reset")),this.$selectedOption.querySelector("a").click())}}customElements.define("ngr-select",n)}();