"use strict";let H=n=>crypto.getRandomValues(new Uint8Array(n)),P=(n,c,f)=>{let d=(2<{let u="";for(;;){let b=f(m),p=m|0;for(;p--;)if(u+=n[b[p]&d]||"",u.length>=h)return u}}},X=(n,c=21)=>P(n,c|0,H);const _=X("abcdefghijklmnopqrstuvwxyz",12);var $=(n=>(n.badInput="badInput",n.customError="customError",n.patternMismatch="patternMismatch",n.rangeOverflow="rangeOverflow",n.rangeUnderflow="rangeUnderflow",n.stepMismatch="stepMismatch",n.tooLong="tooLong",n.tooShort="tooShort",n.typeMismatch="typeMismatch",n.valueMissing="valueMissing",n))($||{}),q=(n=>(n.pattern="pattern",n.max="max",n.min="min",n.step="step",n.maxlength="maxlength",n.minlength="minlength",n.type="type",n.required="required",n))(q||{});const G={badInput:void 0,customError:void 0,patternMismatch:"pattern",rangeOverflow:"max",rangeUnderflow:"min",stepMismatch:"step",tooLong:"maxlength",tooShort:"minlength",typeMismatch:"type",valueMissing:"required"};function J(n){return G[n]}const K={pattern:"patternMismatch",max:"rangeOverflow",min:"rangeUnderflow",step:"stepMismatch",maxlength:"tooLong",minlength:"tooShort",type:"typeMismatch",required:"valueMissing"};function Q(n){return K[n]}const W={badInput:"The input {{input}} is not a valid {{type}}",customError:"The input {{input}} is not valid",patternMismatch:"The value {{input}} does not match the required pattern of {{constraint}}",rangeOverflow:"The value {{input}} is too big. It cannot be bigger than {{constraint}}.",rangeUnderflow:"The value {{input}} is too small. It must be at least {{constraint}}.",stepMismatch:"The value {{input}} is not in within the correct step interval of {{constraint}}",tooLong:"The value {{input}} is too long. It cannot be longer than {{constraint}} characters.",tooShort:"The value {{input}} is too short. It must be at least {{constraint}} characters long.",typeMismatch:"The value {{input}} must be a {{constraint}}",valueMissing:"Please provide the {{type}} value"},I="form",T=".afova-form-message-container",Y={selector:I,formMessageSelector:T,validateOnChange:!1,focusOnFirstError:!0},Z=["color","date","datetime-local","email","month","number","range","tel","time","url","week"],tt=["submit","reset","button","fieldset","image"],g=new Set;function et(n){function c(t){t.id||(t.id=`afova-${_()}`)}function f(t){const e=x(t)||t;return document.querySelector(`#${e.id}-afova-message-container`)||void 0}function d(t){const e=h(t);u(e)?(e.remove(),t.style.display="none"):t.style.display=""}function m(t){const e=t.closest("form");if(e){const o=e.getAttribute("afova-form-message-container-id");if(o)return document.querySelector(`#${o}`)||void 0}}function h(t){let e=t.querySelector(".afova-message-list");return e||(e=document.createElement("UL"),e.classList.add("afova-message-list"),t.appendChild(e)),e}function u(t){return t.children.length==0}function b(t){var e;let o=f(t);if(!o){const a=x(t)||t;o=document.createElement("DIV"),(e=a.parentNode)==null||e.insertBefore(o,a),o.id=`${a.id}-afova-message-container`,o.classList.add("afova-message-container"),t.setAttribute("aria-errormessage",o.id)}return o}function p(t){if(t.type=="radio"&&t.name){const e=document.querySelectorAll(`input[name="${t.name}"][type="radio"]`),o=f(t);if(o){for(let a of e)if(o.querySelector(`[afova-message-for="${a.id}"]`))return!0}}return!1}function F(t,e){if(t!="customError"){let o=J(t),a=e.dataset[o||t.toLowerCase()]||e.dataset[t.toLowerCase()]||W[t];const i=e.getAttribute(o||"");if(i){let l=new RegExp("{{\\s*constraint\\s*}}","ig");a=a.replace(l,i)}let s=new RegExp("{{\\s*input\\s*}}","ig");return a=a.replace(s,e.value),s=new RegExp("{{\\s*type\\s*}}","ig"),a=a.replace(s,e.type),a}return e.validationMessage}function O(t){var e,o;const a=y(t);if(a){let i=a.querySelector(".afova-label");if(!i&&a.tagName!="LABEL"&&(i=a.querySelector("label")),i||(i=a),i){let s="";for(const l of i.childNodes)l.nodeType==Node.TEXT_NODE&&(s&&(e=l.textContent)!=null&&e.trim()&&(s+=" "),(o=l.textContent)!=null&&o.trim()&&(s+=l.textContent.trim()));return s}}return""}function N(t,e){const o=m(t);if(o){const a=h(o),i=document.createElement("LI");i.setAttribute("afova-message-for",t.id),i.classList.add("afova-collected-message");const s=O(t);if(s){const L=document.createElement("DIV");L.innerText=s,L.classList.add("afova-message-label"),i.appendChild(L)}const l=document.createElement("A");l.innerText=e,l.href=`#${t.id}`,l.classList.add("afova-message"),i.appendChild(l),a.appendChild(i),d(o)}}function w(t){t.removeAttribute("aria-invalid"),t.removeAttribute("aria-errormessage"),t.setCustomValidity("");const e=document.querySelectorAll(`[afova-message-for="${t.id}"]`);for(const s of e)s.remove();const o=f(t);o&&u(o)&&o.remove();const a=m(t);a&&d(a);let i=y(t);i&&document.querySelectorAll(`#${i.id} [aria-invalid].afova-control`).length==0&&i.classList.remove("afova-active")}function E(t){for(const e of v(t))w(e)}function A(t){if(!t.validity.valid&&!p(t)){const e=y(t);e&&e.classList.add("afova-active"),t.setAttribute("aria-invalid","true");const o=b(t);let a;o.tagName=="UL"||o.tagName=="OL"?a=document.createElement("LI"):a=document.createElement("DIV"),a.classList.add("afova-message"),a.setAttribute("afova-message-for",t.id),o.appendChild(a);for(const i in $)if(t.validity[i]){let s=F(i,t);s&&(a.innerHTML=s,N(t,s));break}}}async function M(t,e=!0){try{w(t),t.validity.valid&&(r.onValidateControl&&r.onValidateControl(t),r.onAsyncValidateControl&&await r.onAsyncValidateControl(t)),e&&A(t)}catch(o){if(r.onValidateControlError)r.onValidateControlError(t,o);else throw o}return t.validity.valid}function v(t){const e=[];for(const o of t.elements)tt.includes(o.type)||e.push(o);return e}async function U(t){E(t),r.onBeforeValidateForm&&r.onBeforeValidateForm(t);for(const o of v(t))await M(o,!1);t.checkValidity()&&(r.onValidateForm&&r.onValidateForm(t),r.onAsyncValidateForm&&await r.onAsyncValidateForm(t));let e;for(const o of v(t))A(o),r.focusOnFirstError&&!o.validity.valid&&!e&&(e=o,e.focus());return t.checkValidity()}function k(t){E(t)}function R(){const t=document.querySelectorAll(r.selector||I);for(const e of t){c(e),e.addEventListener("submit",V),e.addEventListener("reset",S);for(const a of v(e))j(a);e.setAttribute("novalidate","");const o=e.querySelector(r.formMessageSelector||T);o&&(c(o),d(o),e.setAttribute("afova-form-message-container-id",o.id),o.classList.add("afova-form-message-container"))}}async function V(t){t.preventDefault();const e=t.target;try{if(g.has(e.id))return;if(g.add(e.id),!await U(e)){r.onInvalid&&r.onInvalid(t),g.delete(e.id);return}r.onValid&&r.onValid(t),r.onSubmit?r.onSubmit(t):e.submit(),g.delete(e.id)}catch(o){if(g.delete(e.id),r.onValidateFormError)r.onValidateFormError(e,o);else throw o}}function S(t){k(t.target),r.onReset&&r.onReset(t)}function B(){const t=document.querySelectorAll(r.selector||"form");for(const e of t){e.removeAttribute("novalidate"),e.removeEventListener("submit",V),e.removeEventListener("reset",S);for(const o of v(e))z(o)}}function D(t){const e=t.getAttributeNames().map(o=>o.toLowerCase());for(const o in q)if(e.includes(o)&&!t.getAttribute(`data-${o}`)&&!t.getAttribute(`data-${Q(o).toLowerCase()}`)&&(o!="type"||o=="type"&&Z.includes(t.type))){const a=t.getAttribute("name");console.warn(a?`afova: Missing attribute [data-${o}] for the control with [name="${a}"]. Therefore only a fallback message will be used in case of a [${o}] constraint violation. It is recommended to define the violation message with the [data-${o}] attribute.`:`afova: Missing attribute [data-${o}] for the control with [id="${t.id}"]. Therefore only a fallback message will be used in case of a [${o}] constraint violation. It is recommended to define the violation message with the [data-${o}] attribute.`)}}function j(t){c(t),y(t),t.classList.add("afova-control"),r.validateOnChange&&t.addEventListener("change",C),D(t)}async function C(t){const e=t.target;await M(e)||e.focus()}function z(t){t.classList.remove("afova-control"),t.removeEventListener("change",C)}function y(t){let e=t.closest(".afova-context");return e||(e=t.closest("label")),e&&(c(e),e.classList.add("afova-context"),e.tagName=="LABEL"&&!e.htmlFor&&(e.htmlFor=t.id)),e||void 0}function x(t){const e=t.closest(".afova-group");return e&&c(e),e||void 0}let r=Object.assign({},Y,n);return R(),{clear:()=>B()}}export{et as afova}; //# sourceMappingURL=afova.min.js.map