"use strict";function _typeof(t){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}var Virtuemart=window.Virtuemart||{};!function(u){var i={_cache:{},list:function(t){return void 0===Virtuemart.vmSiteurl&&(Virtuemart.vmSiteurl=""),this.each(function(){this.self=u(this),this.opt=this.opt||{},this.opt=u.extend(!0,{},u.fn.vm2front.defaults,this.opt,i._processOpt(t)),this.state_fields=u(this.form).find(this.opt.state_field_selector),i._update.call(this,!1),u(this).on("change",function(){i._update.call(this,this.opt.show_list_loader)})})},setOpt:function(t){return this.each(function(){this.opt=u.extend(!0,{},u.fn.vm2front.defaults,this.opt,i._processOpt(t))})},_processOpt:function(t){return t.hasOwnProperty("dest")&&(t.state_field_selector=t.dest,delete t.dest),t.hasOwnProperty("ids")&&(t.selected_state_ids=t.ids,delete t.ids),t.hasOwnProperty("prefiks")&&(t.field_prefix=t.prefiks,delete t.prefiks),t},_update:function(a){var e=this,r=u(this).val()||[];(r=u.isArray(r)?r:u.makeArray(r)).length&&(r=r.join(","),i._cache.hasOwnProperty(r)?i._addToList.call(e,i._cache[r]):u.ajax({dataType:"JSON",url:Virtuemart.vmSiteurl+"index.php?option=com_virtuemart&view=state&format=json&virtuemart_country_id="+r,beforeSend:function(){a&&i.startVmLoading()},success:function(t){t&&(i._cache[r]=t,i._addToList.call(e,i._cache[r])),a&&i.stopVmLoading()},error:function(t,e,r){console.log(t),console.log(e),console.log(r),a&&i.stopVmLoading()}}))},_addToList:function(r){var d=this,a=u.type(r),n=0,l=[];d.opt.selected_state_ids&&d.opt.selected_state_ids.length&&d.opt.selected_state_ids&&("string"===u.type(d.opt.selected_state_ids)&&(l=d.opt.selected_state_ids.split(",")),"number"===u.type(d.opt.selected_state_ids))&&l.push(d.opt.selected_state_ids),u(d.state_fields).each(function(){var s=this,t=u(this).attr("id"),e=u(d.form),e=t&&e.length?e.find('label[for="'+t+'"]'):null,t=u(s).data("required"),o=!1;u(s).data("label",e),(!0!==t&&!1!==t||u(s).attr("required")||u(s).hasClass("required"))&&(u(s).attr("required")||u(s).hasClass("required")?(u(s).data("required",!0).removeAttr("required").removeAttr("aria-required").removeClass("required"),e&&e.length&&d.opt.asterisk_class&&(e.find("."+d.opt.asterisk_class).hide(),e.find(".form-control-feedback").hide())):u(s).data("required",!1)),u("optgroup",s).each(function(){u(this).data("ajaxloaded")&&u(this).remove()}),"object"!==a&&"array"!==a||(o=!1,u.each(r,function(t,e){var r,a,i=u(d).find('option[value="'+t+'"]').text(),t=(d.opt.field_prefix&&d.opt.field_prefix+"-")+"group-"+n+"-"+t;u("#"+t,this).length||(r=u("<optgroup />",{id:t,label:i}).data("ajaxloaded",!0),u.each(e,function(t,e){a=u("<option />",{value:e.virtuemart_state_id}).text(e.state_name),0<=u.inArray(e.virtuemart_state_id.toString(),l)&&a.attr("selected",!0),r.append(a),o=!0})),r&&o&&u(s).append(r)})),o&&u(s).data("required")&&(u(s).attr("required",!0).attr("aria-required",!0),e=u(s).data("label"))&&u(e).length&&d.opt.asterisk_class&&(u(e).find("."+d.opt.asterisk_class).show(),e.find(".form-control-feedback").show()),!u(s).hasClass("invalid")&&"true"!=u(s).attr("aria-invalid")||u(s).trigger("blur"),d.opt.field_update_trigger&&"string"===u.type(d.opt.field_update_trigger)&&u(s).trigger(d.opt.field_update_trigger),n++})},startVmLoading:function(t){Virtuemart.startVmLoading({data:{msg:t||""}})},stopVmLoading:function(){Virtuemart.stopVmLoading()}};u.fn.vm2front=function(t){return i[t]?i[t].apply(this,Array.prototype.slice.call(arguments,1)):"object"!==_typeof(t)&&t?void u.error("Method "+t+" does not exist in Vm2front plugin."):i.init.apply(this,arguments)},u.fn.vm2front.defaults={state_field_selector:"#virtuemart_state_id_field",selected_state_ids:"",field_prefix:"",field_update_trigger:"liszt:updated",show_list_loader:!0,asterisk_class:"asterisk"},Virtuemart.startVmLoading=function(t){var e="";void 0!==t&&void 0!==t.data&&void 0!==t.data.msg&&(e=t.data.msg),u(".vm-loader").length||u("<div></div>").addClass("vm-loader").attr("aria-hidden","true").attr("tabindex","-1").appendTo(u("body")),u(".vm-loader-spinner").length||u("<div></div>").addClass("vm-loader-spinner").attr("aria-hidden","true").attr("tabindex","-1").appendTo(u("body")),e&&(u(".vm-loader-message").length||u("<div></div>").addClass("vm-loader-message").appendTo(u("body")),u(".vm-loader-message").html(e)),u("body").addClass("vm-loading")},Virtuemart.stopVmLoading=function(){u("body").removeClass("vm-loading"),u(".vm-loader-message").remove()},Virtuemart.sendCurrForm=function(t){t.preventDefault(),0<t.currentTarget.length&&u(t.currentTarget[0]).is("form")?u(t.currentTarget[0].form.submit()):u(t.currentTarget).closest("form").submit(),u(t.currentTarget).closest("form").find("button").click()}}(jQuery);