(() => { /** Create ocrReimagine namespace */ if (!window.ocrReimagine) { window.ocrReimagine = {}; } /** Create Market Selector module namespace */ if (!window.ocrReimagine.MarketSelector) { window.ocrReimagine.MarketSelector = {}; } const cookieExpiryDays = 30; const cookieKey = 'PMGSKUMarketCk'; /** @type {MarketSelector[]} */ const instances = []; const queryParamKey = 'market'; /** * CSS selector. * @enum {string} */ const Selector = { AFFECTED: '[data-oc-market-selector]', DATA_MOUNT: '[data-mount="market-selector"]', FW_LINKS: '[data-regenerate-fwlink="true"]', SELECT_MENU: '.custom-select-input' }; /** * JS event name. * @enum {string} */ const EventName = { ON_INIT: 'onInit', ON_REFRESHED: 'onRefreshed', ON_REMOVE: 'onRemove', ON_SELECT: 'onSelect', ON_UPDATE: 'onUpdate' }; /** * Handles change events on select menus. * @this {MarketSelector} Parent component. * @param {HTMLSelectElement} selectMenu Select element. */ function _change(selectMenu) { if (selectMenu.selectedOptions.length) { let market = selectMenu.selectedOptions[0].value; this.el.dispatchEvent(this[EventName.ON_SELECT]); // update regenerating FWLinks, and add the information to On_Refreshed Event this.fwlinkParams = selectMenu.selectedOptions[0].dataset.fwlinkParams; this[EventName.ON_REFRESHED].detail.fwlinkParams = this.fwlinkParams; _setFWLinksQueryParams(this.fwlinkParams); _setMarket.call(this, market); //Dispatch Market Selector. oc.event.marketSelector.dispatch(EventName.ON_SELECT.toLocaleLowerCase(), market); } } /** * Gets a set cookie value for the key provided, if available. * @param {string} key Cookie key. * @returns {string | null} Cookie value, or null. */ function _getCookie(key) { const cookies = _getAllCookies(); if (cookies) { const value = cookies[key]; if (value !== undefined) { return value; } } return null; } /** * Gets all set cookie values, if available. * @returns {Record | null} All cookie values. */ function _getAllCookies() { /** @type {Record} */ const cookies = {}; document.cookie.split('; ').forEach(pair => { const separatorIndex = pair.indexOf('='); if (separatorIndex !== -1) { const key = pair.slice(0, separatorIndex); const value = decodeURIComponent(pair.slice(separatorIndex + 1)); cookies[key] = value; } }); if (Object.keys(cookies).length > 0) { return cookies; } return null; } /** * Gets the geodetected country code which was previously set by an edge-side include. * @returns {string | null} Geodetected country code, or null. */ function _getGeoCountry() { if ( window.oc && window.oc.geo && window.oc.geo.country && typeof window.oc.geo.country === 'string' ) { return window.oc.geo.country; } return null; } /** * Updates all regenerating FWLinks href's to the option's designated query params. * @param {string} queryParams */ function _setFWLinksQueryParams(queryParams) { const fwLinks = document.querySelectorAll(Selector.FW_LINKS); fwLinks.forEach(link => { try { const url = new URL(link.getAttribute('href')); const searchParams = new URLSearchParams(queryParams); const urlSearchParams = new URLSearchParams(url.search); searchParams.forEach((key, value) => { urlSearchParams.set(value, key); }); // 'clcid' is not a guaranteed part of the new Query Search Params. Delete this from the original fwlink's destination // url if the