jQuery(function(c){"use strict";var o=null;var t={init:function(){c(".ep-variation-swatches__field-color:not(.is-multicolor) input").wpColorPicker();c(document.body).on("click",".ep-variation-swatches__field-image .button-add-image, .ep-variation-swatches__field-image img",this.uploadImage).on("click",".ep-variation-swatches__field-image .button-remove-image",this.removeImage);c("#ep_variation_swatches_data").on("change",".ep-variaton-swatches__type-field select",this.toggleSwatchesMetabox).on("change",".ep-variaton-swatches__size-field select",this.toggleCustomSizeFields);c("#variable_product_options").on("reload",this.reloadSwatchesPanel);c("#product_attributes").on("click","button.add_new_attribute_with_swatches",this.openNewAttributeDialog);c("#ep-variation-swatches-new-term-dialog").on("input",'input[name="attribute_name"]',this.validateDialogInputs).on("click","button.media-modal-close, .media-modal-backdrop",this.closeDialog).on("click","button.button-add",this.ajaxAddTerm)},uploadImage:function(t){t.preventDefault();var a=c(this);if(o){o.off("select")}else{o=wp.media({title:a.attr("aria-label"),button:{text:a.data("choose")},multiple:false})}o.on("select",function(){var t=o.state().get("selection").first().toJSON(),e=t.sizes&&t.sizes.thumbnail?t.sizes.thumbnail.url:t.url,i=a.closest(".ep-variation-swatches__field-image");i.find('input[type="hidden"]').val(t.id);i.find(".button-remove-image").removeClass("hidden");i.find("img").attr("src",e);i.removeClass("is-empty")});o.open()},removeImage:function(t){t.preventDefault();var e=c(this),i=e.closest(".ep-variation-swatches__field-image");e.addClass("hidden");i.addClass("is-empty");i.find('input[type="hidden"]').val("");i.find("img").attr("src",function(){return this.dataset.placeholder})},toggleSwatchesMetabox:function(){if(this.value&&this.value!=="select"&&this.value!=="button"){c(this).closest(".options_group").siblings(".options_group--swatches").find(".form-field__swatches-"+this.value).show().siblings().hide()}else{c(this).closest(".options_group").siblings(".options_group--swatches").children().hide()}},toggleCustomSizeFields:function(){if(this.value==="custom"){c(this).closest(".form-field").next(".form-field--custom-size").show()}else{c(this).closest(".form-field").next(".form-field--custom-size").hide()}},reloadSwatchesPanel:function(){var t=window.location.toString();t=t.replace("post-new.php?","post.php?post="+woocommerce_admin_meta_boxes.post_id+"&action=edit&");c("#ep_variation_swatches_data").load(t+" #ep_variation_swatches_data_inner",function(){setTimeout(function(){c(".ep-variation-swatches__field-color:not(.is-multicolor) input").wpColorPicker()})})},closeDialog:function(t){t.preventDefault();c("#ep-variation-swatches-new-term-dialog").hide()},openNewAttributeDialog:function(t){t.preventDefault();var e=c(this),i=c("#ep-variation-swatches-new-term-dialog"),a={type:e.data("type"),taxonomy:e.closest(".woocommerce_attribute.wc-metabox").data("taxonomy")};c('input[name="attribute_taxonomy"]',i).val(a.taxonomy);c('input[name="attribute_type"]',i).val(a.type);c(".form-field__swatches",i).children().hide().filter(".ep-variation-swatches__field-"+a.type).show();i.show()},validateDialogInputs:function(){if(!this.value){this.classList.add("error")}else{this.classList.remove("error")}},ajaxAddTerm:function(t){t.preventDefault();var i=c(this),a=c("#ep-variation-swatches-new-term-dialog"),o=c(".spinner",a),s=c(".ep-variation-swatches-modal__message",a),n=c(":input",a).serializeObject();if(!n.attribute_name){c('input[name="attribute_name"]',a).get(0).focus();return}o.addClass("is-active");s.hide();i.prop("disabled",true);wp.ajax.send("ep_variation_swatches_add_term",{data:n,error:function(t){o.removeClass("is-active");s.addClass("error").text(t).show();i.prop("disabled",false)},success:function(t){o.removeClass("is-active");s.addClass("success").text(t.message).show();i.prop("disabled",false);c('input[name="attribute_name"]',a).val("").removeClass("error");c(".button-remove-image",a).trigger("click");c(".wp-picker-clear",a).trigger("click");c(".ep-variation-swatches__field-color > :input",a).val("");c(".ep-variation-swatches__field-label > :input",a).val("");var e=c('.woocommerce_attribute.wc-metabox[data-taxonomy="'+n.attribute_taxonomy+'"]',"#product_attributes");c("select.attribute_values",e).append('<option value="'+t.term_id+'" selected="selected">'+n.attribute_name+"</option>").trigger("change");a.get(0).dispatchEvent(new CustomEvent("ep_variation_swatches_term_added"))}})}};t.init()});