/*! For license information please see Discount.js.LICENSE.txt */ !function(t){function e(e){for(var n,c,a=e[0],u=e[1],s=e[2],d=0,p=[];dt.length)&&(e=t.length);for(var r=0,n=new Array(e);r=0;--o){var i=this.tryEntries[o],c=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var a=r.call(i,"catchLoc"),u=r.call(i,"finallyLoc");if(a&&u){if(this.prev=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),j(r),p}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;j(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:S(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),p}},t}function d(t,e,r,n,o,i,c){try{var a=t[i](c),u=a.value}catch(t){return void r(t)}a.done?e(u):Promise.resolve(u).then(n,o)}function p(t){return function(){var e=this,r=arguments;return new Promise((function(n,o){var i=t.apply(e,r);function c(t){d(i,n,o,c,a,"next",t)}function a(t){d(i,n,o,c,a,"throw",t)}c(void 0)}))}}function f(t,e){for(var r=0;r0&&this.updateBonusItems(),this.promos.value.length>0&&e.items.length>0&&this.getBonusProductPromo(),t.next=18;break;case 15:t.prev=15,t.t0=t.catch(1),console.log("Error find discount",t.t0);case 18:case"end":return t.stop()}}),t,this,[[1,15]])}))),function(){return s.apply(this,arguments)})},{key:"findCombo",value:(a=p(l().mark((function t(){var e,r,o,i,c,a;return l().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return o=(null===(e=this.cart)||void 0===e||null===(r=e.state)||void 0===r?void 0:r.items)||[],i={site_id:document.documentElement.getAttribute("x:id"),items:o||[],payment_method:this.payment.state.method},t.prev=2,t.next=5,Object(n.apiPost)("/view/find_combo_availability",i);case 5:c=t.sent,a=c.data.data||[],this.calcCombo(a),t.next=13;break;case 10:t.prev=10,t.t0=t.catch(2),console.log("Error find combo",t.t0);case 13:case"end":return t.stop()}}),t,this,[[2,10]])}))),function(){return a.apply(this,arguments)})},{key:"getBonusProductPromo",value:(i=p(l().mark((function t(){var e,r,o,i,a,s,d,p,f,h,v,m,_,y,b;return l().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return e={site_id:document.documentElement.getAttribute("x:id"),ids:(this.promos.value||[]).map((function(t){return t.id}))},t.prev=1,t.next=4,Object(n.apiPost)("/view/get_bonus_product_promo",e);case 4:if(o=t.sent,i=o.data.promotions,a=i.reduce((function(t,e){return[].concat(u(t),u(e.bonus_items))}),[]),s=i.reduce((function(t,e){return t+(e.total_quantity_bonus||0)}),0),d=0==s?999999999:s,p=i.some((function(t){var e;return null===(e=t.coupon_info)||void 0===e?void 0:e.is_quantity_same_product})),1!=a.length||0!=((null===(r=this.cart.state)||void 0===r?void 0:r.bonus_items)||[]).length||p){t.next=26;break}if(h=a[0],window.localStorage.getItem("id_bonus_item_rm")!=h.variation.id){t.next=15;break}return t.abrupt("return");case 15:if(v=h.product_bonus,m=Object(n.isPreOrder)(v,h.variation),_=h.variation.remain_quantity||0,!(v.is_runout||h.quantity<1||!Object(n.getIsSellNegative)(v)&&_<=0&&!m)){t.next=20;break}return t.abrupt("return");case 20:y=_>h.quantity||Object(n.getIsSellNegative)(v)?h.quantity:_,b=c(c({},h.variation),{},{product:v,category_id:(null===(f=h.product)||void 0===f?void 0:f.category_id)||"",quantity:d>y?y:d,is_bonus_product:!0,promotion_info:{promotion_id:h.promotion_id,is_variation:!0,bonus_items_active:h.variation.id,quantity:h.quantity}}),this.cart.dispatch("bonus_items",[b]),window.localStorage.setItem("bonus_items",JSON.stringify([b])),t.next=27;break;case 26:Object(n.autoAddBonusItemsSameProduct)(i);case 27:t.next=32;break;case 29:t.prev=29,t.t0=t.catch(1),console.log("Error get bonus product in promotion",t.t0);case 32:case"end":return t.stop()}}),t,this,[[1,29]])}))),function(){return i.apply(this,arguments)})},{key:"calcCombo",value:function(t){var e=this.cart.state.items||[],r=new Map;e.forEach((function(t){return r.set(t.id,t)}));var o=e.filter((function(t){var e;return null==t||null===(e=t.product)||void 0===e?void 0:e.is_combo_product})),i=o.map((function(t){return t.id})),c=e.reduce((function(t,e){var r,o,i,c=null===(r=e.product)||void 0===r?void 0:r.is_price_contact,a=e.discount_once||0,u=Object(n.getRetailPriceByCurrency)(e);return null!==(o=e.product)&&void 0!==o&&o.is_combo_product&&(u=((null===(i=e.product)||void 0===i?void 0:i.variations_combo)||[]).reduce((function(t,e){return t+e.retail_price*(e.quantity||e.quantity_per_combo)}),0)),t+(c?0:u)*e.quantity-a}),0),a=t.reduce((function(t,e){var c=e.is_value_combo,a=e.value_combo,u=void 0===a?0:a,s=e.is_use_percent,l=e.total_discount_percent,d=void 0===l?0:l,p=e.discount_amount,f=void 0===p?0:p,h=e.count_active,v=e.combo_items,m=0;i.includes(e.id)||(m=v.reduce((function(t,o){var i=null!=e&&e.is_categories?r.get(o.id):r.get(o.variation_id);return i?t+Object(n.getRetailPriceByCurrency)(i)*o.count*h:t}),0));var _=o.reduce((function(t,r){var n;return r.id==e.id?t+((null===(n=r.product)||void 0===n?void 0:n.variations_combo)||[]).reduce((function(t,e){return t+e.retail_price*(e.quantity||e.quantity_per_combo)}),0)*(r.quantity||1):t}),0);return t+(c?m+_-u*h:s?d*h:f*h)}),0),u=t.some((function(t){return t.is_free_shipping})),s=Math.max(c-a,0);this.cart.dispatch("is_active_combo",t.length>0),this.cart.dispatch("total_price_apply_combo",s),this.cart.dispatch("discount_combo",a),this.cart.dispatch("is_free_shipping_combo",u)}},{key:"calcDiscount",value:function(){var t,e=this,r=function(t){var r=e.cart.state.items||[],n=r.reduce((function(t,e){return t+e.retail_price*e.quantity-~~e.discount_once}),0),o=function(t,e){var r=t.discount,n=void 0===r?0:r,o=t.is_percent,i=t.max_discount_by_percent;if(o){var c=Math.floor(n*e/100);return Math.min(c,i||1e9)}return n};return t.reduce((function(t,e){var i=e||{},c=i.discount_info,a=i.items_active,u=void 0===a?[]:a,s=i.type;if(!c)return t;if("discount_by_order_price"===s){var l=u.map((function(t){return t.id})),d=r.reduce((function(t,e){return l.includes(e.id)?t+e.retail_price*e.quantity-~~e.discount_once:t}),0);return t+o(c,d)}if("coupon"===s){c.coupon_info;var p=c.ids_items_active,f=void 0===p?[]:p,h=c.max_discount_by_percent,v=c.discount,m=c.is_percent;return 0==f.length&&m&&0!=v&&!h?0:t+o(c,n)}return t+o(c,n)}),0)},n=this.promos.value.filter((function(t){return"concurrent"==t.promotion_classify||!t.promotion_classify})),o=this.promos.value.filter((function(t){return"exclude"==t.promotion_classify})),i=r(n)+(t=o.reduce((function(t,e){return(e.priority_level||0)>((null==t?void 0:t.priority_level)||0)||null==t?e:t}),null),r([t]));i=window.currency!==window.defaultCurrency?0:i;var c=this.promos.value.some((function(t){return t.is_free_shipping}));this.cart.dispatch("discount",i||0),this.cart.dispatch("is_free_shipping",c)}},{key:"updateBonusItems",value:function(){var t=this,e=this.cart.state.items||[];if(0==e.length)return this.cart.dispatch("bonus_items",[]);var r=(this.cart.state.bonus_items||[]).reduce((function(r,n){var o=n.promotion_info,i=o.bonus_items_active,c=o.is_variation,a=o.promotion_id,u=e.some((function(t){return c?(i||[]).includes(t.id):(i||[]).includes(t.product_id)})),s=t.promos.value.some((function(t){return t.id==a}));return(u||s)&&r.push(n),r}),[]);this.cart.dispatch("bonus_items",r)}},{key:"calcDiscountCustomerLevel",value:function(){var t=this.cart.state.items,e=void 0===t?[]:t,r=window.useStore("customer").state.discount;if(r)if(window.IS_LEVEL_DISCOUNT_TO_DISCOUNTED_PRODUCTS){var n=e.filter((function(t){return!(t.discount_once||t.discount_once_web||t.discount_retail_price_web||t.product_retail_price!=t.retail_price&&t.product_retail_price)})),o=e.filter((function(t){return t.discount_once||t.discount_once_web||t.discount_retail_price_web||t.product_retail_price!=t.retail_price&&!!t.product_retail_price})),i=this.calcTotalPrice(n),c=this.calcTotalPrice(o);if(2==r.discount_type){var a=i/(i+c)*r.discount;this.cart.dispatch("discount_customer_level",Math.floor(a)||0)}else{var u=Math.floor(r.discount*i/100);this.cart.dispatch("discount_customer_level",u)}}else if(2==r.discount_type)this.cart.dispatch("discount_customer_level",r.discount||0);else{var s=this.cartTotalPrice(),l=Math.floor(r.discount*s/100);this.cart.dispatch("discount_customer_level",l)}}},{key:"calcTotalPrice",value:function(t){var e=this.cart.state,r=e.is_active_combo,o=e.total_price_apply_combo;if(r)return Math.max(0,o);var i=t.reduce((function(t,e){var r,o=null===(r=e.product)||void 0===r?void 0:r.is_price_contact,i=c({},e);return i.retail_price=i.product_retail_price||i.retail_price,t+Math.max(0,(o?0:Object(n.getRetailPriceByCurrency)(i))*i.quantity)}),0);return Math.max(0,i)}},{key:"cartTotalPrice",value:function(){var t=this.cart.state,e=t.discount,r=void 0===e?0:e,o=t.surcharge,i=void 0===o?0:o,c=t.discount_shipping,a=void 0===c?0:c,u=t.is_active_combo,s=t.total_price_apply_combo,l=t.items,d=void 0===l?[]:l,p=t.discount_after_other_discount_coupon,f=void 0!==p&&p,h=t.referral_amount_discount,v=void 0===h?0:h,m=f?0:this.cart.state.coupon||0,_=d.reduce((function(t,e){var r=e.retail_price,n=e.retail_price_after_discount,o=e.discount_once||0,i=e.discount_retail_price_web?0:r-(n||r);return t+(e.is_price_contact?0:i*e.quantity+o)}),0);if(u)return Math.max(0,s-a-r-m+i-v);var y=d.reduce((function(t,e){var r,o=null===(r=e.product)||void 0===r?void 0:r.is_price_contact;return t+Math.max(0,(o?0:Object(n.getRetailPriceByCurrency)(e))*e.quantity)}),0);return Math.max(0,y-a-r-m+i-_-v)}}])&&f(e.prototype,r),o&&f(e,o),Object.defineProperty(e,"prototype",{writable:!1}),t}());window.discount=h}});