jQuery(document).ready(function(o){function e(e){o(".product-sync-field").each(function(){let c=o(this);o(this).hasClass("wc-enhanced-select")&&(c=o(this).next("span.select2-container")),e?c.css("pointer-events","all").css("opacity","1.0"):c.css("pointer-events","none").css("opacity","0.4")})}o(".woocommerce-help-tip").tipTip({attribute:"data-tip",fadeIn:50,fadeOut:50,delay:200}),o("form.wc-facebook-settings").hasClass("disconnected")&&e(!1),o("input#wc_facebook_enable_product_sync").on("change",function(c){o("form.wc-facebook-settings").hasClass("disconnected")?o(this).css("pointer-events","none").css("opacity","0.4"):e(o(this).is(":checked"))}).trigger("change");let c=!1;function n(o=null){s(o),window.syncStatusInterval||(window.syncStatusInterval=setInterval(_,1e4))}function s(c=null){e(!1),o('input#wc_facebook_enable_product_sync, input[name="save_product_sync_settings"]').css("pointer-events","none").css("opacity","0.4");let n=facebook_for_woocommerce_settings_sync.i18n.sync_in_progress;c&&(n+=c>1?facebook_for_woocommerce_settings_sync.i18n.sync_remaining_items_plural:facebook_for_woocommerce_settings_sync.i18n.sync_remaining_items_singular,n=n.replace("{count}",c)),o("#sync_progress").show().html(n).css("color","inherit"),facebook_for_woocommerce_settings_sync.sync_in_progress=!0}function t(c=""){facebook_for_woocommerce_settings_sync.sync_in_progress=!1,clearInterval(window.syncStatusInterval),window.syncStatusInterval=null,e(!0),o('input#wc_facebook_enable_product_sync, input[name="save_product_sync_settings"]').css("pointer-events","all").css("opacity","1"),c?o("#sync_progress").show().html(c).css("color","#DC3232"):o("#sync_progress").hide()}function _(){facebook_for_woocommerce_settings_sync.sync_in_progress&&o.post(facebook_for_woocommerce_settings_sync.ajax_url,{action:"wc_facebook_get_sync_status",nonce:facebook_for_woocommerce_settings_sync.sync_status_nonce},function(o){console.log(o),o.success&&(o.data>0?n(o.data):t())})}o('input[name="save_product_sync_settings"]').on("click",function(e){if(c)return!0;e.preventDefault();const n=o(this),s=function(){const e=o("#wc_facebook_excluded_product_category_ids").val();let c=[];return window.facebook_for_woocommerce_settings_sync&&window.facebook_for_woocommerce_settings_sync.excluded_category_ids&&(c=window.facebook_for_woocommerce_settings_sync.excluded_category_ids),o(e).not(c).get()}(),t=function(){const e=o("#wc_facebook_excluded_product_tag_ids").val();let c=[];return window.facebook_for_woocommerce_settings_sync&&window.facebook_for_woocommerce_settings_sync.excluded_tag_ids&&(c=window.facebook_for_woocommerce_settings_sync.excluded_tag_ids),o(e).not(c).get()}();s.length>0||t.length>0?o.post(facebook_for_woocommerce_settings_sync.ajax_url,{action:"facebook_for_woocommerce_set_excluded_terms_prompt",security:facebook_for_woocommerce_settings_sync.set_excluded_terms_prompt_nonce,categories:s,tags:t},function(e){e&&!e.success?(o("#wc-backbone-modal-dialog .modal-close").trigger("click"),new o.WCBackboneModal.View({target:"facebook-for-woocommerce-modal",string:e.data}),o(".facebook-for-woocommerce-confirm-settings-change").on("click",function(){blockModal(),c=!0,n.trigger("click")})):(c=!0,n.trigger("click"))}):(c=!0,n.trigger("click"))}),facebook_for_woocommerce_settings_sync.sync_in_progress&&n(),o("#woocommerce-facebook-settings-sync-products").click(function(e){if(e.preventDefault(),confirm(facebook_for_woocommerce_settings_sync.i18n.confirm_sync)){s();let e=Date.now();o.post(facebook_for_woocommerce_settings_sync.ajax_url,{action:"wc_facebook_sync_products",nonce:facebook_for_woocommerce_settings_sync.sync_products_nonce},function(o){if(o.success)setTimeout(_,Math.max(0,1e4-(Date.now()-e)));else{let e=facebook_for_woocommerce_settings_sync.i18n.general_error;o.data&&o.data.length>0&&(e=o.data),t(e)}}).fail(function(){t(facebook_for_woocommerce_settings_sync.i18n.general_error)})}})});