(()=>{"use strict";var e={n:t=>{var o=t&&t.__esModule?()=>t.default:()=>t;return e.d(o,{a:o}),o},d:(t,o)=>{for(var n in o)e.o(o,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:o[n]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)};const t=jQuery;var o=e.n(t);const n=new function(){const e=`wc_bogof_data_${wc_bogof_modal_gifts_params.site_hash}`,t=sessionStorage.getItem(e);let o={};if(t)try{o=JSON.parse(t)}catch(e){o={}}this.get=function(e){return o&&"object"==typeof o&&e in o?o[e]:null},this.set=function(e,t){o&&"object"==typeof o&&(o[e]=t)},this.delete=function(e){o&&"object"==typeof o&&e in o&&delete o[e]},this.save=function(t,n){sessionStorage.setItem(e,JSON.stringify(o))}}(0);function a(){let e=null;const t="-wc-bogof-collapsed";function o(){return e.classList.toggle(t),e.classList.contains(t)?n.set("announcementBarcollapsed",n.get("gift_hash_key")):n.delete("announcementBarcollapsed"),n.save(),!1}function a(){return null!==n.get("announcementBarcollapsed")&&n.get("announcementBarcollapsed")===n.get("gift_hash_key")}this.setContent=function(n){null==e&&function(){const n=document.getElementById("wc-bogof-announcement-tpl"),c=n.content.cloneNode(!0);if(null===n)return null;a()&&c.firstElementChild.classList.add(t),document.body.insertBefore(c,document.body.firstChild),e=document.getElementById("wc-bogof-announcement-bar"),e.querySelector("a.wc-bogof-announcement-close").addEventListener("click",o),e.querySelector("div.close-container a.toggle-close").addEventListener("click",o)}(),e.querySelector("li.wc-bogof-announcement-notice").innerHTML=n,a()||e.classList.remove(t)},this.destroy=function(){null!=e&&(e.remove(),e=null)}}function c(){const e="wc-bogof-woo-notice-content",t=`div.${e}`;function o(){return document.querySelector(t)}this.setContent=function(t){const n=o();null===n?function(t){const o=document.getElementById("wc-bogof-woo-notice-tpl");if(null===o)return null;const n=o.content.cloneNode(!0);if(null===o)return null;const a=function(){let e=null;if("undefined"!=typeof wc_bogof_modal_gifts_params&&wc_bogof_modal_gifts_params.notice_parent_node&&(e=document.querySelector(wc_bogof_modal_gifts_params.notice_parent_node),null!==e))return e;if(e=document.querySelector(".woocommerce-bogof-notices-wrapper"),null!==e)return e;const t=Array.from(document.querySelectorAll(".wc-block-components-notices")).filter(e=>null===e.closest('[data-block-name="woocommerce/mini-cart-contents"]'));if(t.length)return t[0];if(e=document.querySelector(".wc-block-components-notice-banner"),null!==e)return e.parentNode;if(e=document.querySelector('[data-block-name="woocommerce/store-notices"]'),null!==e)return e;if(e=document.querySelector(".woocommerce-notices-wrapper"),null!==e){const t=document.createElement("div");return t.classList.add("woocommerce-bogof-notices-wrapper"),t.style.width="100%",e.parentNode.insertBefore(t,e)}return e=document.querySelector(".woocommerce-message"),null!==e?e.parentNode:(e=document.querySelector(".woocommerce-info"),null!==e?e.parentNode:(e=document.querySelector(".woocommerce-error"),null!==e?e.parentNode:e))}();if(null===a)return;const c=`<div class="${e}">${t}</div>`;n.firstElementChild.innerHTML=c,n.firstElementChild.classList.add("wc-bogof-notice"),a.insertBefore(n,a.firstChild)}(t):n.innerHTML=t},this.destroy=function(){const e=o();null!==e&&e.parentNode.remove()}}function r(){if(this.silentEvent=!1,void 0!==window.wc&&void 0!==window.wc.blocksCheckout&&void 0!==window.wp&&void 0!==window.wp.plugins){let e=Cookies.get("wc_bogof_gifts_hash")??"",t=this;const o=new Event("wc-bogof-cart_block_updated"),n=()=>{document.body.dispatchEvent(o)},{registerPlugin:a}=window.wp.plugins,{createElement:c}=window.wp.element,{blocksCheckout:r}=window.wc,i=o=>{if("wc_bogof_gifts_modal_data"in o.extensions){const{wc_bogof_gifts_modal_data:a}=o.extensions;"gifts_hash"in a&&a.gifts_hash!==e&&(e=a.gifts_hash,t.silentEvent?t.silentEvent=!1:setTimeout(n,50))}return null};a("wc-buy-one-get-one-free",{render:()=>c(r.ExperimentalDiscountsMeta,null,c(i,null)),scope:"woocommerce-checkout"})}}function i(){let e=!1;const t=null!==document.querySelector('[data-block-name="woocommerce/cart"]')||null!==document.querySelector('[data-block-name="woocommerce/checkout"]')?new r:null;null===t&&function(){if(void 0===window.wc||void 0===window.wc.wcBlocksData)return!1;const e=new Event("wc-bogof-cart_block_updated"),{CART_STORE_KEY:t}=window.wc.wcBlocksData;if("undefined"==typeof wp||void 0===wp.hooks)return!1;const o={itemsCount:0,timeoutID:0,checkItems:function(){o.itemsCount!==wp.data.select(t).getCartData().itemsCount?(o.itemsCount=0,o.timeoutID=0,document.body.dispatchEvent(e)):o.timeoutID=setTimeout(o.checkItems,100)},start:function(){const e=wp.data.select(t).getCartData().itemsCount;o.itemsCount!==e&&(o.timeoutID>0&&(o.timeoutID=0,clearTimeout(o.timeoutID)),o.itemsCount=e,o.checkItems())}};wp.hooks.addAction("experimental__woocommerce_blocks-cart-set-item-quantity","wc-buy-one-get-one-free",o.start),wp.hooks.addAction("experimental__woocommerce_blocks-cart-remove-item","wc-buy-one-get-one-free",o.start)}();const n=new Event("wc-bogof-cart_updated"),a=()=>{e?e=!1:document.body.dispatchEvent(n)};o()(document.body).on("updated_wc_div wc_fragment_refresh added_to_cart removed_from_cart",a),document.body.addEventListener("wc-bogof-cart_block_updated",a),document.addEventListener("reycore/minicart/updated",a),this.refresh=function(n,a){let c=!1;if(null!==document.querySelector("form.woocommerce-cart-form")&&(e=!0,c=!0,o()(document.body).trigger("wc_update_cart")),null!==document.querySelector("form.woocommerce-checkout")&&(c=!0,o()(document.body).trigger("update_checkout")),!c&&"cart_hash"in n&&"fragments"in n){e=!0,null!==t&&(t.silentEvent=!0);const c=a?"added_to_cart":"removed_from_cart";o()(document.body).trigger(c,[n.fragments,n.cart_hash,o()("<button></button>")])}}}function s(e){if("1"===e.data("wc_bogof_variarion_form_loaded"))return;const t=e,n=t.closest(".wc-bogof-product"),a=t.find('button[type="submit"]'),c=n.find(".wc-bogof-product-summary"),r=c.find("span.price"),i=n.find(".wc-bogof-gift-item__image img"),s=c.find(".wc-bogof-gift-item__short-description"),d={price_html:r.length>0?r[0].outerHTML:"",variation_description:s.length>0?s.html():"",image:{src:i.attr("src"),height:i.attr("height"),width:i.attr("width")}};function l(e){r.length>0&&r.html(o()(e).html())}function u(e){i.wc_set_variation_attr("src",e.image.src),i.wc_set_variation_attr("height",e.image.height),i.wc_set_variation_attr("width",e.image.width)}function m(e){s.length>0&&s.html(e.variation_description)}t.wc_variation_form(),t.on("found_variation",function(e,n){var c;l(n.price_html),a.val(n.variation_id),a.prop("disabled",!1),n.image_id>0?u(n):u(d),m(n),t.find(".wc-bogof-error.wc-bogof-variation-notice").remove(),n.status_notice&&(c=n.status_notice,o()(`<span class="wc-bogof-error wc-bogof-variation-notice">${c}</span>`).insertAfter(a))}),t.on("reset_data",function(){l(d.price_html),a.val(""),a.prop("disabled",!0),u(d),m(d)}),t.data("wc_bogof_variarion_form_loaded","1")}function d(e){return wc_bogof_modal_gifts_params.wc_ajax_url.toString().replace("%%endpoint%%",`bogof_${e}`)}const l={addingToCart:!1,notice:null,cart:null,xhr:!1,announcement:function(){const e=n.get("fragments");if(e.announcement){if(l.notice.setContent(e.announcement),wc_bogof_modal_gifts_params.auto_open_modal&&!l.addingToCart){const e=n.get("gift_hash_key");n.get("modal_hide")!==e&&l.modal()}}else l.notice.destroy()},modal:function(){const e=document.getElementById("choose-your-gift-dialog"),t=n.get("fragments");if(t.modal){if(null===e){const e=document.getElementById("wc-bogof-gift-modal-tpl").content.cloneNode(!0);for(const o in t.modal)e.querySelector(o).innerHTML=t.modal[o];document.body.appendChild(e)}else for(const o in t.modal)null!==e.querySelector(o)&&(e.querySelector(o).innerHTML=t.modal[o]);o()("#choose-your-gift-dialog").wc_bogo_modal_dialog("show"),l.init_variation_forms(),l.update_header_height()}},init_variation_forms:function(){document.getElementById("choose-your-gift-dialog").querySelectorAll(".wc-bogof-gift-variations-form").forEach(e=>{new s(o()(e))})},update_header_height:function(){const e=document.querySelectorAll("#choose-your-gift-dialog .wc-bogo-modal-header");null!==e&&e.length&&document.getElementById("choose-your-gift-dialog").style.setProperty("--header-height",e[0].offsetHeight+"px")},load_more:function(e){e.preventDefault(),o()(this).addClass("loading-page");const t=o()(this).data("appendTo"),a=o()(this).data("page");o().ajax({url:d("get_gift_fragments"),type:"post",data:{page:a},dataType:"json",success:function(e){if(e&&e.gifts_hash&&n.set("gift_hash_key",e.gifts_hash),e&&e.fragments&&e.fragments.modal){const a=n.get("fragments"),c=document.getElementById("choose-your-gift-dialog");for(const n in e.fragments.modal)n===t?(a.modal[n]=a.modal[n]+e.fragments.modal[n],o()(c).find(n).append(o()(e.fragments.modal[n]))):null!==c.querySelector(n)&&(a.modal[n]=e.fragments.modal[n],c.querySelector(n).innerHTML=a.modal[n]);n.set("fragments",a),l.init_variation_forms()}n.save()}})},add_gift_to_cart:function(e){if(e.preventDefault(),l.addingToCart)return;l.addingToCart=!0,o()(this).find(".wc-bogof-error.adding-error").remove(),o()(this).find(".wc-bogof-added").removeClass("wc-bogof-added");const t=o()(this).find('button[type="submit"]');t.removeClass("wc-bogof-loading").addClass("wc-bogof-loading").prop("disabled",!0);const a={product_id:t.val()};o().ajax({url:d("add_gift_to_cart"),type:"post",data:a,dataType:"json",success:function(e){try{const t=document.getElementById("choose-your-gift-dialog");if(e&&e.destroy){if(e.fragments&&e.fragments.modal)for(const o in e.fragments.modal)null!==t.querySelector(o)&&(t.querySelector(o).innerHTML=e.fragments.modal[o]);setTimeout(l.destroy,800)}else if(e){e.gifts_hash&&n.set("gift_hash_key",e.gifts_hash);const o=n.get("fragments");if(e.fragments&&e.fragments.announcement&&(o.announcement=e.fragments.announcement),e.fragments&&e.fragments.modal){for(const n in e.fragments.modal)null!==t.querySelector(n)&&(o.modal[n]=e.fragments.modal[n],t.querySelector(n).innerHTML=e.fragments.modal[n]);l.init_variation_forms()}n.set("fragments",o),n.save(),l.announcement()}e&&e.cart_fragments&&l.cart.refresh(e.cart_fragments,e.destroy)}catch(e){window.console.log(e)}},error:function(e){o()(`<span class="wc-bogof-error adding-error">${e.responseJSON.data}</span>`).insertAfter(t)},complete:function(){t.removeClass("wc-bogof-loading").prop("disabled",!1),l.addingToCart=!1}})},destroy:function(){l.notice.destroy();const e=document.getElementById("choose-your-gift-dialog");null!==e&&o()(e).wc_bogo_modal_dialog("hide"),n.set("gift_hash_key",null),n.set("fragments",{modal:{},announcement:!1}),n.save()},update_fragments:function(){l.addingToCart||(l.xhr&&l.xhr.abort(),l.xhr=o().ajax({url:d("get_gift_fragments"),type:"post",data:{},dataType:"json",success:function(e){e&&(e.gifts_hash?(n.set("gift_hash_key",e.gifts_hash),e.fragments&&n.set("fragments",e.fragments),n.save(),l.announcement()):l.destroy())},complete:function(){l.xhr=!1}}))},maybe_update_fragments:function(){if(l.addingToCart)return;const e=void 0===Cookies.get("wc_bogof_gifts_hash")?null:Cookies.get("wc_bogof_gifts_hash"),t=n.get("fragments");null===e?l.destroy():null===t||n.get("gift_hash_key")!==e?l.update_fragments():l.announcement()},on_page_load:function(){"woo"===wc_bogof_modal_gifts_params.notice_style?l.notice=new c:l.notice=new a,l.cart=new i,l.maybe_update_fragments(),window.addEventListener("resize",l.update_header_height),o()(document.body).on("click","a.button-choose-your-gift",function(e){e.preventDefault(),l.modal()}),o()(document.body).on("userhide.modal.wc-bogo","#choose-your-gift-dialog",function(e){n.set("modal_hide",n.get("gift_hash_key")),n.save()}),o()(document.body).on("click","div.wc-bogof-load-more-products button",l.load_more),o()(document.body).on("submit",".wc-bogof-products form.wc-bogof-gift-cart",l.add_gift_to_cart),o()(document.body).on("updated_checkout",l.maybe_update_fragments),o()(document.body).on("updated_wc_div",l.announcement),document.body.addEventListener("wc-bogof-cart_updated",l.update_fragments)},init:function(){if("undefined"==typeof wc_bogof_modal_gifts_params)return!1;"complete"===document.readyState||!0===window.loaded?this.on_page_load():window.addEventListener("load",this.on_page_load)}};l.init()})();