function CMSForms(e){let t,i,r,n,a,o=this;this.create=function(){t=$("#plugin-forms-"+e),i=t.parent().find(".plugin-form-loader"),n=void 0!==window["formDropzones_"+e],a=void 0!==window["formMaps_"+e],o.bind()},this.bind=function(){n&&o.bindDropzone(),a&&o.bindMaps(),t.find(".plugin-form-submit").on("click",(function(e){e.preventDefault(),i.show(),o.submitForm()})),$("#confirmationSmsButton").on("click",(function(e){e.preventDefault(),i.show(),o.confirmSms()})),$(document).ready((function(){$("#plugin-forms-"+e+" .info").tooltip(),$("textarea.g-recaptcha-response").attr("aria-label","Google recaptcha")}))},this.bindDropzone=function(){let t=JSON.parse(window["formDropzones_"+e]),i=parseInt(window["uploadFileMaxSize_"+e]),n=parseInt(window["uploadFileAllMaxSize_"+e]),a=window["dropzoneTranslation_"+e],s=0;r=new FormData;for(let l in t){let d=$("#file-field-"+l),c=t[l],f=$("#"+l),p=d.parent();r.append(l,""),d.dropzone({url:URL_CLIENT_REQUEST,acceptedFiles:c.allowedExtensionsMimes.join(),addRemoveLinks:!0,maxFilesize:i/1024/1024,maxFiles:c.isMultiple?null:1,uploadMultiple:!1,dictCancelUpload:a.dictCancelUpload,dictCancelUploadConfirmation:a.dictCancelUploadConfirmation,dictDefaultMessage:f.attr("placeholder")?f.attr("placeholder"):a.dictDefaultMessage,dictFallbackMessage:a.dictFallbackMessage,dictFallbackText:a.dictFallbackText,dictFileTooBig:a.dictFileTooBig,dictInvalidFileType:a.dictInvalidFileType.replace("{{fileExt}}",c.allowedExtensions),dictMaxFilesExceeded:a.dictMaxFilesExceeded,dictRemoveFile:a.dictRemoveFile,dictResponseError:a.dictResponseError,dictUploadCanceled:a.dictUploadCanceled,hiddenInputContainer:p[0],renameFilename:function(e){return e.replace(/[<>:"/\\\|?*]|[\x00-\x1F]|[\x7F\xA0\xAD]|[#\[\]@!$&\'()+,;=]|[{}^\~`]/g,"")},accept:function(e,t){s>=n?(e.status=Dropzone.CANCELED,this._errorProcessing([e],a.dictMaxFilesExceeded,null)):t()},init:function(){dzClosure=this,p.find(".dz-hidden-input").attr("aria-label","Dropzone field"),d.keyup((function(e){13===e.which&&this.click()})),this.on("addedfile",(function(e){s+=e.size})),this.on("removedfile",(function(t){if(0!==t.upload.progress&&(s-=t.size),""!==r.get(l)){let i=JSON.parse(r.get(l)),n=i.find((function(e){return e.uuid===t.upload.uuid&&e}));n&&($.ajax({type:"POST",url:URL_CLIENT_REQUEST,data:{Action:"REMOVE_FILE",PluginName:"forms",formId:e,fieldSlug:l,fileName:n.name,csrf_token:getCsrfTokenJs()},success:function(){},error:ajaxErrorResponse}),i.splice(i.findIndex((function(e,i){return e.uuid===t.upload.uuid&&i})),1),r.set(l,JSON.stringify(i)))}})),this.on("error",(function(e){s-=e.size,void 0!==e.xhr&&void 0!==e.xhr.status&&e.xhr.status&&419===e.xhr.status&&ajaxErrorResponse(e.xhr)})),this.on("sending",(function(t,i,r){r.append("Action","UPLOAD_FILE"),r.append("PluginName","forms"),r.append("fieldSlug",l),r.append("formId",e),r.append("csrf_token",getCsrfTokenJs())})),this.on("success",(function(e,t){let i=JSON.parse(t),n=""===r.get(l)?[]:JSON.parse(r.get(l));if("success"===i.status){const t=i.file_data[l].file_size;if(t){s-=e.size,s+=t;const i=e.previewElement.querySelector("[data-dz-size]");i&&(i.innerHTML=o.formatBytes(t)),Object.defineProperty(e,"size",{value:t})}n.push({uuid:e.upload.uuid,name:i.file_data[l].temp_file_name}),r.set(l,JSON.stringify(n))}else e.status=Dropzone.CANCELED,s+=e.size,this._errorProcessing([e],i.errors[l],null);p.find(".dz-hidden-input").attr("aria-label","Dropzone field"),d.find(".dz-file-preview .dz-image > img").remove()}))}})}},this.formatBytes=function(e){if(0===e)return"0 B";const t=Math.floor(Math.log(e)/Math.log(1024));return""+parseFloat((e/Math.pow(1024,t)).toFixed(2))+" "+["B","KB","MB","GB","TB"][t]},this.bindMaps=function(){let t=JSON.parse(window["formMaps_"+e]);for(let r in t){let n=t[r],a=L.map(n.map_id,{center:[n.latitude,n.longitude],minZoom:2,zoom:n.zoom});function i(){let t=r,i=$("#geolocation-"+t).val(),n=$("#geolocate-error-"+t);n.html("");let s=window["localizationTranslation_"+e];i.length>2?$.getJSON("https://nominatim.openstreetmap.org/search?format=json&limit=1&q="+i,(function(e){e.length?$.each(e,(function(e,i){let r={latlng:[i.lat,i.lon]};o.addMarker(r,a,t)})):(o.removeMarker(a,t),n.html('"))})):(o.removeMarker(a,t),n.html('"))}L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",{attribution:"OSM"}).addTo(a),a.on("click",(function(e){o.addMarker(e,a,r)})),$("#geolocate-"+r).on("click",(function(e){e.preventDefault(),i()})),$("#geolocation-"+r).on("keypress",(function(e){13===e.which&&(e.preventDefault(),i())}))}},this.addMarker=function(e,t,i){o.removeMarker(t,i);let r=L.marker(e.latlng,{draggable:!0,icon:L.icon({iconUrl:"/cms/assets/plugins/leaflet/image/marker-icon.png",iconSize:[25,40],iconAnchor:[16,32],popupAnchor:[0,-32]})}).addTo(t).on("dragend",(function(){$("#"+i).val(JSON.stringify({latitude:r.getLatLng().lat,longitude:r.getLatLng().lng})),t.panTo([r.getLatLng().lat,r.getLatLng().lng])}));$("#"+i).val(JSON.stringify({latitude:r.getLatLng().lat,longitude:r.getLatLng().lng})),t.panTo(e.latlng),t.flyTo(e.latlng,13)},this.removeMarker=function(e,t){e.eachLayer((function(t){t instanceof L.Marker&&e.removeLayer(t)})),$("#"+t).val("")},this.submitForm=function(){const s=t.find("[name=recaptcha_key]").val();let l=new FormData(t[0]);n&&r.forEach((function(e,t){let i=[];if(""!==e){JSON.parse(e).forEach((function(e,t){i.push(e.name)}))}l.set(t,JSON.stringify(i))})),grecaptcha.ready((function(){try{grecaptcha.execute(s,{action:"submit"}).then((function(s){t.find("[name=recaptcha_token]").val(s),l.append("csrf_token",getCsrfTokenJs()),$.ajax({type:"POST",url:URL_CLIENT_REQUEST+"?Action=SUBMIT_PUBLIC_FORM&PluginName=forms",data:l,contentType:!1,processData:!1,success:function(s){let l=JSON.parse(s);if(o.clearMessages(t),"success"===l.status){if(o.showSuccessMessage(t,l.message),t[0].reset(),t.find(".showConfirmationUserContainer").hide(),l.showSmsConfirm&&$("#confirmationSmsContainer").show(),n&&(r.forEach((function(e,t){Dropzone.forElement("#file-field-"+t).destroy()})),o.bindDropzone()),a){let t=JSON.parse(window["formMaps_"+e]);for(let e in t){let i=$("#map-localization-"+e),r='
',n=$("#"+t[e].map_id);n.off(),n.remove(),i.append(r),$("#"+e).val("")}o.bindMaps()}void 0!==l.refresh&&o.refreshSelectLimitedField(l.refresh)}else"error"===l.status&&o.showValidationErrors(t,l.errors);i.hide()},error:ajaxErrorResponse})}))}catch(e){o.clearMessages(t),o.showErrorMessage(t,t.find("[name=recaptcha_error_message]").val()),i.hide()}}))},this.confirmSms=function(){let t=$("#confirmationSms");$.ajax({type:"POST",url:URL_CLIENT_REQUEST,data:{Action:"CONFIRM_SMS",PluginName:"forms",form_id:e,confirmationCode:$("#confirmationCode").val(),csrf_token:getCsrfTokenJs()},success:function(e){let i=JSON.parse(e);o.clearMessages(t),"success"===i.status?(t[0].reset(),o.showSuccessMessage(t,i.message)):o.showErrorMessage(t,i.message)},complete:function(){i.hide()},error:ajaxErrorResponse})},this.clearMessages=function(e){e.find(".alert, .error").remove(),e.find(".error-map").removeClass("error-map"),e.find("[aria-invalid]").attr("aria-invalid","false"),e.find("[aria-describedby]").removeAttr("aria-describedby")},this.showSuccessMessage=function(e,t){e.append('
'+t+"
")},this.showErrorMessage=function(e,t){e.append('
'+t+"
")},this.showValidationErrors=function(e,t){let[i]=Object.keys(t),r=document.querySelector('label[for="'+i+'"]');if(r)r.scrollIntoView();else if(r=document.querySelector('input[name="'+i+'"]'),r){let e=r.parentNode.parentNode.parentNode.querySelector("legend");e&&e.scrollIntoView()}else if(r=document.querySelector('input[name="'+i+'[]"]'),r){let e=r.parentNode.parentNode.parentNode.querySelector("legend");e&&(console.log(e),e.scrollIntoView())}for(let i in t)if("refresh"===i)o.refreshSelectLimitedField(t[i]);else{let r="";"string"!=typeof t[i]?t[i].forEach((function(e,t){if(r+=e+"
","map_error"===i){let t=$("#map_field_"+e);1===t.length&&t.addClass("error-map")}})):r=t[i];let n=i+"_error",a='";if("custom"===i){e.find(".plugin-form-submit").before(a)}if("confirmation_limit"===i){a='",e.find(".plugin-form-submit").before(a)}else{let t=e.find('[name="'+i+'"]');t.length>1?(t=$(t[0]),t.parents("div.formBox").after(a),t.parents("fieldset").attr("aria-describedby",n)):0===t.length?(t=e.find('[name="'+i+'[]"]'),t.length>1&&(t=$(t[0])),"file"===t.attr("type")?(t.attr("aria-invalid","true"),t.attr("aria-describedby",n),t.after(a)):(t.parents("div.formBox").after(a),t.parents("fieldset").attr("aria-describedby",n))):t.parent().hasClass("agreement-checkbox")?(t.parent().after(a),t.attr("aria-invalid","true"),t.attr("aria-describedby",n)):(t.after(a),t.attr("aria-invalid","true"),t.attr("aria-describedby",n))}}},this.refreshSelectLimitedField=function(e){let i=e;for(let e in i){let r=t.find('[name="'+e+'"]');r.length>0&&(r=$(r[0]),r.empty(),"string"==typeof i[e]?setTimeout((function(){window.location.reload()}),3e3):i[e].forEach((function(e,t){r.append('")})))}}}