var loyaltyWidgetStyles = ``;
document.querySelector('body').insertAdjacentHTML('beforeend', loyaltyWidgetStyles);
var mainHTMLString = `
`;
document.querySelector('body').insertAdjacentHTML('beforeend', mainHTMLString);
window.addEventListener('message', appstleLoyaltyIframeListener, false);
function appstleLoyaltyIframeListener(event) {
if (event?.data && typeof event?.data.indexOf === 'function' && event?.data?.indexOf('appstle_message_to_redirect_to_account') !== -1) {
window.location.href = 'https://' + window.location.host + '/account';
} else if (event?.data && typeof event?.data.indexOf === 'function' && event?.data?.indexOf('appstle_message_to_redirect_to_signup') !== -1) {
window.location.href = 'https://' + window.location.host + '/account/register';
} else if (event?.data && typeof event?.data.indexOf === 'function' && event?.data?.indexOf('appstle_message_to_redirect_to_after_copied_discount_redirect_url') !== -1) {
window.location.href = 'https://' + window.location.host + _ALConfig?.widget_setting?.afterCopiedDiscountRedirectUrl;
} else if (event?.data && typeof event?.data.indexOf === 'function' && event?.data?.indexOf('appstle_message_to_apply_discount') !== -1) {
var discountCode = (event.data.split('|'))[1];
var discountURL = location.origin + '/discount/' + discountCode;
fetch(discountURL).then(res => {
if (res.ok) {
console.log('Discount Code Applied');
const discountDetails = window?._ALConfig?.customerLoyalty && window?._ALConfig?.customerLoyalty?.rewards?.find((item) => item?.discountCode === discountCode) || null;
if(discountCode && discountDetails){
const discountData = {discountCode: discountCode || discountDetails?.discountCode, description: discountDetails?.description, customerId: discountDetails?.customerId}
document.dispatchEvent(new CustomEvent(`AppstleLoyalty:AppliedDiscountCode`, { detail: discountData}));
window.dispatchEvent(new CustomEvent(`AppstleLoyalty:AppliedDiscountCode`, { detail: discountData}));
}
} else {
console.log('Discount Code NOT Applied');
}
});
} else if (event?.data && typeof event?.data.indexOf === 'function' && event?.data?.indexOf('appstle_message_for_notification') !== -1) {
var message = event?.data?.split('|')[1];
showAppstleLoyaltyToaster(message);
} else if (event?.data && typeof event?.data.indexOf === 'function' && event?.data?.indexOf('appstle_message_for_showWidget') !== -1) {
} else if (event?.data && typeof event?.data.indexOf === 'function' && event?.data?.indexOf('appstle_widget_showHide') !== -1) {
document.querySelector('#appstle-loyalty-button').classList.remove('appstle-loyalty-hidden');
toggleWidget();
} else if (event?.data && typeof event?.data.indexOf === 'function' && event?.data?.indexOf('appstle_message_to_redirect_to_custom_url') !== -1) {
if (_ALConfig?.widget_setting?.createAccountLink) {
window.location.href = 'https://' + window.location.host + _ALConfig?.widget_setting?.createAccountLink;
}
}
}
function removeAppstleLoyaltyWidget() {
document.querySelector('#appstle-loyalty-button').classList.add('appstle-loyalty-hidden');
document.querySelector('#appstle_loyalty_iframe').classList.add('appstle-loyalty-hidden');
}
function checkForWidgetRestrictions() {
let isShowWidget = true;
if(_ALConfig?.customerLoyalty?.customerStatus === 'EXCLUDED') {
isShowWidget = false;
}
if (_ALConfig?.widget_setting?.allowedCustomersTag?.length && isShowWidget) {
let allowedTags = _ALConfig?.widget_setting?.allowedCustomersTag.split(',');
if (allowedTags?.length > 0 && !_ALConfig?.customerTags?.some((tags) => allowedTags?.includes(tags))) {
isShowWidget = false;
}
}
if (_ALConfig?.widget_setting?.restrictedCustomersTag?.length && isShowWidget) {
let restrictedTags = _ALConfig?.widget_setting?.restrictedCustomersTag?.split(',');
if (restrictedTags?.length > 0 && _ALConfig?.customerTags?.some((tags) => restrictedTags?.includes(tags))) {
isShowWidget = false;
}
}
if (_ALConfig?.widget_setting?.blockWidgetOnPages?.length && isShowWidget) {
let restrictedPages = _ALConfig?.widget_setting?.blockWidgetOnPages?.split(',');
const currentPage = window?.location?.pathname;
if (restrictedPages?.length > 0) {
if (restrictedPages?.includes('/homepage') && currentPage === '/') {
isShowWidget = false;
} else if (restrictedPages?.some((page) => currentPage?.includes(page))) {
isShowWidget = false;
}
}
}
if (_ALConfig?.widget_setting?.allowWidgetOnSpecificPages?.length && isShowWidget) {
let allowedPages = _ALConfig?.widget_setting?.allowWidgetOnSpecificPages?.split(',');
const currentPage = window?.location?.pathname;
if (allowedPages?.length > 0) {
if (!allowedPages?.some((page) => currentPage?.includes(page))) {
isShowWidget = false;
}
}
}
if (!_ALConfig?.customerId && _ALConfig?.widget_setting?.showToLoggedInCustomerOnly) {
isShowWidget = false;
}
if (isShowWidget) {
showAppstleLoyaltyWidget();
} else {
removeAppstleLoyaltyWidget();
}
}
checkForWidgetRestrictions();
function enableWidget() {
document.querySelector('#appstle_loyalty_iframe').classList.add('open');
document.querySelector('#appstle_loyalty_iframe').classList.remove('appstle-loyalty-hidden');
setTimeout(function() {
document.querySelector('#appstle_loyalty_iframe').classList.toggle('show-close-icon');
}, 900);
}
function showAppstleLoyaltyWidget() {
document.querySelector('#appstle-loyalty-button').classList.remove('appstle-loyalty-hidden');
document.querySelector('#appstle_loyalty_iframe').classList.remove('appstle-loyalty-hidden');
}
document.querySelector('.appstle-loyalty-button').addEventListener('click', toggleWidget);
if (_ALConfig?.widget_setting?.widgetPosition === 'LEFT') {
document.getElementById('appstle_loyalty_iframe').classList.add('appstle-loyalty-left');
document.querySelector('.appstle-loyalty-button').classList.add('appstle-loyalty-left');
document.getElementById('appstle_loyalty_iframe').classList.add('appstle-loyalty-left');
document.getElementById('snackbar').classList.add('left_snackbar');
} else {
document.getElementById('appstle_loyalty_iframe').classList.add('appstle-loyalty-right');
document.querySelector('.appstle-loyalty-button').classList.add('appstle-loyalty-right');
document.getElementById('snackbar').classList.add('right_snackbar');
}
document.getElementById('loyalty-widget-title').innerHTML = _ALConfig?.widget_setting?.widgetTitle;
const alIframeSrc = ''
+ ' '
+ '