var PC=PC||{};PC.fe=PC.fe||{},PC.fe.views=PC.fe.views||{},PC.options=PC.options||{},PC.conditionalLogic={should_sync:!0},jQuery(function(_){var c,f=PC._us||window._,g=!1,i=new Backbone.Collection;function a(e,t){t&&this.el&&"IMG"==this.el.tagName&&u(this.model)&&(t=!1),this.$el.toggle(t),this.$el.toggleClass("cshow-hidden",!t),wp.hooks.doAction("mkl_conditions_show_hide_choice",this,t)}function t(e,t){e.set("available",!t),this.$el.toggleClass("cdisabled",t),this.$("> .choice-item").prop("disabled",t).attr("aria-disabled",t?"true":"false"),"form"==e.collection.layer_type&&this.$("span input, select, .upload-field input").prop("disabled",t),t&&e.get("active")&&("simple"==e.collection.layer_type?e.collection.layer.get("can_deselect")?e.set("active",!1):e.collection.resetChoice():"multiple"==e.collection.layer_type&&e.set("active",!1)),wp.hooks.doAction("mkl_conditions_disabled_choice",this,t)}function n(e){c&&!e||(e=PC.fe.currentProductData,c=e.conditions)}function o(e,t){var i;wp.hooks.applyFilters("mkl_pc_conditionals.use_jquery_toggle",this.model&&!this.model.get("is_step"))&&this.$el.toggle(t),this.$el.toggleClass("cshow-hidden",!t),this.choices&&this.choices.$el?(i=!this.model.get("parent"),wp.hooks.applyFilters("mkl_pc_conditionals.toggle_choices",i)&&this.choices.$el.toggle(t)):console.log("this.choices not found",this,e)}wp.hooks.addAction("PC.fe.setConfig.before","MKL/PC/Conditional",function(){PC.conditionalLogic.should_sync=!1}),wp.hooks.addAction("PC.fe.setConfig","MKL/PC/Conditional",function(){PC.conditionalLogic.should_sync=!0}),wp.hooks.addAction("PC.fe.init","MKL/PC/Conditional",function(e,t){e!=t&&PC.fe.currentProductData.product_info.mode&&"share_layers_config"===PC.fe.currentProductData.product_info.mode&&(PC.fe.currentProductData.conditions=[],PC.productData["prod_"+e])&&PC.productData["prod_"+e].conditions&&(PC.fe.currentProductData.conditions=PC.productData["prod_"+e].conditions),n(!0),l()},30),wp.hooks.addAction("PC.fe.layers_list_item.init","MKL/PC/Conditional",function(e){n(),c&&e.listenTo(e.options.model,"change:cshow",o)}),wp.hooks.addAction("PC.fe.choice-img.init","MKL/PC/Conditional",function(t){var e,i,o;n(),c&&(t.listenTo(t.model,"change:cshow",a),e=PC.fe.layers.get(t.model.get("layerId")),i=function(e){var t=e.get("parent");return t&&(e=e.collection.get(t))?e&&e.get("parent")?(t=i(e),[].concat([e],t)):[e]:[]},t.listenTo(e,"change:cshow",a),(o=(o=i(t.model)).concat(i(e))).length)&&f.each(o,function(e){t.listenTo(e,"change:cshow",a)})}),wp.hooks.addAction("PC.fe.angle_view.init","MKL/PC/Conditional",function(e){n(),c&&e.listenTo(e.model,"change:cshow",function(e,t){this.$el.toggle(t),this.$el.toggleClass("cshow-hidden",!t)})}),wp.hooks.addAction("PC.fe.choice.init","MKL/PC/Conditional",function(e){n(),c&&(e.listenTo(e.model,"change:cshow",a),e.listenTo(e.model,"change:cdisabled",t))}),wp.hooks.addAction("PC.fe.form.item.change","MKL/PC/Conditional",function(e){n(),c&&i.length&&i.findWhere({item:e.get("layerId")+"_"+e.id})&&l()},30),wp.hooks.addAction("PC.fe.text_overlay.item.change","MKL/PC/Conditional",function(e,t){n(),c&&t&&t.collection&&"text-overlay"===t.collection.layer_type&&i.length&&i.findWhere({item:t.get("layerId")+"_"+t.id})&&l()},30),wp.hooks.addAction("variation_content_loaded","MKL/PC/Conditional",function(e){e.response&&e.response.conditions&&e.response.conditions.length&&(PC.fe.currentProductData.conditions=e.response.conditions,n(!0))}),wp.hooks.addAction("PC.fe.start","MKL/PC/Conditional",function(e){l(),e.listenTo(PC.fe.angles,"change:active",function(e){l({layerId:-10,choiceId:e.id})})},30),wp.hooks.addAction("PC.fe.reset_configurator","MKL/PC/Conditional",function(){l()}),wp.hooks.addAction("PC.fe.setConfig","MKL/PC/Conditional",function(e){l(),f.each(e,function(e){var t=PC.fe.layers.get(e.layer_id);t&&t.get("sync")&&(t=PC.fe.getLayerContent(e.layer_id))&&(t=t.get(e.choice_id))&&y(t)})}),wp.hooks.addAction("PC.fe.choice.set_choice","MKL/PC/Conditional",function(e,t){l({layerId:e.get("layerId"),choiceId:e.id}),setTimeout(function(){y(e),wp.hooks.doAction("conditions.set_choice.after",e)},10)});var l=function(r){var t,h;n(),c&&(t=[],h=[],f.each(c,function(e){var n,l,s,d;("boolean"==typeof e.enabled?e.enabled:parseInt(e.enabled))&&(n=0,l=!r||e.always_check&&parseInt(e.always_check),d=s=!1,l||f.findWhere(e.actions,{action:"save_result"})&&(l=!0),f.each(e.rules,function(e){if(e.actioner.layerId&&e.actioner.choiceId){!l&&r&&r.layerId===e.actioner.layerId&&(l=!0);var t,i,o=r&&e.actioner.layerId==r.layerId&&(e.actioner.choiceId==r.choiceId||-1==e.actioner.choiceId);if(!s&&o&&(s=!0),o&&"clicked"==e.action)d=!0,n++;else{if(-10==e.actioner.layerId)return!e.actioner.choiceId||!(o=PC.fe.angles.get(e.actioner.choiceId))||("selected"!==e.action||!o.get("active"))&&("not_selected"!==e.action||o.get("active"))?void 0:void n++;if(-15==e.actioner.layerId)return e.actioner.choiceId?(o="condition_"+e.actioner.choiceId,"true"!==e.action&&"selected"!==e.action||!h[o]||n++,"false"===e.action&&!1===h[o]&&n++,void(l=!0)):void 0;if((o=PC.fe.getLayerContent(e.actioner.layerId))&&(t=PC.fe.layers.get(e.actioner.layerId))){if(-1==e.actioner.choiceId){if("visible"===e.action&&"visible"===e.action&&!u(t))return void n++;if("hidden"===e.action&&"hidden"===e.action&&u(t))return void n++;if("selected"===e.action)if(o.filter(function(e){return e.get("active")&&!u(e)}).length)return void n++}if(-2==e.actioner.choiceId)if(!o.filter(function(e){return e.get("active")&&!u(e)}).length)return void n++;if((i=o.get(e.actioner.choiceId))&&(g||-1==["equals","not_equal","greater_than","less_than","less_than_or_equal","greater_than_or_equal","not_empty","empty"].indexOf(e.action)||m(i,t),!1!==t.get("cshow"))){if(i.get("is_group"))return o=o.filter(function(e){return e.get("active")&&i.id==e.get("parent")&&!u(e)}),("selected"!==e.action||!o.length)&&("not_selected"!==e.action||o.length)?void 0:void n++;if((-1!=["equals","not_equal","greater_than","less_than","greater_than_or_equal","less_than_or_equal"].indexOf(e.action)&&e.action_value||-1!=["not_empty","empty"].indexOf(e.action))&&parseInt(e.state)&&("form"==t.get("type")&&void 0!==i.get("field_value")||"form"!==t.get("type")&&("text-overlay"===t.get("type")&&void 0!==i.get("text")||"text-overlay"!==t.get("type")&&void 0!==i.get("field_value"))&&i.get("active"))){var c,a="text-overlay"===t.get("type")?i.get("text")||"":i.get("field_value");switch(-1!=["radio","select"].indexOf(i.get("text_field_type"))&&(o=i.get("select_field_options"))[c=i.get("field_value")-1]&&(o[c].value?a=o[c].value:o[c].label&&(a=o[c].label)),e.action){case"equals":a==e.action_value&&n++;break;case"not_equal":a!=e.action_value&&n++;break;case"greater_than":parseFloat(a)>parseFloat(e.action_value)&&n++;break;case"less_than":parseFloat(a)<parseFloat(e.action_value)&&n++;break;case"greater_than_or_equal":parseFloat(a)>=parseFloat(e.action_value)&&n++;break;case"less_than_or_equal":parseFloat(a)<=parseFloat(e.action_value)&&n++;break;case"empty":a&&0!==a.length&&("string"!=typeof a||""!==a.trim())||n++;break;case"not_empty":a&&("string"==typeof a&&""!==a.trim()||"number"==typeof a&&0!==a)&&n++}}else(i.get("active")&&parseInt(e.state)&&"selected"===e.action||!i.get("active")&&parseInt(e.state)&&"not_selected"===e.action||"visible"===e.action&&!u(t)&&!u(i)||"hidden"===e.action&&u(i))&&n++}}}}}),wp.hooks.applyFilters("mkl_pc_conditionals.proceed",l))&&("AND"==e.relationship&&n==e.rules.length||"OR"==e.relationship&&n?d?t=t.concat(e.actions):(h["condition_"+e._id]=!0,f.each(e.actions,P,_.extend({clicked:s},e))):parseInt(e.reversible)?(h["condition_"+e._id]=!1,f.each(e.actions,k,_.extend({clicked:s},e))):h["condition_"+e._id]=!1)}),t.length&&f.each(t,P,{clicked:!0,do_last:t}),g=!0,wp.hooks.doAction("mkl_checked_conditions"))},s=function(e){if(e&&e.get("parent")){var t=e.collection.get(e.get("parent"));if(t&&!1===t.get("cshow"))return!0;if(t)return t.id!=e.id&&s(t)}return!1},u=function(e){return!!(!1===e.get("cshow")||e.collection.hasOwnProperty("layer")&&!1===e.collection.layer.get("cshow")||e.collection.hasOwnProperty("layer")&&s(e.collection.layer)||s(e))};function p(e,t,i){var o,c="";return!(c=(c=PC.fe.lang?e["message_"+PC.fe.lang.replace("-","_")]||"":c)||e.message||"")&&PC_config.lang&&(c=PC_config.lang.conditional_invalid_message||""),e=c=c||"{{layer}} > {{choice}} is not valid for the selected configuration.",o={layer:(c=t)&&(wp.hooks.applyFilters("PC.fe.configurator.layer_data",_.extend({},c.attributes)).name||c.get("name"))||"",choice:(t=i)&&((c=wp.hooks.applyFilters("PC.fe.configurator.choice_data",_.extend({},t.attributes))).name||c.text_field_label||(t.get_name?t.get_name():t.get("name")))||""},PC.utils&&PC.utils.get_message_with_vars?PC.utils.get_message_with_vars(e,o):e.replace(/{{(.*?)}}/g,function(e,t){return o[t.trim()]||""})}function C(e,t){return t&&t._id?"condition_"+t._id:"condition_"+e.layerId+"_"+e.choiceId}function y(t){var i,e=t.collection.layer.get("sync");e&&(i=t.collection.findIndex(t),e=PC.fe.layers.where({sync:e}),f.each(e,function(e){var e=PC.fe.getLayerContent(e.id);e&&(e=e.at(i))!==t&&e&&e.collection.selectChoice(e.id,t.get("active"))}))}function P(t){if("angle"==t.type){var e=PC.fe.angles.get(t.layerId);if(e)switch(t.action){case"hide":e.set("cshow",!1);break;case"show":e.set("cshow",!0)}}else{var i,o,c,a,n,l,s=PC.fe.layers.get(t.layerId);if(s)if("layer"==t.type)switch(t.action){case"hide":s.set("cshow",!1);break;case"show":s.set("cshow",!0);break;case"select":s.set("active",!0),PC.fe.last_clicked&&PC.fe.last_clicked.model&&PC.fe.last_clicked.model.collection&&PC.fe.last_clicked.model.collection.layer&&(wp.hooks.applyFilters("mkl_pc_conditionals.hide_triggering_layer",!0)&&PC.fe.last_clicked.model.collection.layer.id!=s.get("parent")&&(PC.fe.last_clicked.model.collection.layer.set("active",!1),PC.fe.last_clicked.model.collection.layer.get("parent"))&&s.get("parent")!=PC.fe.last_clicked.model.collection.layer.get("parent")&&(d=PC.fe.layers.get(PC.fe.last_clicked.model.collection.layer.get("parent")))&&d.set("active",!1),wp.hooks.doAction("conditional.selected_layer",s));break;case"open":this.clicked&&(s.set("active",!0),PC.fe.last_clicked)&&PC.fe.last_clicked.model&&PC.fe.last_clicked.model.collection&&PC.fe.last_clicked.model.collection.layer&&(wp.hooks.applyFilters("mkl_pc_conditionals.hide_triggering_layer",!0)&&PC.fe.last_clicked.model.collection.layer.id!=s.get("parent")&&(PC.fe.last_clicked.model.collection.layer.set("active",!1),PC.fe.last_clicked.model.collection.layer.get("parent"))&&s.get("parent")!=PC.fe.last_clicked.model.collection.layer.get("parent")&&(d=PC.fe.layers.get(PC.fe.last_clicked.model.collection.layer.get("parent")))&&d.set("active",!1),wp.hooks.doAction("conditional.selected_layer",s));break;case"deselect":s.set("active",!1);break;case"sync":if(this.clicked&&PC.fe.last_clicked&&PC.fe.last_clicked.model&&PC.fe.last_clicked.model.collection&&PC.fe.last_clicked.model.collection.layer){var d=PC.fe.last_clicked.model.collection.findIndex(PC.fe.last_clicked.model),r=PC.fe.getLayerContent(s.id);if(!r)return;r=r.at(d);r&&r.collection.selectChoice(r.id,PC.fe.last_clicked.model.get("active"))}break;case"show_in_menu":s.set("hide_in_configurator",!1);break;case"hide_in_menu":s.set("hide_in_configurator",!0);break;case"make_required":s.set("required",1);break;case"make_not_required":s.set("required",0);break;case"reset":(l=PC.fe.getLayerContent(t.layerId))&&(l.resetChoice(),wp.hooks.doAction("conditional/reset_layer",s))}else if("choice"==t.type||"group"==t.type)if(l=PC.fe.getLayerContent(t.layerId))if(-1==t.choiceId)l.each(function(e){e.set("cshow","show"===t.action)});else{var h=l.get(t.choiceId);if(h){switch(t.action){case"hide":h.set("cshow",!1);break;case"show":h.set("cshow",!0);break;case"disable":h.set("cdisabled",!0);break;case"enable":h.set("cdisabled",!1);break;case"invalidate":i=h,o=s,c=t,a=this,(n=_.extend({},i.get("conditional_invalid_messages")||{}))[C(c,a)]=p(c,o,i),i.set({conditional_invalid:!0,conditional_invalid_messages:n});break;case"select":"group"!=t.type&&(h.collection.selectChoice(h.id,!0),PC.conditionalLogic.should_sync)&&setTimeout(function(){y(h)},60);break;case"deselect":"group"!=t.type&&(h.collection.selectChoice(h.id,!1),PC.conditionalLogic.should_sync)&&setTimeout(function(){y(h)},60);break;case"reset":"group"==t.type?function t(e){f.each(e.collection.where({parent:e.id}),function(e){e.get("is_group")?t(e):e.collection.selectChoice(e.id,!1)})}(h):h.collection.selectChoice(h.id,!1),wp.hooks.doAction("conditional/reset_choice",h,s)}v(h)}}}}function k(t){if("angle"==t.type){var e=PC.fe.angles.get(t.layerId);if(e)switch(t.action){case"hide":e.set("cshow",!0);break;case"show":e.set("cshow",!1)}}else{var i,o,c,a,n=PC.fe.layers.get(t.layerId);if(n)if("layer"==t.type)switch(t.action){case"hide":n.set("cshow",!0);break;case"show":n.set("cshow",!1);break;case"show_in_menu":n.set("hide_in_configurator",!0);break;case"hide_in_menu":n.set("hide_in_configurator",!1);break;case"make_required":n.set("required",0);break;case"make_not_required":n.set("required",1)}else if("choice"==t.type||"group"==t.type){var l=PC.fe.getLayerContent(t.layerId);if(l)if(-1==t.choiceId)l.each(function(e){e.set("cshow",!("show"===t.action))});else{var s=l.get(t.choiceId);if(s){switch(t.action){case"hide":s.set("cshow",!0);break;case"show":s.set("cshow",!1);break;case"disable":s.set("cdisabled",!1);break;case"enable":s.set("cdisabled",!0);break;case"invalidate":i=s,o=t,c=this,delete(a=_.extend({},i.get("conditional_invalid_messages")||{}))[C(o,c)],i.set({conditional_invalid:!!f.keys(a).length,conditional_invalid_messages:a});break;case"select":"group"!=t.type&&"multiple"==n.get("type")&&(s.set("active",!1),PC.conditionalLogic.should_sync)&&setTimeout(function(){y(s)},60)}v(s)}}}}}function v(e){var t;"simple"===e.collection.layer_type&&(t=e.collection.findWhere({active:!0}))&&(t.get("cshow"),(t=!!t.get("parent")&&e.collection.get(t.get("parent")))&&t.get("cshow"),e.collection.each(function(e){}))}var m=function(e,t){i.add({item:t.id+"_"+e.id,choice:e})};wp.hooks.addFilter("PC.fe.save_data.parse_choices.add_choice","MKL/PC/Conditional",function(e,t){var i=!1===t.collection.layer.get("cshow"),t=!1===t.get("cshow");return!i&&!t&&e}),wp.hooks.addAction("PC.fe.save_data.parse_choices.after","MKL/PC/Conditional",function(i){var e;u(i)||(e=PC.fe.getLayerContent(i.id))&&e.each(function(e){var t;!e.get("conditional_invalid")||u(e)||"form"!==i.get("type")&&!e.get("active")||PC.fe.errors.push({choice:e,message:(e=(e=e).get("conditional_invalid_messages")||{},(t=f.keys(e)).length?e[t[0]]:"")})})},30),PC.conditionalLogic.check_conditions=l,PC.conditionalLogic.parent_is_hidden=s,PC.conditionalLogic.item_is_hidden=u});