(()=>{"use strict";var e=function(e,t,o,n){return new(o||(o=Promise))(function(r,i){function l(e){try{d(n.next(e))}catch(e){i(e)}}function s(e){try{d(n.throw(e))}catch(e){i(e)}}function d(e){var t;e.done?r(e.value):(t=e.value,t instanceof o?t:new o(function(e){e(t)})).then(l,s)}d((n=n.apply(e,t||[])).next())})};const t=t=>{var n,r;f(t),null===(n=t.querySelector(".nelio-forms-submit"))||void 0===n||n.addEventListener("auxclick",e=>1===e.button&&e.preventDefault()),null===(r=t.querySelector(".nelio-forms-submit"))||void 0===r||r.addEventListener("click",n=>e(void 0,void 0,void 0,function*(){const r=n.target;if(r.dataset.isNelioFormProcessed)return;if(n.preventDefault(),t.classList.contains(".nelio-forms-form--submitting"))return;a(t,r);const i=yield function(t){return e(this,void 0,void 0,function*(){var e,o;const n=yield fetch(null!==(e=t.getAttribute("action"))&&void 0!==e?e:window.location.href,{method:null!==(o=t.getAttribute("method"))&&void 0!==o?o:"POST",body:new FormData(t)});return n.ok?yield n.json():{success:!1,data:{message:`An error has occured: ${n.status}`}}})}(t);o(t,i),u(t,r),i.success&&(r.dataset.isNelioFormProcessed="true",i.data.redirection||r.click(),delete r.dataset.isNelioFormProcessed)}))},o=(e,t)=>{i(e,t),l(e,t),n(e,t),r(t)},n=(e,t)=>{t.success&&"true"===e.dataset.hideForm&&e.classList.add("nelio-forms-form--hidden")},r=e=>{e.success&&e.data.redirection&&(window.location.href=e.data.redirection)},i=(e,t)=>{const o=d(e);o.classList.toggle("nelio-forms__response--success",t.success),o.classList.toggle("nelio-forms__response--fail",!t.success),o.textContent=t.data.message},l=(e,t)=>{var o,n,r,i;if(t.success)return;const l=t.data.errors;if(!l)return;Object.keys(l).forEach(t=>{var o;return function(e,t,o){const n=function(e,t){return document.getElementById(`${e.id}__${t}`)||void 0}(e,t);if(!n)return;const r=function(e,t){return document.getElementById(`${e.id}__${t}-error`)||function(e,t){var o,n,r;const i=document.createElement("label");i.setAttribute("id",`${e.id}__${t}-error`),i.setAttribute("for",`${e.id}__${t}`),i.classList.add("nelio-forms-field__error-label");const l=null===(o=document.getElementById(`${e.id}__${t}`))||void 0===o?void 0:o.parentElement;return(null===(n=null==l?void 0:l.classList)||void 0===n?void 0:n.contains("nelio-forms-field__value-container"))?null===(r=null==l?void 0:l.parentNode)||void 0===r||r.appendChild(i):null==l||l.appendChild(i),i}(e,t)}(e,t);n.classList.remove("nelio-forms-field__value--error"),r.innerHTML="",o.length&&(n.classList.add("nelio-forms-field__value--error"),o.forEach(e=>{const t=document.createElement("div");t.textContent=e,r.appendChild(t)}))}(e,t,null!==(o=l[t])&&void 0!==o?o:[])});const d=null===(n=null===(o=e.querySelector(".nelio-forms-field__error-label > div"))||void 0===o?void 0:o.parentElement)||void 0===n?void 0:n.parentElement;d&&(s(d)||d.scrollIntoView({behavior:"smooth"}),null===(i=null===(r=e.querySelector(".nelio-forms-field__error-label > div"))||void 0===r?void 0:r.parentElement)||void 0===i||i.focus({preventScroll:!0}))},s=e=>{const t=e.getBoundingClientRect();return t.top>=0&&t.left>=0&&t.bottom<=(window.innerHeight||document.documentElement.clientHeight)&&t.right<=(window.innerWidth||document.documentElement.clientWidth)},d=e=>e.querySelector(".nelio-forms__response")||c(e),c=e=>{const t=document.createElement("div");return t.classList.add("nelio-forms__response"),e.appendChild(t),t},a=(e,t)=>{var o,n;const r=e.dataset.submitProcessingLabel;r&&(t.setAttribute("data-label",t.innerText),t.innerHTML=r),function(e){Array.from(e.querySelectorAll(".nelio-forms-field__error-label")).forEach(e=>{var t;return null===(t=e.parentNode)||void 0===t?void 0:t.removeChild(e)})}(e);const i=d(e);null===(o=i.parentNode)||void 0===o||o.removeChild(i),e.classList.add("nelio-forms-form--submitting"),null===(n=e.querySelector(".nelio-forms-submit"))||void 0===n||n.classList.add("nelio-forms-submit--disabled")},u=(e,t)=>{var o;const n=t.dataset.label;n&&(t.innerHTML=n),null===(o=e.querySelector(".nelio-forms-submit"))||void 0===o||o.classList.remove("nelio-forms-submit--disabled"),e.classList.remove("nelio-forms-form--submitting")},m=e=>"FORM"===e.nodeName,f=e=>Array.from(e.querySelectorAll(".nelio-forms-form *[name]:not([id])")).forEach(t=>{var o,n;const r=(null!==(o=t.getAttribute("name"))&&void 0!==o?o:"").replace("nelio_forms[fields][","").split("]")[0];if(!r)return;const i=`${e.id}__${r}`;t.id=i;const l=function(e,t){let o=t.parentElement;for(;o;){if(o.classList.contains("nelio-forms-field"))return o;o=o.parentElement}return o||void 0}(0,t);Array.from(null!==(n=null==l?void 0:l.querySelectorAll("label:not([for])"))&&void 0!==n?n:[]).forEach(e=>e.setAttribute("for",i))});!function(e){if("undefined"!=typeof document){if("complete"!==document.readyState&&"interactive"!==document.readyState)return document.addEventListener("DOMContentLoaded",e);e()}}(()=>Array.from(document.querySelectorAll(".nelio-forms-form")).filter(m).forEach(t))})();