define(["jquery","knockout","ccRestClient","pubsub","navigation","ccResourceLoader!global/frigelarGlobalUtils","notifier","ccNumber","pageLayout/cart","ccResourceLoader!global/frigelarGlobalPaymentUtils"],function(e,t,n,r,i,s,o,u,a,f){"use strict";return{currency:t.observable(),installmentData:t.observable(null),valorsemfrete:t.observable(),valorFrete:t.observable(),maxDiscountEnabled:t.observable(null),maxInstallmentNotFees:t.observable(null),installmentMaxNotFees:t.observable(""),showBoleto:t.observable(!1),showPix:t.observable(!1),showCartao:t.observable(!1),onLoad:function(t){e.Topic(r.topicNames.CART_PRICE_COMPLETE).subscribe(function(){t.maxDiscountEnabled(t.getMaxDiscountValue()),t.maxInstallmentNotFees(t.getMaxInstallmentNotFees())}),e.Topic(r.topicNames.CART_ADD_SUCCESS).subscribe(function(){e.Topic("FRIGELAR_MANAGE_QUANTITY").publish()}),e.Topic(r.topicNames.CART_READY).subscribe(function(e){const n=s.validateCartStock.bind(t);n().then(function(){console.info("Validação de estoque finalizada")})}),e(document).on("click",".icons .cart",function(e){t.cartClick()}),e(document).on("click",".shadow, .cart-close, .icon-close",function(e){minicart.hideCart()}),e.Topic("FRIGELAR_MANAGE_QUANTITY").subscribe(function(){t.cart().items().forEach(function(t,n){t.updatableQuantity(t.quantity()),t.updatableQuantity.subscribe(function(n){if(["cart","checkout"].indexOf(s.currentPage())==-1){t.updatableQuantity&&t.updatableQuantity.isValid()&&t.updatableQuantity()>0&&t.updatableQuantity()!=t.quantity()&&e.Topic(r.topicNames.CART_UPDATE_QUANTITY).publishWith(t.productData(),[{message:"success",commerceItemId:t.commerceItemId}]);if(t.updatableQuantity()>t.orderableQuantity){t.updatableQuantity(t.orderableQuantity);var i="Quantidade máxima do estoque do produto atingida";o.sendMessage("addToCart",i,"error",!0)}}})})});var n=t.site().extensionSiteSettings.precificacao,i=parseInt(n.boletoDiscountPercentage),u=parseInt(n.creditCardDiscountPercentage),a=parseInt(n.pixDiscountPercentage);if(a>u&&i>=u||a>u&&i=i||i==u&&a==u)t.showPix(!1),t.showCartao(!0),t.showBoleto(!1);i>a&&i>u&&(t.showPix(!1),t.showCartao(!1),t.showBoleto(!0))},beforeAppear:function(t){var n=this;window.minicart=n,n.currency(n.cart().currency);var r=n.getFormattedInstallment();n.installmentData(r),e.Topic("FRIGELAR_MANAGE_QUANTITY").publish()},formatProductName:function(e){if(e){if(window.innerWidth<1024&&e.length>35)return e.substring(0,35)+"...";if(!(window.innerWidth<1024)&&e.length>110)return e.substring(0,80)+"..."}return e},cartClick:function(){var e=this;if(window.innerWidth<1024){var t=e.links().cart.route;i.goTo(t)}else e.showCart()},handleCartQuantityInput:function(e,t){return t.type==="blur"?(window.minicart.updateItemQuantity(this,parseInt(t.target.value)),!0):t.keyCode===8||t.keyCode===46?!0:t.keyCode===13?(window.minicart.updateItemQuantity(this,parseInt(t.target.value)),!0):!isNaN(Number(t.key))},updateItemQuantity:function(e,t){var n=a.getInstance();if(e&&e.updatableQuantity){var r=e.quantity();r&&typeof r=="number"?(e.updatableQuantity(t),n.updateItemQuantity(e,null,e.commerceItemId)):(e.updatableQuantity(e.quantity()),n.updateItemQuantity(e,null,e.commerceItemId))}},quantityPlus:function(e){var t=a.getInstance();if(e&&e.updatableQuantity){var n=e.quantity();n&&typeof n=="number"?(e.updatableQuantity(n+1),t.updateItemQuantity(e,null,e.commerceItemId)):(e.updatableQuantity(e.quantity()),t.updateItemQuantity(e,null,e.commerceItemId))}},quantityMinus:function(e){var t=a.getInstance();if(e&&e.updatableQuantity){var n=e.quantity();n&&typeof n=="number"&&n>1?(e.updatableQuantity(n-1),t.updateItemQuantity(e,null,e.commerceItemId)):(e.updatableQuantity(e.quantity()),t.updateItemQuantity(e,null,e.commerceItemId))}},hideCart:function(){e(".cartview .cart").css("right","-500px"),e(".cartview .shadow").css("opacity","0"),document.body.style.overflow="auto",document.getElementById("oracle-cc").style.overflow="auto",e("body").removeClass("modal-open");var t=setTimeout(function(){e(".cartview").hide()},1e3)},removeItem:function(t){var n=a.getInstance();n.removeItem(t),e.Topic("GTM_CART_REMOVE").publish(t)},showCart:function(){e(".cartview").show(1,function(){e(".cartview .cart").css("right","0px"),e(".cartview .shadow").css("opacity","1")}),document.body.style.overflow="auto",document.getElementById("oracle-cc").style.overflow="auto",e("body").addClass("modal-open");var t=this;t.sendProductsToGTM()},getFormattedInstallment:function(){var e=this,t=s.getInstallmentsForOrder();if(t){var n=t[t.length-1],r=n.totalCost,i=n.totalPrice,o=n.amount,a=u.formatNumber(n.amount,!0,e.cart().currency.fractionalDigits),f="em até __installment__x de __price__ sem juros",l=f.replace("__installment__",n.installment);l=l.replace("__price__",e.cart().currency.symbol.toString().concat(a)),e.valorFrete(r);var c=i-r;return e.valorsemfrete(c),n.displayText=l,n}},sendProductsToGTM:function(){var t=this,n={products:t.cart().items(),total:t.cart().amount()};e.Topic("GTM_CART_IMPRESSIONS").publish(n)},getMaxDiscountValue:function(){var e=this;if(e.cart()){var t=e.cart().subTotal(),n=f.getHigherDiscountBySettings(t);return n}return null},getMaxInstallmentNotFees:function(){var e=this;if(e.cart()){var t=e.cart().subTotal(),n=f.getCreditCardDiscountInformations_MaxInstallmentWithoutFees(t);return n&&e.installmentMaxNotFees(" em "+n.installment+"x de "+n.formattedInstallment+" sem juros"),n}return null}}})