(function(){"use strict";function g(){sessionStorage.setItem("_up_core_boosted","1")}var d=(t=>(t.LINKER_COMPLETED="_up_lk_cp",t.EXPOSE_ACTION_CONTEXT="__up_exposeActionContext",t.LAST_APPLIED_DISCOUNT_CODE="__up_lastAppliedDiscountCode",t))(d||{});const c={LINKER:"_upl",REDIRECT_URL:"redirect_url"};function w(){if(new URLSearchParams(window.location.search).has(c.LINKER))return!0;if(!("__st"in window))return!1;try{return new URL("https://"+__st.pageurl).searchParams.has(c.LINKER)}catch{return!1}}function m(){return window.location.search!=""?new URL(window.location.href):"__st"in window?new URL("https://"+__st.pageurl):new URL(location.href)}function E(){const e=m().searchParams.get(c.REDIRECT_URL);e&&(window.location.href=atob(e))}function S(){if(!w())return;const t=m(),e=t.searchParams.get(c.REDIRECT_URL),n=t.searchParams.get(c.LINKER);if(!e||!n)return;const r=setInterval(()=>{if(document.readyState!="complete")return;const s=localStorage.getItem(d.LINKER_COMPLETED);s&&s===n&&(E(),clearInterval(r))},100);setTimeout(()=>{clearInterval(r)},1e4)}function y(){const t=UpPromoteCoreSettings.app_env.env,e={local:"https://secomapp-affiliate.test/js/storefront/uppromote-dev.js",test:"https://af-test.uppromote.com/js/storefront/uppromote-test.js",staging:"https://af-test.uppromote.com/js/storefront/uppromote-test.js",production:"https://d1639lhkj5l89m.cloudfront.net/js/storefront/uppromote.js"},n=e[t]||e.production,r=document.createElement("script");r.setAttribute("async","true"),r.src=n,document.body.prepend(r)}async function u(t,e){return(await fetch(window.Shopify.routes.root+t,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)})).json()}function P(t,e){console.log("Apply attr to car");const n=o=>{const a=document.createElement("input");return a.type="hidden",a.name=o,a.value=e,a},r=o=>{o.appendChild(n(`attributes[${t}]`)),o.appendChild(n(`properties[${t}]`))},s=o=>{o.querySelector(`input[name="attributes[${t}]"]`)?.remove(),o.querySelector(`input[name="properties[${t}]"]`)?.remove()},_=(o,a,i)=>{a.setAttribute("disabled","disabled"),s(i),a.removeAttribute("disabled"),a.dispatchEvent(new Event("click")),setTimeout(()=>{r(i)},500)},D=document.querySelectorAll('form[action$="/cart/add"] button[type="submit"]');for(const o of Array.from(D)){const a=o.closest("form");a&&(r(a),o.addEventListener("mousedown",i=>_(i,o,a)),o.addEventListener("touchstart",i=>_(i,o,a)))}}const p="_up_antiLeak";async function T(t,e){const n=!!UpPromoteCoreSettings.currentCart.attributes[t],r=e!=UpPromoteCoreSettings.currentCart.attributes[t];(!n||r)&&await u("cart/update.js",{attributes:{[t]:e}}),P(t,e)}function A(){setTimeout(()=>{if(!!!UpPromoteCoreSettings.currentCart.attributes[p])return;u("cart/update.js",{attributes:{[p]:null}}).then(),UpPromoteCoreSettings.currentCart.items.map((r,s)=>({index:s+1,hasProperty:!!r.properties[p]})).filter(r=>r.hasProperty).forEach(r=>{u("cart/change.js",{line:r.index,properties:{[p]:null}}).then()})},1e3)}async function U(t){if(!t.useAntiLeak){A();return}const e=t.aid+"|"+t.pid;T(p,e).then()}let l=null;async function L(){return(await fetch("/cart.js")).json()}function I(){return l.discount_codes}async function f(t){await fetch("/cart/update.js",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({discount:t})})}async function b(t){I().some(r=>r.code===t)||await f(t)}async function O(t){if(l=await L(),!t){l.discount_codes&&l.discount_codes.length>0&&await f("");return}await b(t)}async function R(){UpPromoteCoreSettings.currentCart.attributes._up_apd&&await u("cart/update.js",{attributes:{_up_apd:null}});const t=UpPromoteCoreSettings.currentCart.items.map((e,n)=>({index:n+1,hasProperty:!!e.properties._up_apd})).filter(e=>e.hasProperty);for(const e of t)await u("cart/change.js",{line:e.index,properties:{_up_apd:null}})}const N={applyDiscountCode:O,applyAntiLeak:U,cleanOldData:R};function j(){try{const t=localStorage.getItem(d.EXPOSE_ACTION_CONTEXT);return t?JSON.parse(t):[]}catch{return[]}}function v(t){localStorage.setItem(d.EXPOSE_ACTION_CONTEXT,JSON.stringify(t))}async function h(){if(window.UpPromoteCoreSettings.communicating)return;window.UpPromoteCoreSettings.communicating=!0;const t=j();for(;t.length>0;){const e=t.shift();v(t);try{N[e.n]?.(e.d)}finally{}}window.UpPromoteCoreSettings.communicating=!1}function C(){h().then(),setInterval(h,300),g(),y(),S()}document.readyState!=="loading"?C():document.addEventListener("DOMContentLoaded",C)})();