!function(e,o,n,t){e(function(){var i=e(".wc-shipping-zone-methods"),s=e(".wc-shipping-zone-method-rows"),d=e(".wc-shipping-zone-method-save"),a=n.template("wc-shipping-zone-method-row"),c=n.template("wc-shipping-zone-method-row-blank"),r=Backbone.Model.extend({changes:{},logChanges:function(e){var o=this.changes||{};_.each(e.methods,function(e,n){o.methods=o.methods||{methods:{}},o.methods[n]=_.extend(o.methods[n]||{instance_id:n},e)}),"undefined"!=typeof e.zone_name&&(o.zone_name=e.zone_name),"undefined"!=typeof e.zone_locations&&(o.zone_locations=e.zone_locations),"undefined"!=typeof e.zone_postcodes&&(o.zone_postcodes=e.zone_postcodes),this.changes=o,this.trigger("change:methods")},save:function(){var n=_.clone(this.changes);_.has(n,"zone_locations")&&_.isEmpty(n.zone_locations)&&(n.zone_locations=[""]),e.post(t+(t.indexOf("?")>0?"&":"?")+"action=woocommerce_shipping_zone_methods_save_changes",{wc_shipping_zones_nonce:o.wc_shipping_zones_nonce,changes:n,zone_id:o.zone_id},this.onSaveResponse,"json")},onSaveResponse:function(e,n){"success"===n&&(e.success?(e.data.zone_id!==o.zone_id&&(o.zone_id=e.data.zone_id,window.history.pushState&&window.history.pushState({},"","admin.php?page=wc-settings&tab=shipping&zone_id="+e.data.zone_id)),l.set("methods",e.data.methods),l.trigger("change:methods"),l.changes={},l.trigger("saved:methods"),window.onbeforeunload=null):window.alert(o.strings.save_failed))}}),h=Backbone.View.extend({rowTemplate:a,initialize:function(){this.listenTo(this.model,"change:methods",this.setUnloadConfirmation),this.listenTo(this.model,"saved:methods",this.clearUnloadConfirmation),this.listenTo(this.model,"saved:methods",this.render),this.listenTo(this.model,"rerender",this.render),s.on("change",{view:this},this.updateModelOnChange),s.on("sortupdate",{view:this},this.updateModelOnSort),e(window).on("beforeunload",{view:this},this.unloadConfirmation),d.on("click",{view:this},this.onSubmit),e(document.body).on("input change","#zone_name, #zone_locations, #zone_postcodes",{view:this},this.onUpdateZone),e(document.body).on("click",".wc-shipping-zone-method-settings",{view:this},this.onConfigureShippingMethod),e(document.body).on("click",".wc-shipping-zone-add-method",{view:this},this.onAddShippingMethod),e(document.body).on("wc_backbone_modal_response",this.onConfigureShippingMethodSubmitted),e(document.body).on("wc_region_picker_update",this.onUpdateZoneRegionPicker),e(document.body).on("wc_backbone_modal_next_response",this.onAddShippingMethodSubmitted),e(document.body).on("wc_backbone_modal_before_remove",this.onCloseConfigureShippingMethod),e(document.body).on("change",".wc-shipping-zone-method-selector select",this.onChangeShippingMethodSelector),e(document.body).on("click",".wc-shipping-zone-postcodes-toggle",this.onTogglePostcodes),e(document.body).on("wc_backbone_modal_validation",{view:this},this.validateFormArguments),e(document.body).on("wc_backbone_modal_loaded",{view:this},this.onModalLoaded)},onUpdateZoneRegionPicker:function(e){var o=e.detail,n={};n.zone_locations=o,p.model.set("zone_locations",o),p.model.logChanges(n)},onUpdateZone:function(o){var n=o.data.view,t=n.model,i=e(this).val(),s=e(o.target).data("attribute"),d={};o.preventDefault(),d[s]=i,t.set(s,i),t.logChanges(d),n.render()},block:function(){e(this.el).block({message:null,overlayCSS:{background:"#fff",opacity:.6}})},unblock:function(){e(this.el).unblock()},render:function(){var n=_.indexBy(this.model.get("methods"),"instance_id"),t=this.model.get("zone_name"),i=this;e(".wc-shipping-zone-name").text(t||o.strings.default_zone_name),this.$el.empty(),this.unblock(),_.size(n)?(n=_.sortBy(n,function(e){return parseInt(e.method_order,10)}),e.each(n,function(e,n){"yes"===n.enabled?n.enabled_icon='<span class="woocommerce-input-toggle woocommerce-input-toggle--enabled">'+o.strings.yes+"</span>":n.enabled_icon='<span class="woocommerce-input-toggle woocommerce-input-toggle--disabled">'+o.strings.no+"</span>",i.$el.append(i.rowTemplate(n));var t=i.$el.find('tr[data-id="'+n.instance_id+'"]');if(!n.has_settings){t.find(".wc-shipping-zone-method-title > a").replaceWith("<span>"+t.find(".wc-shipping-zone-method-title > a").text()+"</span>");var s=t.find(".wc-shipping-zone-method-delete");t.find(".wc-shipping-zone-method-title .row-actions").empty().html(s)}}),this.$el.find(".wc-shipping-zone-method-delete").on("click",{view:this},this.onDeleteRow),this.$el.find(".wc-shipping-zone-method-enabled a").on("click",{view:this},this.onToggleEnabled)):i.$el.append(c),this.initTooltips()},initTooltips:function(){e("#tiptip_holder").removeAttr("style"),e("#tiptip_arrow").removeAttr("style"),e(".tips").tipTip({attribute:"data-tip",fadeIn:50,fadeOut:50,delay:50})},onSubmit:function(e){e.data.view.block(),e.data.view.model.save(),e.preventDefault()},onDeleteRow:function(n){var i=n.data.view,s=i.model,d=_.indexBy(s.get("methods"),"instance_id"),a={},c=e(this).closest("tr").data("id");n.preventDefault(),window.confirm(o.strings.delete_shipping_method_confirmation)&&(p.block(),e.post({url:t+(t.indexOf("?")>0?"&":"?")+"action=woocommerce_shipping_zone_remove_method",data:{wc_shipping_zones_nonce:o.wc_shipping_zones_nonce,instance_id:c,zone_id:o.zone_id},success:function({data:e}){delete d[c],a.methods=a.methods||e.methods,s.set("methods",d),s.logChanges(a),i.clearUnloadConfirmation(),i.render(),p.unblock()},error:function(e,n,t){window.alert(o.strings.remove_method_failed),p.unblock()},dataType:"json"}))},onToggleEnabled:function(o){var n=o.data.view,t=e(o.target),i=n.model,s=_.indexBy(i.get("methods"),"instance_id"),d=t.closest("tr").data("id"),a="yes"===t.closest("tr").data("enabled")?"no":"yes",c={};o.preventDefault(),s[d].enabled=a,c.methods=c.methods||{methods:{}},c.methods[d]=_.extend(c.methods[d]||{},{enabled:a}),i.set("methods",s),i.logChanges(c),n.render()},setUnloadConfirmation:function(){this.needsUnloadConfirm=!0,d.prop("disabled",!1)},clearUnloadConfirmation:function(){this.needsUnloadConfirm=!1,d.attr("disabled","disabled")},unloadConfirmation:function(e){if(e.data.view.needsUnloadConfirm)return e.returnValue=o.strings.unload_confirmation_msg,window.event.returnValue=o.strings.unload_confirmation_msg,o.strings.unload_confirmation_msg},updateModelOnChange:function(o){var n=o.data.view.model,t=e(o.target),i=t.closest("tr").data("id"),s=t.data("attribute"),d=t.val(),a=_.indexBy(n.get("methods"),"instance_id"),c={};a[i][s]!==d&&(c.methods[i]={},c.methods[i][s]=d,a[i][s]=d),n.logChanges(c)},updateModelOnSort:function(e){var o=e.data.view.model,n=_.indexBy(o.get("methods"),"instance_id"),t={};_.each(n,function(e){var o=parseInt(e.method_order,10),s=parseInt(i.find('tr[data-id="'+e.instance_id+'"]').index()+1,10);o!==s&&(n[e.instance_id].method_order=s,t.methods=t.methods||{methods:{}},t.methods[e.instance_id]=_.extend(t.methods[e.instance_id]||{},{method_order:s}))}),_.size(t)&&o.logChanges(t)},onConfigureShippingMethod:function(o){var n=e(this).closest("tr").data("id"),t=o.data.view.model,i=_.indexBy(t.get("methods"),"instance_id")[n];if(!i.settings_html)return!0;o.preventDefault(),i.settings_html=p.reformatSettingsHTML(i.settings_html),e(this).WCBackboneModal({template:"wc-modal-shipping-method-settings",variable:{instance_id:n,method:i,status:"existing"},data:{instance_id:n,method:i,status:"existing"}}),e(document.body).trigger("init_tooltips")},onConfigureShippingMethodSubmitted:function(n,i,s){"wc-modal-shipping-method-settings"===i&&(p.block(),e.post(t+(t.indexOf("?")>0?"&":"?")+"action=woocommerce_shipping_zone_methods_save_settings",{wc_shipping_zones_nonce:o.wc_shipping_zones_nonce,instance_id:s.instance_id,data:s},function(n,t){"success"===t&&n.success?(e("table.wc-shipping-zone-methods").parent().find("#woocommerce_errors").remove(),n.data.errors.length>0&&p.showErrors(n.data.errors),_.size(p.model.changes)?p.model.save():p.model.onSaveResponse(n,t)):(window.alert(o.strings.save_failed),p.unblock())},"json"))},showErrors:function(o){var n='<div id="woocommerce_errors" class="error notice is-dismissible">';e(o).each(function(e,o){n=n+"<p>"+o+"</p>"}),n+="</div>",e("table.wc-shipping-zone-methods").before(n)},onAddShippingMethod:function(n){n.preventDefault(),e(this).WCBackboneModal({template:"wc-modal-add-shipping-method",variable:{zone_id:o.zone_id}}),e(".wc-shipping-zone-method-selector select").trigger("change")},reformatSettingsHTML:function(e){return[this.replaceHTMLTables,this.moveAdvancedCostsHelpTip,this.moveHTMLHelpTips,this.addCurrencySymbol].reduce((e,o)=>o(e),e)},moveAdvancedCostsHelpTip:function(o){const n=e(o),t=n.find("#wc-shipping-advanced-costs-help-text");t.addClass("wc-shipping-zone-method-fields-help-text");const i=n.find("#woocommerce_flat_rate_cost").closest("fieldset");return t.appendTo(i),n.prop("outerHTML")},addCurrencySymbol:function(o){if(!window.wc.ShippingCurrencyContext||!window.wc.ShippingCurrencyNumberFormat)return o;const n=e(o),t=n.find(".wc-shipping-modal-price"),i=window.wc.ShippingCurrencyContext.getCurrencyConfig(),{symbol:s,symbolPosition:d}=i;return t.addClass(`wc-shipping-currency-size-${s.length}`),t.addClass(`wc-shipping-currency-position-${d}`),t.before(`<div class="wc-shipping-zone-method-currency wc-shipping-currency-position-${d}">${s}</div>`),t.each(o=>{const n=e(t[o]),s=n.attr("value"),d=window.wc.ShippingCurrencyNumberFormat(i,s);n.attr("value",d)}),n.prop("outerHTML")},moveHTMLHelpTips:function(o){const n=["woocommerce_flat_rate_cost","woocommerce_flat_rate_no_class_cost","woocommerce_flat_rate_class_cost_"],t=e(o),i=t.find("label");return i.each(o=>{const s=e(i[o]),d=s.find(".woocommerce-help-tip");if(0===d.length)return;const a=s.attr("for");if(n.some(e=>a.includes(e))){t.find(`label[for=${a}] span.woocommerce-help-tip`).addClass("wc-shipping-visible-help-text")}else{if("woocommerce_free_shipping_ignore_discounts"===a){t.find(`#${a}`).closest("fieldset").find("label").append(d)}else{const e=d.data("tip"),o=t.find(`#${a}`).closest("fieldset");o.length&&0===o.find(".wc-shipping-zone-method-fields-help-text").length&&o.append(`<div class="wc-shipping-zone-method-fields-help-text">${e}</div>`)}"Coupons discounts"===s.text().trim()&&s.text("")}}),t.prop("outerHTML")},replaceHTMLTables:function(o){const n=e(o),t=n.find("table.form-table");return t.each(o=>{const n=e(t[o]),i=e('<div class="wc-shipping-zone-method-fields" />');i.html(n.html()),n.replaceWith(i)}),n.prop("outerHTML")},onAddShippingMethodSubmitted:function(n,i,s,d){"wc-modal-add-shipping-method"===i&&(p.block(),e("#btn-next").html('<img alt="processing" src="images/wpspin_light.gif" class="waiting" />'),e.post(t+(t.indexOf("?")>0?"&":"?")+"action=woocommerce_shipping_zone_add_method",{wc_shipping_zones_nonce:o.wc_shipping_zones_nonce,method_id:s.add_method_id,zone_id:o.zone_id},function(n,t){"success"===t&&n.success&&(n.data.zone_id!==o.zone_id&&(o.zone_id=n.data.zone_id,window.history.pushState&&window.history.pushState({},"","admin.php?page=wc-settings&tab=shipping&zone_id="+n.data.zone_id)),p.model.set("methods",n.data.methods),d());var i=n.data.instance_id,s=n.data.methods[i];p.unblock(),s.settings_html?(s.settings_html=p.reformatSettingsHTML(s.settings_html),e(this).WCBackboneModal({template:"wc-modal-shipping-method-settings",variable:{instance_id:i,method:s,status:"new"},data:{instance_id:i,method:s,status:"new"}})):(p.model.trigger("change:methods"),p.model.trigger("saved:methods")),e(document.body).trigger("init_tooltips")},"json"))},possiblyHideFreeShippingRequirements:function(o){if(Object.keys(o).includes("woocommerce_free_shipping_requires")){const n=null===o.woocommerce_free_shipping_requires||""===o.woocommerce_free_shipping_requires||"coupon"===o.woocommerce_free_shipping_requires,t=e("#woocommerce_free_shipping_requires").closest("fieldset"),i=t.nextAll("label"),s=t.nextAll("fieldset");i.each(o=>{e(i[o]).css("display",n?"none":"block")}),s.each(o=>{e(s[o]).css("display",n?"none":"block")})}},onModalLoaded:function(o,n){if("wc-modal-shipping-method-settings"===n){const n=e("#woocommerce_free_shipping_requires");n.length>0&&o.data.view.possiblyHideFreeShippingRequirements({woocommerce_free_shipping_requires:n.val()}),o.data.view.possiblyAddShippingClassLink(o)}},possiblyAddShippingClassLink:function(o){const n=e("article.wc-modal-shipping-method-settings"),t=n.data("shipping-classes-count"),i=(n.data("status"),n.data("id")),s=o.data.view.model;if("flat_rate"===_.indexBy(s.get("methods"),"instance_id")[i].id&&0===t){n.find(".wc-shipping-method-add-class-costs").css("display","block")}},validateFormArguments:function(e,o,n){if("wc-modal-add-shipping-method"===o){if(n.add_method_id){const e=document.getElementById("btn-next");e.disabled=!1,e.classList.remove("disabled")}}else"wc-modal-shipping-method-settings"===o&&e.data.view.possiblyHideFreeShippingRequirements(n)},onCloseConfigureShippingMethod:function(n,i,s,d){if("wc-modal-shipping-method-settings"===i){var a=e("#btn-ok").data();if(!d&&a&&"new"===a.status){p.block();var c=p,r=c.model,h=_.indexBy(r.get("methods"),"instance_id"),l={},m=s.instance_id;e.post({url:t+(t.indexOf("?")>0?"&":"?")+"action=woocommerce_shipping_zone_remove_method",data:{wc_shipping_zones_nonce:o.wc_shipping_zones_nonce,instance_id:m,zone_id:o.zone_id},success:function({data:e}){delete h[m],l.methods=l.methods||e.methods,r.set("methods",h),r.logChanges(l),c.clearUnloadConfirmation(),c.render(),p.unblock()},error:function(e,n,t){window.alert(o.strings.remove_method_failed),p.unblock()},dataType:"json"})}}},onChangeShippingMethodSelector:function(){var o=e(this).find("option:selected").data("description");e(this).parent().find(".wc-shipping-zone-method-description").remove(),e(this).after('<div class="wc-shipping-zone-method-description">'+o+"</div>")},onTogglePostcodes:function(o){o.preventDefault();var n=e(this).closest("tr");n.find(".wc-shipping-zone-postcodes").show(),n.find(".wc-shipping-zone-postcodes-toggle").hide()}}),l=new r({methods:o.methods,zone_name:o.zone_name}),p=new h({model:l,el:s});p.render(),s.sortable({items:"tr",cursor:"move",axis:"y",handle:"td.wc-shipping-zone-method-sort",scrollSensitivity:40})})}(jQuery,shippingZoneMethodsLocalizeScript,wp,ajaxurl);