(()=>{"use strict";!function(r){var e=function(){var e=(window.RTSB||{}).cache,t=null==e?void 0:e.create("compare");return{name:"compare",version:"1.0.0",init:function(){return this.bindCompareButtonEvents(),this.initCompareEventActions(),t.set("initialized",!0),this},bindCompareButtonEvents:function(){var e=this,o=".rtsb-compare-add",a=".rtsb-compare-remove";r(document).off("click",o).off("click",a).on("click",o,function(o){o.preventDefault();var a=r(o.currentTarget),n=a.data("product_id");if(!n)return e;var s="undefined"!=typeof RTSB&&RTSB.toastr||window.rtsbToastr;if(null==t?void 0:t.get("add_".concat(n)))return e;null==t||t.set("add_".concat(n),!0),r.ajax({type:"POST",url:rtsbCompareParams.resturl+"rtsb/v1/compare/add_to_list/",data:{product_id:n},beforeSend:function(e){a.rtsbBlock(),r(document).trigger("rtsb.compare.adding",[a]),e.setRequestHeader("X-WP-Nonce",rtsbCompareParams.rest_nonce)},success:function(e){if(e.success){null==t||t.set("status_".concat(n),"added"),r(document).trigger("rtsb.compare.added",[e,a]);var o,c=rtsbCompareParams.notice.added;rtsbCompareParams.notice.browse&&rtsbCompareParams.notice.browse&&(c=' <a href="'+rtsbCompareParams.pageUrl+'">'+rtsbCompareParams.notice.browse+"</a>",o=rtsbCompareParams.notice.added),s.success(c,o)}else r(document).trigger("rtsb.compare.adding_error",[e,a]),s.error(e.message);a.rtsbUnblock()},error:function(e){r(document).trigger("rtsb.compare.adding_error",[e,a]),a.rtsbUnblock(),s.error(rtsbCompareParams.notice.error)},complete:function(e){null==t||t.delete("add_".concat(n)),r(document).trigger("rtsb.compare.adding_completed",[e,a])}})}).on("click",a,function(o){o.preventDefault();var a=r(o.currentTarget),n=a.data("product_id");if(!n)return e;var s="undefined"!=typeof RTSB&&RTSB.toastr||window.rtsbToastr;if(null==t?void 0:t.get("remove_".concat(n)))return e;null==t||t.set("remove_".concat(n),!0),r.ajax({type:"POST",url:rtsbCompareParams.resturl+"rtsb/v1/compare/remove_from_list/",data:{product_id:n},beforeSend:function(e){a.rtsbBlock(),r(document).trigger("rtsb.compare.removing",[a]),e.setRequestHeader("X-WP-Nonce",rtsbCompareParams.rest_nonce)},success:function(e){e.success?(null==t||t.set("status_".concat(n),"removed"),r(document).trigger("rtsb.compare.removed",[e,a]),s.success(rtsbCompareParams.notice.removed)):(r(document).trigger("rtsb.compare.removing_error",[e,a]),s.error(e.message)),a.rtsbUnblock()},error:function(e){r(document).trigger("rtsb.compare.removing_error",[e,a]),a.rtsbUnblock(),s.error(rtsbCompareParams.notice.error)},complete:function(e){null==t||t.delete("remove_".concat(n)),r(document).trigger("rtsb.compare.removing_completed",[e,a])}})})},initCompareEventActions:function(){r(document).on("rtsb.compare.added",function(e,t,o){o.removeClass("rtsb-compare-add").addClass("rtsb-compare-remove"),r(".rtsb-compare-list").html(t.data.list_html),r(".rtsb-compare-counter").text(t.data.item_count),r(o).find(".button-text").text(rtsbCompareParams.notice.ajax_remove_compare)}).on("rtsb.compare.removed",function(e,t,o){o.removeClass("rtsb-compare-remove").addClass("rtsb-compare-add"),r(".rtsb-compare-list").html(t.data.list_html),r(".rtsb-compare-counter").text(t.data.item_count),r(o).find(".button-text").text(rtsbCompareParams.notice.ajax_button_text)})},destroy:function(){t.clear("compare")}}};if(window.RTSB&&window.RTSB._initialized){var t;null===(t=RTSB.modules.register("compare",e))||void 0===t||t.init()}else{var o=e();r.fn.rtsb_compare=function(){o.bindCompareButtonEvents()},r.fn.rtsb_compare(),o.initCompareEventActions()}}(jQuery)})();