jQuery(document).ready(e=>{"use strict";window.WC_Facebook_Google_Product_Category_Fields=class{constructor(t,o){this.categories=t,this.input_id=o;var a=e("#"+this.input_id);e('<div id="wc-facebook-google-product-category-fields"></div>').insertBefore(a).on("change","select.wc-facebook-google-product-category-select",t=>{this.onChange(e(t.target))}),this.addInitialSelects(a.val());var c=this.globalsHolder().enhanced_attribute_optional_selector;void 0!==c&&e("#"+c).on("change",function(){e(".wc-facebook-enhanced-catalog-attribute-optional-row").toggleClass("hidden",!e(this).prop("checked"))})}globalsHolder(){return"undefined"!=typeof facebook_for_woocommerce_product_categories?facebook_for_woocommerce_product_categories:"undefined"!=typeof facebook_for_woocommerce_settings_sync?facebook_for_woocommerce_settings_sync:facebook_for_woocommerce_products_admin}getPageType(){return"undefined"!=typeof facebook_for_woocommerce_product_categories?0===e("input[name=tag_ID]").length?this.globalsHolder().enhanced_attribute_page_type_add_category:this.globalsHolder().enhanced_attribute_page_type_edit_category:this.globalsHolder().enhanced_attribute_page_type_edit_product}addInitialSelects(e){if(e){this.getSelectedCategoryIds(e).forEach(e=>{this.addSelect(this.getOptions(e[1]),e[0])});var t=this.getOptions(e);Object.keys(t).length&&this.addSelect(t)}else this.addSelect(this.getOptions()),this.addSelect({})}requestAttributesIfValid(){if("true"===e("#wc_facebook_can_show_enhanced_catalog_attributes_id").val()&&(e(".wc-facebook-enhanced-catalog-attribute-row").remove(),this.isValid())){var t="#"+this.input_id,o=e(t).parents("div.form-field"),a=this.globalsHolder().enhanced_attribute_optional_selector;this.getPageType()===this.globalsHolder().enhanced_attribute_page_type_edit_category?o=e(t).parents("tr.form-field"):this.getPageType()===this.globalsHolder().enhanced_attribute_page_type_edit_product&&(o=e(t).parents("p.form-field")),e.get(this.globalsHolder().ajax_url,{action:"wc_facebook_enhanced_catalog_attributes",security:"",selected_category:e(t).val(),tag_id:parseInt(e("input[name=tag_ID]").val(),10),taxonomy:e("input[name=taxonomy]").val(),item_id:parseInt(e("input[name=post_ID]").val(),10),page_type:this.getPageType()},function(t){var c=e(t);e("#"+a,c).on("change",function(){e(".wc-facebook-enhanced-catalog-attribute-optional-row").toggleClass("hidden",!e(this).prop("checked"))}),c.insertAfter(o),e(document.body).trigger("init_tooltips")})}}onChange(t){t.hasClass("locked")&&t.closest(".wc-facebook-google-product-category-field").nextAll().remove();var o=t.val();if(o){var a=this.getOptions(o);Object.keys(a).length&&this.addSelect(a)}else(o=t.closest("#wc-facebook-google-product-category-fields").find(".wc-facebook-google-product-category-select").not(t).last().val())||this.addSelect({});e("#"+this.input_id).val(o),this.requestAttributesIfValid()}isValid(){return e(".wc-facebook-google-product-category-select").filter(function(t,o){return""!==e(o).val()}).length>=2}addSelect(t,o){var a=e("#wc-facebook-google-product-category-fields"),c=a.find(".wc-facebook-google-product-category-select"),r=e('<select class="wc-facebook-google-product-category-select"></select>');c.addClass("locked"),a.append(e('<div class="wc-facebook-google-product-category-field" style="margin-bottom: 16px">').append(r)),r.attr("data-placeholder",this.getSelectPlaceholder(c,t)).append(e('<option value=""></option>')),Object.keys(t).forEach(o=>{r.append(e('<option value="'+o+'">'+t[o]+"</option>"))}),r.val(o).select2({allowClear:!0})}getSelectPlaceholder(e,t){return 0===e.length?facebook_for_woocommerce_google_product_category.i18n.top_level_dropdown_placeholder:1===e.length&&0===Object.keys(t).length?facebook_for_woocommerce_google_product_category.i18n.second_level_empty_dropdown_placeholder:facebook_for_woocommerce_google_product_category.i18n.general_dropdown_placeholder}getOptions(e){return void 0===e||""===e?this.getTopLevelOptions():void 0===this.categories[e]||void 0===this.categories[e].options?[]:this.categories[e].options}getTopLevelOptions(){let e={};return Object.keys(this.categories).forEach(t=>{this.categories[t].parent||(e[t]=this.categories[t].label)}),e}getSelectedCategoryIds(e){var t=[];do{void 0!==this.categories[e]&&(t.push([e,this.categories[e].parent]),e=this.categories[e].parent)}while(""!==e);return t.reverse()}}});