(()=>{var AddToCartListener=class _AddToCartListener{static ADD_TO_CART_REGEX=/^(?:\/[a-zA-Z]+(?:-[a-zA-Z]+)?)?\/+cart\/+add(?:\.js|\.json)?\/*$/;static XHR_FALLBACK_STATE={UNSENT:0,OPENED:1,HEADERS_RECEIVED:2,LOADING:3,DONE:4};static handleXhrOpen(){}constructor(xhr,url,method,body){this.xhr=xhr;this.url=url;this.method=method;this.body=body}onReadyStateChange(){if(this.xhr.readyState===_AddToCartListener.XHR_FALLBACK_STATE.DONE){_AddToCartListener.handleXhrDone({method:this.method,url:this.url,body:this.body,xhr:this.xhr})}if(this.oldOnReadyStateChange){this.oldOnReadyStateChange()}}static handleXhrDone(payload){if(payload.xhr.status>=400){return}try{const hyperlinkElement=document.createElement("a");hyperlinkElement.href=payload.url;const path=hyperlinkElement.pathname?hyperlinkElement.pathname:payload.url;if(_AddToCartListener.ADD_TO_CART_REGEX.test(path)){_AddToCartListener._parsePayloadResponse(payload,parsedResponse=>{try{handleAddResponse(parsedResponse,"xhr")}catch(error){}const keys=Object.keys(parsedResponse);if(keys.length===1&&keys[0]==="items"){const parsedResponseItemsList=parsedResponse.items;let parsedPayloadBodyItemsList;try{parsedPayloadBodyItemsList=JSON.parse(payload.body).items}catch(error){parsedPayloadBodyItemsList=parseFormEncodedBulkPayload(payload.body,parsedResponseItemsList.length)}handleBulkItemCartAddResponse(parsedResponseItemsList,parsedPayloadBodyItemsList,"add-xhr-bulk")}else{handleItemAdded(parsedResponse,parseSingleItemQuantity(payload.body),"add-xhr")}})}}catch(error){if(console&&console.warn){console.warn(`[shop_events_listener] Error in handleXhrDone: ${error.message}`)}}}static parseBlobToJson(blob,parsedPayloadHandler){const reader=new FileReader;reader.addEventListener("loadend",()=>parsedPayloadHandler(JSON.parse(String.fromCharCode(...new Uint8Array(reader.result)))));reader.readAsArrayBuffer(blob)}static _parsePayloadResponse(payload,parsedPayloadHandler){if(payload.xhr.response instanceof Blob){_AddToCartListener.parseBlobToJson(payload.xhr.response,parsedPayloadHandler)}else if(payload.xhr.responseText){parsedPayloadHandler(JSON.parse(payload.xhr.responseText))}}};(function(){function addListener(element,type,callback){if(window.jQuery&&window.jQuery(element).bind){window.jQuery(element).bind(type,callback)}else if(element.addEventListener){element.addEventListener(type,callback)}else if(element.attachEvent){element.attachEvent(`on${type}`,callback)}}function handleSubmitToCartAdd(event){event=event||window.event;if(event.defaultPrevented||event.isDefaultPrevented&&event.isDefaultPrevented()){return}const target=event.target||event.srcElement;if(target&&(target.getAttribute("action")||target.getAttribute("href"))){try{let element;const idElement=target.id||target.elements.id;if(idElement.options){element=idElement.options[idElement.selectedIndex]}else{element=idElement}const cartToken=getCookie("cart");const variantId=element.value;const properties=trackProperties(variantId);properties.quantity=String(target.quantity?target.quantity.value:1);const eventProperties=extend({cartToken},properties);const monorailProperties=extend({referer:window.location.href},properties);window.ShopifyAnalytics.lib.track("Added Product",eventProperties,void 0,void 0,{addApiSource:"add-form",shopifyEmitted:true});window.ShopifyAnalytics.lib.track("monorail://trekkie_storefront_track_added_product/1.1",monorailProperties)}catch(error){if(console&&console.warn){console.warn(`[shop_events_listener] Error in handleSubmitCartAdd: ${error.message}`)}}}}function handleSubmitToPaymentAdd(event){event=event||window.event;const target=event.target||event.srcElement;if(target&&target.getAttribute("action")&&target.getAttribute("data-payment-form")!==null){try{window.ShopifyAnalytics.lib.track("Added Payment",{currency:window.ShopifyAnalytics.meta.currency,total:window.ShopifyAnalytics.meta.checkout.payment_due/100},void 0,void 0,{shopifyEmitted:true})}catch(error){if(console&&console.warn){console.warn(`[shop_events_listener] Error in handleSubmitToPaymentAdd: ${error.message}`)}}}}function handleViewedProductVariant(event){event=event||window.event;trackViewedProductVariant(event.currentTarget)}function trackViewedProductVariant(target){try{let element;const idElement=target.id||target.elements.id;if(idElement.options&&idElement.options[idElement.selectedIndex]){element=idElement.options[idElement.selectedIndex]}else{element=idElement}if(!element){return}const newVariantId=element.value;if(window.ShopifyAnalytics.meta.selectedVariantId&&window.ShopifyAnalytics.meta.selectedVariantId==newVariantId){return}window.ShopifyAnalytics.meta.selectedVariantId=newVariantId;const properties=trackProperties(newVariantId);window.ShopifyAnalytics.lib.track("Viewed Product Variant",properties,void 0,void 0,{shopifyEmitted:true})}catch(error){if(console&&console.warn){console.warn(`[shop_events_listener] Error in trackViewedProductVariant: ${error.message}`)}}}function trackProperties(variantId){const productProperties=productTrackProperties(variantId);const pageProperties=pageTrackProperties();const result=extend(productProperties,pageProperties);result.currency=window.ShopifyAnalytics.meta.currency;return result}function findProductAndVariantInCollection(variantId,products){for(const product of products){const variant=findProductVariant(variantId,product);if(variant){return{product,variant}}}}function findProductVariant(variantId,product){for(const variant of product.variants){if(variant.id==variantId){return variant}}}function productTrackProperties(variantId){let product;let productVariant;let result;if(window.ShopifyAnalytics.meta.products){const products=window.ShopifyAnalytics.meta.products;({product,variant:productVariant}=findProductAndVariantInCollection(variantId,products))}else if(window.ShopifyAnalytics.meta.product){product=window.ShopifyAnalytics.meta.product;productVariant=findProductVariant(variantId,product)}if(product){result={productId:product.id,productGid:product.gid,brand:product.vendor,category:product.type};if(productVariant){result=extend(result,{variantId,price:productVariant.price/100,name:productVariant.name,sku:productVariant.sku,variant:productVariant.public_title})}}else{result={variantId}}return result}window.addEventListener("load",()=>{for(let i=0;i=0){addListener(document.forms[i],"submit",handleSubmitToCartAdd);addListener(document.forms[i],"change",handleViewedProductVariant);trackViewedProductVariant(document.forms[i])}const previousStep=document.forms[i].elements.previous_step;if(previousStep&&previousStep.value==="payment_method"){addListener(document.body,"submit",handleSubmitToPaymentAdd)}}})})();(function(XHR){const open=XHR.prototype.open;const send=XHR.prototype.send;XHR.prototype.open=function(method,url){this._url=url;this._method=method;AddToCartListener.handleXhrOpen();open.apply(this,arguments)};XHR.prototype.send=function(data){const addToCartListener=new AddToCartListener(this,this._url,this._method,data);if(this.addEventListener){this.addEventListener("readystatechange",addToCartListener.onReadyStateChange.bind(addToCartListener),false)}else{addToCartListener.oldOnReadyStateChange=this.onreadystatechange;this.onreadystatechange=addToCartListener.onReadyStateChange}send.call(this,data)}})(XMLHttpRequest);(function(window2,fetch){if(typeof fetch==="function"){window2.fetch=function(){return fetch.apply(this,Array.prototype.slice.call(arguments)).then(response=>{if(!response.ok){return response}const hyperlinkElement=document.createElement("a");hyperlinkElement.href=response.url;const path=hyperlinkElement.pathname?hyperlinkElement.pathname:response.url;try{if(AddToCartListener.ADD_TO_CART_REGEX.test(path)){try{handleFetchAddToCart(response,arguments[1].body)}catch(error){}}}catch(error){consoleWarn(error)}return response})}}function handleFetchAddToCart(response,requestBody){response.clone().json().then(responseJson=>{if(Object.prototype.toString.call(responseJson)!=="[object Object]"){return}try{handleAddResponse(responseJson,"fetch")}catch(error){}if(responseJson.items){const parsedPayloadBodyItemsList=parseRequestBody(requestBody).items;const parsedResponseItemsList=responseJson.items;handleBulkItemCartAddResponse(parsedResponseItemsList,parsedPayloadBodyItemsList,"add-fetch-bulk");return}const quantity=parseSingleItemQuantity(requestBody);handleItemAdded(responseJson,quantity,"add-fetch")}).catch(consoleWarn)}function consoleWarn(error){if(console&&console.warn){console.warn(`[shop_events_listener] Error in handleFetchRequest: ${error.message}`)}}})(window,window.fetch);function handleAddResponse(response,apiSource){const itemsAdded=response&&response.items_changelog&&response.items_changelog.added;if(!itemsAdded||!Array.isArray(itemsAdded)){return}const isBulkAdd=Array.isArray(response.items);const addApiSource=`add-${apiSource}${isBulkAdd?"-bulk":""}`;const itemsInCart=isBulkAdd?response.items:[response];const itemsAddedWithPayload=itemsAdded.map(addedItem=>{const matchedItem=itemsInCart.find(item=>{return String(item.variant_id||item.id)===String(addedItem.variant_id)});if(matchedItem){const result={};for(const prop in matchedItem){if(matchedItem.hasOwnProperty(prop)){result[prop]=matchedItem[prop]}}result.quantity=addedItem.quantity;return result}return null}).filter(Boolean);itemsAddedWithPayload.forEach(item=>{handleItemAddedServerSideChangelogTest(item,item.quantity,addApiSource)})}function handleBulkItemCartAddResponse(parsedResponseItemsList,parsedPayloadBodyItemsList,addApiSource){if(parsedResponseItemsList.length!=parsedPayloadBodyItemsList.length){throw Error("Payload body and response have different number of items")}parsedResponseItemsList.forEach((item,idx)=>{let quantity=1;try{quantity=parseInt(parsedPayloadBodyItemsList[idx].quantity,10)||1}catch(error){if(console&&console.warn){console.warn(`[shop_events_listener] Error in handleBulkItemCartAddResponse: ${error.message}`)}}handleItemAdded(item,quantity,addApiSource)})}function parseFormEncodedBulkPayload(body,size){const parsedPayloadBodyItems=new Array(size);for(let i=0;i