define(['jquery','Magento_Catalog/js/price-utils','underscore','mage/template','jquery-ui-modules/widget'],function($,utils,_,mageTemplate){'use strict';var globalOptions={productId:null,priceConfig:null,prices:{},priceTemplate:'<%- data.formatted %>'};$.widget('mage.priceBox',{options:globalOptions,qtyInfo:'#qty',_init:function initPriceBox(){var box=this.element;box.trigger('updatePrice');this.cache.displayPrices=utils.deepClone(this.options.prices);},_create:function createPriceBox(){var box=this.element;this.cache={};this._setDefaultsFromPriceConfig();this._setDefaultsFromDataSet();box.on('reloadPrice',this.reloadPrice.bind(this));box.on('updatePrice',this.onUpdatePrice.bind(this));$(this.qtyInfo).on('input',this.updateProductTierPrice.bind(this));box.trigger('price-box-initialized');},onUpdatePrice:function onUpdatePrice(event,prices){return this.updatePrice(prices);},updatePrice:function updatePrice(newPrices){var prices=this.cache.displayPrices,additionalPrice={},pricesCode=[],priceValue,origin,finalPrice;this.cache.additionalPriceObject=this.cache.additionalPriceObject||{};if(newPrices){$.extend(this.cache.additionalPriceObject,newPrices);}
if(!_.isEmpty(additionalPrice)){pricesCode=_.keys(additionalPrice);}else if(!_.isEmpty(prices)){pricesCode=_.keys(prices);}
_.each(this.cache.additionalPriceObject,function(additional){if(additional&&!_.isEmpty(additional)){pricesCode=_.keys(additional);}
_.each(pricesCode,function(priceCode){priceValue=additional[priceCode]||{};priceValue.amount=+priceValue.amount||0;priceValue.adjustments=priceValue.adjustments||{};additionalPrice[priceCode]=additionalPrice[priceCode]||{'amount':0,'adjustments':{}};additionalPrice[priceCode].amount=0+(additionalPrice[priceCode].amount||0)+
priceValue.amount;_.each(priceValue.adjustments,function(adValue,adCode){additionalPrice[priceCode].adjustments[adCode]=0+
(additionalPrice[priceCode].adjustments[adCode]||0)+adValue;});});});if(_.isEmpty(additionalPrice)){this.cache.displayPrices=utils.deepClone(this.options.prices);}else{_.each(additionalPrice,function(option,priceCode){origin=this.options.prices[priceCode]||{};finalPrice=prices[priceCode]||{};option.amount=option.amount||0;origin.amount=origin.amount||0;origin.adjustments=origin.adjustments||{};finalPrice.adjustments=finalPrice.adjustments||{};finalPrice.amount=0+origin.amount+option.amount;_.each(option.adjustments,function(pa,paCode){finalPrice.adjustments[paCode]=0+(origin.adjustments[paCode]||0)+pa;});},this);}
this.element.trigger('priceUpdated',this.cache.displayPrices);this.element.trigger('reloadPrice');},reloadPrice:function reDrawPrices(){var priceFormat=(this.options.priceConfig&&this.options.priceConfig.priceFormat)||{},priceTemplate=mageTemplate(this.options.priceTemplate);_.each(this.cache.displayPrices,function(price,priceCode){price.final=_.reduce(price.adjustments,function(memo,amount){return memo+amount;},price.amount);price.formatted=utils.formatPriceLocale(price.final,priceFormat);$('[data-price-type="'+priceCode+'"]',this.element).html(priceTemplate({data:price}));},this);},setDefault:function setDefaultPrices(prices){this.cache.displayPrices=utils.deepClone(prices);this.options.prices=utils.deepClone(prices);},_setOptions:function setOptions(options){$.extend(true,this.options,options);if('disabled'in options){this._setOption('disabled',options.disabled);}
return this;},_setDefaultsFromDataSet:function _setDefaultsFromDataSet(){var box=this.element,priceHolders=$('[data-price-type]',box),prices=this.options.prices;this.options.productId=box.data('productId');if(_.isEmpty(prices)){priceHolders.each(function(index,element){var type=$(element).data('priceType'),amount=parseFloat($(element).data('priceAmount'));if(type&&!_.isNaN(amount)){prices[type]={amount:amount};}});}},_setDefaultsFromPriceConfig:function _setDefaultsFromPriceConfig(){var config=this.options.priceConfig;if(config&&config.prices){this.options.prices=config.prices;}},updateProductTierPrice:function updateProductTierPrice(){var originalPrice,prices={'prices':{}};if(this.options.prices.finalPrice){originalPrice=this.options.prices.finalPrice.amount;prices.prices.finalPrice={'amount':this.getPrice('price')-originalPrice};}
if(this.options.prices.basePrice){originalPrice=this.options.prices.basePrice.amount;prices.prices.basePrice={'amount':this.getPrice('basePrice')-originalPrice};}
this.updatePrice(prices);},getPrice:function(priceKey){var productQty=$(this.qtyInfo).val(),result,tierPriceItem,i;for(i=0;i=tierPriceItem.qty&&tierPriceItem[priceKey]){result=tierPriceItem[priceKey];}}
return result;}});return $.mage.priceBox;});