!function(t,e,a,n){t(function(){String.prototype.trim||(String.prototype.trim=function(){return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,"")});var i=a.template("wc-tax-table-row"),r=a.template("wc-tax-table-row-empty"),o=a.template("wc-tax-table-pagination"),s=t(".wc_tax_rates"),c=t("#rates"),d=t(':input[name="save"]'),l=t("#rates-pagination, #rates-bottom-pagination"),u=t("#rates-search .wc-tax-rates-search-field"),h=t(".submit .button-primary[type=submit]"),p=Backbone.Model.extend({changes:{},setRateAttribute:function(t,e,a){var n=_.indexBy(this.get("rates"),"tax_rate_id"),i={};n[t][e]!==a&&(i[t]={},i[t][e]=a,n[t][e]=a),this.logChanges(i)},logChanges:function(t){var e=this.changes||{};_.each(t,function(t,a){e[a]=_.extend(e[a]||{tax_rate_id:a},t)}),this.changes=e,this.trigger("change:rates")},getFilteredRates:function(){var t=this.get("rates"),e=u.val().toLowerCase();return e.length&&(t=_.filter(t,function(t){return-1!==_.toArray(t).join(" ").toLowerCase().indexOf(e)})),t=_.sortBy(t,function(t){return parseInt(t.tax_rate_order,10)})},block:function(){t(".wc_tax_rates").block({message:null,overlayCSS:{background:"#fff",opacity:.6}})},unblock:function(){t(".wc_tax_rates").unblock()},save:function(){var t=this;t.block(),Backbone.ajax({method:"POST",dataType:"json",url:n+(n.indexOf("?")>0?"&":"?")+"action=woocommerce_tax_rates_save_changes",data:{current_class:e.current_class,wc_tax_nonce:e.wc_tax_nonce,changes:t.changes},success:function(e,a){"success"===a&&e.success&&(f.set("rates",e.data.rates),f.trigger("change:rates"),f.changes={},f.trigger("saved:rates"),m.render()),t.unblock()}})}}),g=Backbone.View.extend({rowTemplate:i,per_page:e.limit,page:e.page,initialize:function(){var a=Math.ceil(_.toArray(this.model.get("rates")).length/this.per_page);this.qty_pages=0===a?1:a,this.page=this.sanitizePage(e.page),this.listenTo(this.model,"change:rates",this.setUnloadConfirmation),this.listenTo(this.model,"saved:rates",this.clearUnloadConfirmation),c.on("change autocompletechange",":input",{view:this},this.updateModelOnChange),u.on("keyup search",{view:this},this.onSearchField),l.on("click","a",{view:this},this.onPageChange),l.on("change","input",{view:this},this.onPageChange),t(window).on("beforeunload",{view:this},this.unloadConfirmation),h.on("click",{view:this},this.onSubmit),d.prop("disabled",!0),s.find(".insert").on("click",{view:this},this.onAddNewRow),s.find(".remove_tax_rates").on("click",{view:this},this.onDeleteRow),s.find(".export").on("click",{view:this},this.onExport)},render:function(){var a=this.model.getFilteredRates(),n=_.size(a),i=Math.ceil(n/this.per_page),s=0===n?0:this.per_page*(this.page-1),c=this.per_page*this.page,d=_.toArray(a).slice(s,c),u=this;this.$el.empty(),d.length?t.each(d,function(t,e){u.$el.append(u.rowTemplate(e))}):u.$el.append(r()),this.$el.find("td.country input").autocomplete({source:e.countries,minLength:2}),this.$el.find("td.state input").autocomplete({source:e.states,minLength:3}),this.$el.find("td.postcode input, td.city input").on("change",function(){t(this).attr("name",t(this).data("name"))}),i>1?l.html(o({qty_rates:n,current_page:this.page,qty_pages:i})):(l.empty(),u.page=1)},updateUrl:function(){if(window.history.replaceState){var t=e.base_url,a=u.val();1<this.page&&(t+="&p="+encodeURIComponent(this.page)),a.length&&(t+="&s="+encodeURIComponent(a)),window.history.replaceState({},"",t)}},onSubmit:function(t){t.data.view.model.save(),t.preventDefault()},onAddNewRow:function(t){var a,n,i,r,o=t.data.view,s=o.model,d=_.indexBy(s.get("rates"),"tax_rate_id"),l={},u=_.size(d),h=_.extend({},e.default_rate,{tax_rate_id:"new-"+u+"-"+Date.now(),newRow:!0});(a=c.children(".current")).length?(n=a.last().data("id"),i=parseInt(d[n].tax_rate_order,10),h.tax_rate_order=1+i,r=_.filter(d,function(t){return parseInt(t.tax_rate_order,10)>i}),_.map(r,function(t){return t.tax_rate_order++,l[t.tax_rate_id]=_.extend(l[t.tax_rate_id]||{},{tax_rate_order:t.tax_rate_order}),t})):(h.tax_rate_order=1+_.max(_.pluck(d,"tax_rate_order"),function(t){return parseInt(t,10)}),o.page=o.qty_pages),d[h.tax_rate_id]=h,l[h.tax_rate_id]=h,s.set("rates",d),s.logChanges(l),o.render()},onDeleteRow:function(a){var n,i,r=a.data.view,o=r.model,s=_.indexBy(o.get("rates"),"tax_rate_id"),d={};a.preventDefault(),(n=c.children(".current"))?(n.each(function(){i=t(this).data("id"),delete s[i],d[i]=_.extend(d[i]||{},{deleted:"deleted"})}),o.set("rates",s),o.logChanges(d),r.render()):window.alert(e.strings.no_rows_selected)},onSearchField:function(t){t.data.view.updateUrl(),t.data.view.render()},onPageChange:function(e){var a=t(e.currentTarget);e.preventDefault(),e.data.view.page=a.data("goto")?a.data("goto"):a.val(),e.data.view.render(),e.data.view.updateUrl()},onExport:function(a){var n="data:application/csv;charset=utf-8,"+e.strings.csv_data_cols.join(",")+"\n";return t.each(a.data.view.model.getFilteredRates(),function(t,a){var i="";i+=a.tax_rate_country+",",i+=a.tax_rate_state+",",i+=(a.postcode?a.postcode.join("; "):"")+",",i+=(a.city?a.city.join("; "):"")+",",i+=a.tax_rate+",",i+=a.tax_rate_name+",",i+=a.tax_rate_priority+",",i+=a.tax_rate_compound+",",i+=a.tax_rate_shipping+",",i+=e.current_class,n+=i+"\n"}),t(this).attr("href",encodeURI(n)),!0},setUnloadConfirmation:function(){this.needsUnloadConfirm=!0,d.prop("disabled",!1)},clearUnloadConfirmation:function(){this.needsUnloadConfirm=!1,d.prop("disabled",!0)},unloadConfirmation:function(t){if(t.data.view.needsUnloadConfirm)return t.returnValue=e.strings.unload_confirmation_msg,window.event.returnValue=e.strings.unload_confirmation_msg,e.strings.unload_confirmation_msg},updateModelOnChange:function(e){var a=e.data.view.model,n=t(e.target),i=n.closest("tr").data("id"),r=n.data("attribute"),o=n.val();"city"!==r&&"postcode"!==r||(o=o.split(";"),o=t.map(o,function(t){return t.trim()})),"tax_rate_compound"!==r&&"tax_rate_shipping"!==r||(o=n.is(":checked")?1:0),a.setRateAttribute(i,r,o)},sanitizePage:function(t){return(t=parseInt(t,10))<1?t=1:t>this.qty_pages&&(t=this.qty_pages),t}}),f=new p({rates:e.rates}),m=new g({model:f,el:"#rates"});m.render()})}(jQuery,htmlSettingsTaxLocalizeScript,wp,ajaxurl);