jQuery(document).ready(e=>{const t=window.typenow||"",s=window.pagenow||"",__=wp.i18n.__;if("product"===t&&wc_square_admin_products.is_product_sync_enabled){if("product_page_product_importer"===s){const i='<div class="error"><p>'+__("Product syncing with square has been enabled. If you are trying to update product inventory, you should do it in Square. Your existing inventory data in WooCommerce will be overwritten with data from Square products.","woocommerce-square")+"</p></div>";e(".wc-progress-steps").after(i)}if("edit-product"===s&&e("#the-list").on("click",".editinline",t=>{const s=e(t.target).closest("tr").find("th.check-column input").val(),a={action:"wc_square_get_quick_edit_product_details",security:wc_square_admin_products.get_quick_edit_product_details_nonce,product_id:s};e.post(wc_square_admin_products.ajax_url,a,t=>{const a=e("tr#edit-"+s),r=a.find("select.square-synced"),i=a.find(".wc-square-sync-with-square-errors");if(!t.success&&t.data&&"missing_variation_sku"===t.data)return r.prop("checked",!1),r.prop("disabled",!0),void i.find(".missing_variation_sku").show();const o=a.find("input[name=_sku]"),c=a.find("select[name=_stock_status]"),n=a.find("input[name=_stock]"),d=a.find(".manage_stock_field .manage_stock"),_=a.find("input[name=_manage_stock]"),u='<span class="description"><a href="'+wc_square_admin_products.settings_url+'">'+wc_square_admin_products.i18n.synced_with_square+"</a></span>",p=t.data.edit_url,l=t.data.i18n,h=t.data.is_variable;r.val(t.data.is_synced_with_square),o.on("change keyup keypress",t=>{if(""!==e(t.target).val()||h)return r.prop("disabled",!1),r.trigger("change"),i.find(".missing_sku").hide();r.val("no").trigger("change"),r.prop("disabled",!0),i.find(".missing_sku").show()}).trigger("change"),r.on("change",t=>{wc_square_admin_products.is_inventory_sync_enabled&&("no"===e(t.target).val()?(_.off(),_.add(n).css({opacity:1}),d.find(".description").remove(),h?_.is(":checked")?(e(".stock_qty_field").show(),e(".backorder_field").show()):e(".stock_status_field").show():(n.prop("readonly",!1),c.off("mousedown keydown change"),c.css({opacity:1}))):(_.on("click",()=>!1),_.add(n).css({opacity:"0.5"}),d.append(u),wc_square_admin_products.is_woocommerce_sor&&p&&l&&d.append('<p class="description"><a href="'+p+'">'+l+"</a></p>"),h?(e(".stock_status_field").hide(),e(".stock_qty_field").hide(),e(".backorder_field").hide()):(n.prop("readonly",!0),c.on("mousedown keydown change",()=>!1),c.css({opacity:.5}))))}).trigger("change")})}),"product"===s){const o="#_"+wc_square_admin_products.synced_with_square_taxonomy,c=()=>wc_square_admin_products.variable_product_types.includes(e("#product-type").val()),n=()=>""!==e("#_sku").val().trim(),d=t=>!!t.length&&t.filter(t=>""!==e(t).val().trim()).length===t.length,_=t=>{const s=t.map(t=>e(t).val());return s.every(e=>s.indexOf(e)===s.lastIndexOf(e))},u=()=>{const t=e('.woocommerce_attribute_data input[name^="attribute_variation"]:checked');return c()&&t&&t.length>1},p=t=>{e(".wc-square-sync-with-square-error."+t).show(),e(o).prop("disabled",!0),e(o).prop("checked",!1)},l=(t,s=!0)=>{e(".wc-square-sync-with-square-error."+t).hide(),s&&e(o).prop("disabled",!1)},h=t=>{if(c()){e("#_sku").off("change keypress keyup"),l("missing_sku",!u());const s=e('input[id^="variable_sku"]');s.on("change keypress keyup",()=>{d(e.makeArray(s))&&_(e.makeArray(s))?l("missing_variation_sku",!u()):p("missing_variation_sku"),e(t).triggerHandler("change")}).triggerHandler("change")}else e('input[id^="variable_sku"]').off("change keypress keyup"),l("missing_variation_sku",!u()),e("#_sku").on("change keypress keyup",s=>{""===e(s.target).val().trim()?p("missing_sku"):l("missing_sku",!u()),e(t).trigger("change")}).trigger("change")},m=t=>{e("#variable_product_options").on("reload",()=>{n()&&e(t).prop("disabled",!1),e(t).trigger("change")}).trigger("reload")},f=()=>{h(o),e(o).trigger("change"),c()&&!e('input[id^="variable_sku"]').length&&p("missing_variation_sku")},w=t=>{const s=e(".wc-square-sync-with-square");wc_square_admin_products.supported_products_for_sync.includes(t)?s.show():s.hide()},y=e("#product-type").val();w(y),m(o);const g=e(".stock_fields"),v=g.find("#_stock"),k=e(".stock_status_field"),q=k.find('input[name="_stock_status"]'),b=e("._manage_stock_field"),x=b.find("#_manage_stock"),D=b.find(".description"),j=D.text(),S=e("#_manage_stock").is(":checked");function a(){const t=e("#product-type").val(),s=e("#_square_gift_card").prop("checked");let a=[e(".wc-square-sync-with-square"),e(".inventory_sold_individually"),e('label[for="_virtual"]'),e('label[for="_downloadable"]'),e("._tax_status_field").closest(".options_group")];"variable"===t&&(a=[...a,e(".variable_is_virtual").closest(".tips"),e(".variable_is_downloadable").closest(".tips")]),s?(e("#_virtual").prop("checked")||e("#_virtual").trigger("click").prop("checked",!0),e(".variable_is_virtual").each((t,s)=>{e(s).prop("checked")||e(s).trigger("click").prop("checked",!0)}),a.forEach(e=>e.hide())):(a.forEach(e=>e.show()),"variable"===t&&(e('label[for="_virtual"]').hide(),e('label[for="_downloadable"]').hide()))}e(o).on("change",t=>{if(!wc_square_admin_products.is_inventory_sync_enabled)return;const s=wc_square_admin_products.variable_product_types.includes(e("#product-type").val());let a;if(e(t.target).is(":checked")&&e("#_square_item_variation_id").length>0){a=!0;let t="";x.is(":checked")||s||(t=' (<a href="#" class="sync-stock-from-square" data-product-id="'+e("#post_ID").val()+'">'+wc_square_admin_products.i18n.sync_inventory+'</a>)<div class="sync-stock-spinner spinner" style="float:none;"></div>'),D.html('<a href="'+wc_square_admin_products.settings_url+'">'+wc_square_admin_products.i18n.synced_with_square+"</a>"+t),x.on("click",()=>!1),x.css({opacity:"0.5"}),q.css({opacity:.5}),q.on("mousedown keydown click",()=>!1),v.prop("readonly",!0),wc_square_admin_products.is_woocommerce_sor&&!s?(0===e("p._stock_field span.description").length&&v.after('<span class="description" style="display:block;clear:both;"><a href="#" id="fetch-stock-with-square">'+wc_square_admin_products.i18n.fetch_stock_with_square+'</a><div class="spinner" style="float:none;"></div></span>'),e("#fetch-stock-with-square").on("click",t=>{t.preventDefault();const s=e("p._stock_field span.description .spinner"),a={action:"wc_square_fetch_product_stock_with_square",security:wc_square_admin_products.fetch_product_stock_with_square_nonce,product_id:e("#post_ID").val()};s.css("visibility","visible"),e.post(wc_square_admin_products.ajax_url,a,t=>{if(t&&t.success){const s=t.data.quantity;t.data.manage_stock||(x.prop("checked",!1).trigger("change"),e("#_stock_status").val("instock"),D.html('<a href="'+wc_square_admin_products.settings_url+'">'+wc_square_admin_products.i18n.inventory_tracking_disabled+"</a>")),v.val(s),g.find("input[name=_original_stock]").val(s),v.prop("readonly",!1),e("p._stock_field span.description").remove()}else t.data&&(e(".inventory-fetch-error").remove(),s.after('<span class="inventory-fetch-error" style="display:inline-block;color:red;">'+t.data+"</span>")),s.css("visibility","hidden")})})):wc_square_admin_products.is_square_sor&&0===e("p._stock_field span.description").length&&v.after('<span class="description" style="display:block;clear:both;">'+wc_square_admin_products.i18n.managed_by_square+' (<a href="#" class="sync-stock-from-square" data-product-id="'+e("#post_ID").val()+'">'+wc_square_admin_products.i18n.sync_stock_from_square+'</a>)<div class="sync-stock-spinner spinner" style="float:none;"></div></span>')}else a=!1,e("p._stock_field span.description").remove(),e(".sync-stock-from-square").remove(),v.prop("readonly",!1),D.html(j),x.off("click"),x.css({opacity:1}),x.prop("checked",S),q.css({opacity:1}),q.off("mousedown keydown click"),s||(S?(g.show(),k.hide()):(k.show(),g.hide()));e(".woocommerce_variation").each((t,s)=>{const r=e(s).find("h3 input.variable_post_id").val(),i=e(s).find(".variable_manage_stock"),o=i.parent(),c=e(s).find(".wc_input_stock"),n=c.parent(),d=e(s).find(".variable_stock_status select");if(a){if(c.prop("readonly",!0),i.on("click",()=>!1),i.css({opacity:"0.5"}),d.css({opacity:.5}),d.on("mousedown keydown change",()=>!1),0===o.find(".description").length){let e="";i.is(":checked")||(e='<a href="#" class="sync-stock-from-square" data-product-id="'+r+'">'+wc_square_admin_products.i18n.sync_inventory+'</a><div class="sync-stock-spinner spinner" style="float:none;"></div>'),o.find(".sync-stock-from-square").length||i.after(`<span class="description">(${wc_square_admin_products.i18n.managed_by_square})${e?" - ":""}</span> ${e}`)}if(wc_square_admin_products.is_woocommerce_sor){const t="fetch-stock-with-square-"+r;0===n.find("span.description").length&&c.after('<span class="description" style="display:block;clear:both;"><a href="#" id="'+t+'">'+wc_square_admin_products.i18n.fetch_stock_with_square+'</a><div class="spinner" style="float:none;"></div></span>'),e("#"+t).on("click",t=>{t.preventDefault();const s=e(t.target).next(".spinner"),a={action:"wc_square_fetch_product_stock_with_square",security:wc_square_admin_products.fetch_product_stock_with_square_nonce,product_id:r};s.css("visibility","visible"),e.post(wc_square_admin_products.ajax_url,a,t=>{if(t&&t.success){const e=t.data.quantity;t.data.manage_stock||i.prop("checked",!1).trigger("change"),c.val(e),n.parent().find('input[name^="variable_original_stock"]').val(e),c.prop("readonly",!1),n.find(".description").remove()}else t.data&&(e(".inventory-fetch-error").remove(),s.after('<span class="inventory-fetch-error" style="display:inline-block;color:red;">'+t.data+"</span>")),s.css("visibility","hidden")})})}else wc_square_admin_products.is_square_sor&&0===n.find("span.description").length&&c.after('<span class="description" style="display:block;clear:both;"><a href="#" class="sync-stock-from-square" data-product-id="'+r+'">'+wc_square_admin_products.i18n.sync_stock_from_square+'</a><div class="sync-stock-spinner spinner" style="float:none;"></span>')}else c.prop("readonly",!1),i.off("click"),i.css({opacity:1}),i.next(".description").remove(),d.css({opacity:1}),d.off("mousedown keydown change")})}).trigger("change"),e("#product-type").on("change",function(t){w(e(t.target).val()),f()}),e("#woocommerce-product-data").on("click",".sync-stock-from-square",t=>{t.preventDefault();const s=e(t.target).data("product-id"),a=e(".sync-stock-spinner.spinner"),r={action:"wc_square_fetch_product_stock_with_square",security:wc_square_admin_products.fetch_product_stock_with_square_nonce,product_id:s};a.css("visibility","visible"),e.post(wc_square_admin_products.ajax_url,r,()=>{a.css("visibility","hidden"),window.location.reload()})}),a(),e("#product-type, #_square_gift_card").on("change",a),e("#woocommerce-product-data").on("click",".sync-stock-from-square",t=>{t.preventDefault();const s=e(t.target).data("product-id"),a=e(".sync-stock-spinner.spinner"),r={action:"wc_square_fetch_product_stock_with_square",security:wc_square_admin_products.fetch_product_stock_with_square_nonce,product_id:s};a.css("visibility","visible"),e.post(wc_square_admin_products.ajax_url,r,()=>{a.css("visibility","hidden"),window.location.reload()})});let I=null;function r(){let t=document.querySelector(".woocommerce_variations");!I&&t&&(I=new MutationObserver(()=>{e("#_square_gift_card").trigger("change")}),I&&I.observe(t,{attributes:!1,childList:!0,subtree:!1}))}r(),e("#woocommerce-product-data").on("woocommerce_variations_loaded woocommerce_variations_added woocommerce_variations_removed",()=>{r(),f()});const A=(t,s)=>{const a=e(t);a.addClass("wc-square-field-error-highlight");let r=e("#wc-square-validation-errors");r.length||(r=e('<div id="wc-square-validation-errors" class="wc-square-validation-errors"><h4>'+__("Square Sync Validation Errors","woocommerce-square")+"</h4><ul></ul></div>"),e("#woocommerce-product-data").prepend(r));const i=r.find("ul"),o="square-error-"+a.attr("id");e("#"+o).length?e("#"+o).text(s):i.append('<li id="'+o+'">'+s+"</li>")},C=e=>(e.attr("id")||e.attr("id","wc-square-field-"+Math.random().toString(36).substring(2,15)),e),E=t=>{t.removeClass("wc-square-field-error-highlight"),e("#square-error-"+t.attr("id")).remove(),e("#wc-square-validation-errors ul li").length||e("#wc-square-validation-errors").remove()},O=()=>{e(".wc-square-field-error-highlight").removeClass("wc-square-field-error-highlight"),e("#wc-square-validation-errors").remove()},$=(e,t,s,a)=>(s=C(s),e>t?(A(s,a),!1):(E(s),!0)),H=e=>{const t=e.val();return $(t.length,65,e,wc_square_admin_products.i18n.attribute_name_too_long+": "+t.substring(0,65)+"...")},M=()=>$(e("#product_attributes .woocommerce_attribute").length,6,e(".attribute_tab"),wc_square_admin_products.i18n.too_many_attributes),L=e=>{let t,s=e.val();return t=Array.isArray(s)?s.length:s.split("|").filter(e=>""!==e.trim()).length,$(t,250,e,wc_square_admin_products.i18n.too_many_attribute_values.replace("%d",t))},P=()=>$(e("#woocommerce-product-data").find(".woocommerce_variation").length,250,e(".variations_tab"),wc_square_admin_products.i18n.too_many_variations);e("#product-type").on("change",()=>{"variable"===e("#product-type").val()?Q():O()});const Q=()=>{if(!c())return;const t=e("#_"+wc_square_admin_products.synced_with_square_taxonomy),s=e("#product_attributes"),a=e("#woocommerce-product-data"),r=()=>{O(),t.prop("checked")&&c()&&(M(),P(),s.find("input.attribute_name").each(function(){H(e(this))}),s.find('[name*="attribute_values"]').each(function(){L(e(this))}))};s.on("change","input.attribute_name",function(){t.prop("checked")&&(H(e(this)),M())}).on("change",'[name*="attribute_values"]',function(){t.prop("checked")&&L(e(this))}).on("click",".product_attributes .delete",function(){t.prop("checked")&&M()}),a.on("click",".save-variation-changes, #variable_product_options button",P),a.on("woocommerce_variations_loaded woocommerce_variations_added woocommerce_variations_removed",r),t.on("change",r),r()};e(document).ready(Q)}}});