var PC=PC||{};var tax_info=tax_info||null;(function($,_){var should_update_prices=!1;let product_qty_changed=!1;let base_product_price=0;if(!wp||!wp.hooks)return;wp.hooks.addAction('PC.fe.start','mkl.pc.extra-price',function(configurator){if(configurator.$('.extra-cost').length)configurator.$el.addClass('display-extra-price-total');});wp.hooks.addAction('PC.fe.choice.change','mkl.pc.extra-price',set_sub_total);wp.hooks.addAction('PC.fe.form.item.change','mkl.pc.extra-price',formItemChange,20);wp.hooks.addAction('PC.fe.open','mkl.pc.extra-price',set_sub_total);wp.hooks.addAction('PC.fe.qty_changed','mkl.pc.extra-price',on_qty_change,50);wp.hooks.addAction('PC.fe.linked_product_qty.updated','mkl.pc.extra-price',function(){set_sub_total()});wp.hooks.addAction('mkl_checked_conditions','mkl.pc.extra-price',set_sub_total);wp.hooks.addAction('mkl_checked_conditions.clicked','mkl.pc.extra-price',set_sub_total);wp.hooks.addAction('PC.fe.choice.change','mkl.pc.extra-price',calculateRelativePrice,900);function calculateRelativePrice(choice){if(PC_config.config.relative_price&&'simple'==choice.collection.layer_type){var current_price=get_extra_price(choice)||0;if(!choice.collection.findWhere('active')){current_price=0}
choice.collection.each(function(item){var item_price=get_extra_price(item)||0;item.set('extra_price_to_display',item_price-current_price)})}else{choice.set('extra_price_to_display',wp.hooks.applyFilters('PC.extra_price_to_display',get_extra_price(choice),choice))}}
wp.hooks.addAction('PC.fe.reset_configurator','mkl.pc.extra-price',function(){set_sub_total()});wp.hooks.addAction('PC.fe.configurator.choice-item.render.after-template','mkl.pc.extra-price',function(choice){if(!$('#tmpl-mkl-pc-extra-price').length)return;if(!wp.hooks.applyFilters('PC.fe.show.extra_price',!0,choice,'choice-item'))return;var ep=new ExtraPrice({model:choice.model,context:'toolbar'});if(choice.$(' > .choice-item .choice-text--container').length){choice.$(' > .choice-item .choice-text--container').append(ep.$el)}else{choice.$(' > .choice-item').append(ep.$el)}
choice.extra_price=ep});wp.hooks.addAction('PC.fe.configurator.summary-item.render.after-template','mkl.pc.extra-price',function(summary_item){if(!$('#tmpl-mkl-pc-extra-price').length)return;if(!wp.hooks.applyFilters('PC.fe.show.extra_price',!0,summary_item.model,'summary-item'))return;var ep=new ExtraPrice({model:summary_item.model,no_relative_price:!0,show_plus_sign:!1,context:'summary'});summary_item.$el.append(ep.$el)});wp.hooks.addAction('PC.fe.models.choice.init','mkl.pc.extra-price',function(choice){choice.set('extra_price_to_display',wp.hooks.applyFilters('PC.extra_price_to_display',get_extra_price(choice),choice))},30);function formItemChange(model,event){if('select'==model.get('text_field_type')){var items=model.get('select_field_options');var selection=event.target.selectedIndex;if(items[selection]&&'undefined'!==typeof items[selection].option_price&&''!==items[selection].option_price){model.set('extra_price',items[selection].option_price);model.set('extra_price_from_form_field',!0)}else{if(model.get('extra_price_from_form_field'))model.set('extra_price',0);}}else if('radio'==model.get('text_field_type')){var items=model.get('select_field_options');var selection=model.get('field_value')-1;if(items[selection]&&'undefined'!==typeof items[selection].option_price&&''!==items[selection].option_price){model.set('extra_price',items[selection].option_price);model.set('extra_price_from_form_field',!0)}else{if(model.get('extra_price_from_form_field'))model.set('extra_price',0);}}}
function get_sub_total(){var subtotal=0;var has_calculation_error=0;if(PC.fe.layers){PC.fe.layers.each(function(layer){if(item_is_hidden(layer))return;var layer_contents=PC.fe.getLayerContent(layer.id)
if(!layer_contents)return;layer_contents.each(function(choice){if(item_is_hidden(choice))return;if('form'==layer.get('type')||choice.get('has_text_field')){if('form'!=layer.get('type')&&!choice.get('active'))return;var field_type=choice.get('text_field_type');if(('number'==field_type||'mkl_quantity'==field_type)&&choice.get('field_value')&&choice.get('price_per_unit')){var field_price=parseFloat(choice.get('field_value'))*parseFloat(choice.get('price_per_unit'));if(field_price)subtotal+=field_price}
if('form'==layer.get('type')&&('select'==field_type||'radio'==field_type)&&choice.get('extra_price')){var field_price=parseFloat(choice.get('extra_price'));if(field_price)subtotal+=field_price}
if('calculation'==field_type){var formula=choice.get('number_field_calculation_formula');if(formula){if(update_calculation){var calculated_price;if(window.pc_extra_price_debug_formula){console.log(parse_formula(formula,choice,'number'),formula,choice)}
try{calculated_price=mexp.eval(parse_formula(formula,choice,'number'))}catch(error){console.error('Caculation error: ',error);console.error('Formula',parse_formula(formula,choice,'number'),choice);console.trace(error);if(!has_calculation_error){has_calculation_error=1;alert('There seems to be an error in the calculation field. Check the browser console for details')}}
var calculation_min_value=choice.get('calculation_min_value');var calculation_max_value=choice.get('calculation_max_value');if(!isNaN(calculated_price)){if('undefined'!=typeof calculation_min_value&&calculated_price<calculation_min_value){calculated_price=calculation_min_value}else if('undefined'!=typeof calculation_max_value&&calculation_max_value>calculation_min_value&&calculated_price>calculation_max_value){calculated_price=calculation_max_value}
choice.set('extra_price',calculated_price)}}
subtotal+=choice.get('extra_price')}}
if(choice.get('extra_price')&&('text'==field_type||'textarea'==field_type||'checkbox'==field_type)&&'form'==layer.get('type')){var content=choice.get('field_value');if(content){subtotal+=choice.get('extra_price')}}}
wp.hooks.doAction('PC.fe.extra_price.before.add.extra_price',choice,layer);if(!choice.get('active'))return;var item_price=get_extra_price(choice);subtotal+=wp.hooks.applyFilters('PC.fe.extra_price.item_price',item_price,choice,layer)})});subtotal=wp.hooks.applyFilters('PC.fe.extra_price.subtotal.before_apply_taxes',subtotal);if(subtotal)subtotal=maybe_apply_taxes(subtotal);}
return wp.hooks.applyFilters('PC.fe.extra_price.subtotal',subtotal)}
function on_qty_change(){product_qty_changed=!0;set_sub_total();product_qty_changed=!1}
function set_sub_total(){var subtotal=get_sub_total()||0;if(subtotal>0){$('.pc-price').html(' + '+PC.utils.formatMoney(subtotal));$('.extra-cost').addClass('show')}else if(subtotal<0){$('.pc-price').html(' '+PC.utils.formatMoney(subtotal));$('.extra-cost').addClass('show')}else{$('.pc-price').html('');$('.extra-cost').removeClass('show')}
const base_price=get_product_price();if($('.pc-total-price').length){if(PC_config.config.woocs_is_fixed_enabled||('undefined'!=typeof PC.fe.config.wcml_rate&&parseFloat(PC.fe.config.wcml_rate))){$('.pc-total-price').html(PC.utils.formatMoney((base_price+parseFloat(PC.utils.maybeConvertAmountToCurrency(subtotal))),!1),!1);if(PC.fe.currentProductData.product_info.regular_price&&PC.fe.currentProductData.product_info.is_on_sale&&$('.pc-total--regular-price').length){$('.pc-total--regular-price').html(PC.utils.formatMoney((parseFloat(PC.fe.currentProductData.product_info.regular_price)+parseFloat(PC.utils.maybeConvertAmountToCurrency(subtotal))),!1),!1)}}else{$('.pc-total-price').html(PC.utils.formatMoney(base_price+parseFloat(subtotal)));if(PC.fe.currentProductData.product_info.regular_price&&PC.fe.currentProductData.product_info.is_on_sale&&$('.pc-total--regular-price').length){$('.pc-total--regular-price').html(PC.utils.formatMoney((parseFloat(PC.fe.currentProductData.product_info.regular_price)+parseFloat(subtotal))))}}}
wp.hooks.doAction('PC.fe.extra_price.after.setSubTotal',base_price,subtotal)}
var formula_warning={done:[],show:function(message_id,message,always){if(always||!this.done[message_id]){console.warn('Configurator formula - '+message_id,message);this.done[message_id]=1}}}
var parse_formula=function(formula,formula_model,data_type){var fields=formula.match(new RegExp(/{field:(.*?)}/g));var formula_with_values=formula;if(fields){fields=fields.map(function(field){var key=field.replace('}','').replace('{field:','');var field_model=formula_model.collection.findWhere({'text_field_id':key});if('undefined'==typeof field_model)return;if('undefined'==typeof data_type||'number'==data_type){var value=field_model.get('field_value');if('select'==field_model.get('text_field_type')||'radio'==field_model.get('text_field_type')){var option=field_model.get('select_field_options')[value-1];if(option)value=option.value}
if(!value||isNaN(value))value=0}else if('labels'==data_type){var value=field_model.get('name');if(!value)value=''}
formula_with_values=formula_with_values.replace('{field:'+key+'}',value)})}
var single_layers=formula.matchAll(new RegExp(/{simple \(.*?\):(.*?)(\|default:(.*?))?}/g));for(var sl of single_layers){var choices=PC.fe.getLayerContent(parseInt(sl[1]))
if(!choices){formula_warning.show('single',['Configurator - price calculation: a single choice layer was not found; the value was defaulted by 0',{'Layer ID':sl[1]}]);if(sl[3]){formula_with_values=formula_with_values.replace(sl[0],sl[3])}else{formula_with_values=formula_with_values.replace(sl[0],0)}}else{var selection=choices.findWhere({active:!0});if(selection&&item_is_hidden(selection)){if(sl[3]){formula_with_values=formula_with_values.replace(sl[0],sl[3])}else{formula_with_values=formula_with_values.replace(sl[0],0)}}else if(selection&&'undefined'!=typeof selection.get('calculation_value')&&!isNaN(parseFloat(selection.get('calculation_value')))){formula_with_values=formula_with_values.replace(sl[0],selection.get('calculation_value'))}else if(selection&&typeof selection.get('has_text_field')&&'number'==selection.get('text_field_type')){formula_with_values=formula_with_values.replace(sl[0],selection.get('field_value'))}else{if(sl[3]){formula_with_values=formula_with_values.replace(sl[0],sl[3])}else{formula_with_values=formula_with_values.replace(sl[0],0)}}}}
var single_layers_linked_product=formula.matchAll(new RegExp(/{simple_link \(.*?\):(.*?)(\|default:(.*?))?}/g));for(var sl of single_layers_linked_product){var choices=PC.fe.getLayerContent(parseInt(sl[1])),selection=!1;if(choices){selection=choices.findWhere({active:!0})}else{formula_warning.show('single_linked',['Configurator - price calculation: a single choice layer was not found; the value was defaulted by 0',{'Layer ID':sl[1]}])}
if(selection&&'undefined'!=typeof selection.get('linked_product_price')&&!item_is_hidden(selection)){formula_with_values=formula_with_values.replace(sl[0],selection.get('linked_product_price'))}else{if(sl[3]){formula_with_values=formula_with_values.replace(sl[0],sl[3])}else{formula_with_values=formula_with_values.replace(sl[0],0)}}}
var multiple_layers=formula.matchAll(new RegExp(/{multiple \(.*?\):(.*?):(.*?)}/g));for(var ml of multiple_layers){var choices=PC.fe.getLayerContent(parseInt(ml[1]));if(choices){var selection=choices.get(parseInt(ml[2]));if(selection&&(item_is_hidden(selection)||!selection.get('active'))){formula_with_values=formula_with_values.replace(ml[0],0)}else if(selection&&selection.get('has_text_field')&&'number'==selection.get('text_field_type')){var value='undefined'!=typeof selection.get('field_value')?selection.get('field_value'):0;formula_with_values=formula_with_values.replace(ml[0],value)}else if(selection&&'undefined'!=typeof selection.get('calculation_value')){formula_with_values=formula_with_values.replace(ml[0],selection.get('calculation_value'))}else{if(!selection){formula_warning.show('multiple_choice',['Configurator - price calculation: a multiple choice item was not found; the value was defaulted by 0',{'Layer ID':ml[1],'Choice ID':ml[2]}])}
formula_with_values=formula_with_values.replace(ml[0],0)}}else{formula_warning.show('multiple_layer',['Configurator - price calculation: a multiple choice layer was not found; the value was defaulted by 0',{'Layer ID':ml[1],'Choice ID':ml[2]}]);formula_with_values=formula_with_values.replace(ml[0],0)}}
var multiple_layers_linked_product=formula.matchAll(new RegExp(/{multiple_link \(.*?\):(.*?):(.*?)}/g));for(var mll of multiple_layers_linked_product){var choices=PC.fe.getLayerContent(parseInt(mll[1]));if(choices){var selection=choices.get(parseInt(mll[2]))}else{var selection=!1;formula_warning.show('multiple_layer_linked',['Configurator - price calculation: a multiple choice layer was not found; the value was defaulted by 0',{'Layer ID':mll[1],'Choice ID':mll[2]}])}
if(selection&&'undefined'!=typeof selection.get('linked_product_price')&&!item_is_hidden(selection)&&selection.get('active')){formula_with_values=formula_with_values.replace(mll[0],selection.get('linked_product_price'))}else{formula_warning.show('multiple_choice_linked',['Configurator - price calculation: a multiple choice layer was not found; the value was defaulted by 0',{'Layer ID':mll[1],'Choice ID':mll[2]}]);if(mll[3]){formula_with_values=formula_with_values.replace(mll[0],mll[3])}else{formula_with_values=formula_with_values.replace(mll[0],0)}}}
var product_price=formula.matchAll(new RegExp(/{product_price}/g));for(var pp of product_price){formula_with_values=formula_with_values.replace(pp[0],get_product_price())}
var count_unique_values=formula_with_values.matchAll(new RegExp(/(count_unique_values\((.*?)\))/g));for(var uv of count_unique_values){var items=uv[2].split(',');items=items.map(function(item){return parseInt(item.trim())});formula_with_values=formula_with_values.replace(uv[0],_.unique(items).length)}
formula_with_values=wp.hooks.applyFilters('formula_with_values',formula_with_values,formula,formula_model,data_type);formula_with_values=formula_with_values.replaceAll(/{.*?}/ig,0);return formula_with_values}
var get_extra_price=function(choice){var ep=0;if(!choice.get('extra_price')){ep=0}else if(!choice.get('extra_price_type')||'amount'==choice.get('extra_price_type')){ep=choice.get('extra_price')}else if('percentage'==choice.get('extra_price_type')){ep=choice.get('extra_price')*get_product_price()/100}
return wp.hooks.applyFilters('pc.get_extra_price',ep,choice)};var maybe_apply_taxes=function(amount,return_inclusive=null){if(!tax_info||!PC_config.config.tax_enabled)return amount;const Settings=PC_config.config.tax_settings;const compound_rates=[];const normal_rates=[];let total_normal_rate=0;const should_return_inclusive=return_inclusive!==null?return_inclusive:'incl'===Settings.tax_display_mode;if(!Settings.adjust_non_base_location_prices&&Settings.tax_display_mode==='incl')return amount;if(!Settings.prices_include_tax&&Settings.tax_display_mode==='excl'&&return_inclusive===null)return amount;_.each(tax_info.tax_rates,function(rate){if(rate.compound==='yes'){compound_rates.push(rate)}else{normal_rates.push(rate);total_normal_rate+=rate.rate/100}});if(Settings.prices_include_tax){amount=amount*tax_info.price_without_tax_ratio}
if(should_return_inclusive){let tax_amount=0;_.each(normal_rates,function(rate){tax_amount+=amount*(rate.rate/100)});let price_with_taxes=amount+tax_amount;_.each(compound_rates,function(rate){price_with_taxes+=price_with_taxes*(rate.rate/100)});return price_with_taxes}
return amount};function get_price_without_tax(amount,def){if(tax_info?.price_without_tax_ratio){return amount*tax_info?.price_without_tax_ratio}
if(tax_info?.price_with_tax_ratio){return amount/tax_info?.price_with_tax_ratio}
return('undefined'!=typeof def)?def:amount}
function get_product_price(){if(!base_product_price||product_qty_changed){if(PC?.fe?.get_product_price){base_product_price=PC.fe.get_product_price()}else{base_product_price=parseFloat(PC.fe.currentProductData.product_info.price)||0}}
return base_product_price}
function get_tax_rates(){var configurator=$('.configure-product[data-product_id]');if(!configurator.length)return[];wp.ajax.send('mkl_pc_get_tax_rates',{data:{product_id:configurator.data('product_id'),},success:function(data){if('taxable'!=data.prod_tax_status)return;tax_info=data;should_update_prices=!0;console.log('got taxes');wp.hooks.doAction('PC.fe.extra_price.after.get_tax_rates');if(PC.fe&&PC.fe.modal){PC.fe.modal.trigger('PC.fe.extra_price.update_taxes')}
set_sub_total();wp.hooks.doAction('PC.fe.extra_price.after.update_price')},error:function(data){console.log(data)}})}
function item_is_hidden(item){if(PC.hasOwnProperty('conditionalLogic')&&PC.conditionalLogic){if(PC.conditionalLogic.hasOwnProperty('item_is_hidden')&&PC.conditionalLogic.item_is_hidden(item))return!0}
return!1}
var ExtraPrice=Backbone.View.extend({template:wp.template('mkl-pc-extra-price'),tagName:'span',className:'choice-price',initialize:function(options){this.options=options;this.listenTo(this.model.collection,'change:extra_price_to_display',this.render);this.listenTo(this.model,'change:on_sale',this.render);this.listenTo(PC.fe.modal,'PC.fe.extra_price.update_taxes',this.check_price);wp.hooks.addAction('PC.fe.start','mkl.pc.extra-price',this.check_price.bind(this),30);this.render()},render:function(){var data=_.extend({},this.model.attributes,{show_plus_sign:(this.options.show_plus_sign||!0)});if(!0===this.options.no_relative_price&&get_extra_price(this.model)){data.no_relative_price=!0;data.formatted_price=PC.utils.formatMoney(Math.abs(wp.hooks.applyFilters('PC.ExtraPrice.view.extra_price_to_display',maybe_apply_taxes(get_extra_price(this.model)),this.model,this)))}else if(this.model.get('extra_price_to_display')){data.formatted_price=PC.utils.formatMoney(Math.abs(wp.hooks.applyFilters('PC.ExtraPrice.view.extra_price_to_display',maybe_apply_taxes(this.model.get('extra_price_to_display')),this.model,this)))}else{data.formatted_price=!1}
data=wp.hooks.applyFilters('PC.ExtraPrice.view.data',data,this.model,this);this.$el.html(this.template(data));this.$el.toggleClass('no-price',!this.model.get('extra_price_to_display'));this.$el.toggleClass('on-sale',!!this.model.get('on_sale'))},check_price:function(){if(this.model.get('active')&&'simple'==this.model.collection.layer_type){calculateRelativePrice(this.model)}else if(!this.model.collection.findWhere('active',!0)||'multiple'==this.model.collection.layer_type){this.model.set('extra_price_to_display',wp.hooks.applyFilters('PC.extra_price_to_display',get_extra_price(this.model),this.model))}
this.render()}});$(function(){if(PC_config.config.tax_enabled)get_tax_rates();set_sub_total()});window.pc_maybe_apply_taxes=maybe_apply_taxes;window.pc_get_extra_price=get_extra_price;window.pc_extra_price_get_sub_total=get_sub_total;window.pc_get_price_without_tax=get_price_without_tax})(jQuery,PC._us||window._)