(()=>{customElements.define("forma-form",class extends HTMLElement{#i=!1;get#t(){return this.getAttribute("state")||"initial"}set#t(t){if(!["initial","submit","success","fail","error"].includes(t))throw Error("Forma: invalid state: "+t);this.setAttribute("state",t)}constructor(){super();this.#t="initial"}connectedCallback(){if(this.addEventListener("submit",this.handleSubmit),this.addEventListener("forma:submit",this.handleAddClientInfo),this.addEventListener("forma:submit",this.handleAddImNotARobot),this.addEventListener("forma:fail",this.handleShowValidationErrors),this.addEventListener("input",this.handleClearValidationErrors),!this.#i)this.dispatchEvent(new CustomEvent("forma:init",{bubbles:!0,composed:!0})),this.#i=!0}disconnectedCallback(){this.removeEventListener("submit",this.handleSubmit),this.removeEventListener("forma:submit",this.handleAddClientInfo),this.removeEventListener("forma:submit",this.handleAddImNotARobot),this.removeEventListener("forma:fail",this.handleShowValidationErrors),this.removeEventListener("input",this.handleClearValidationErrors)}handleSubmit=(t)=>{if(t.preventDefault(),this.#r())return;this.#t="submit";let e=t.target;e.inert=!0;let i=e.getAttribute("action"),s=e.getAttribute("method")||"post",a=new FormData(e);this.dispatchEvent(new CustomEvent("forma:submit",{bubbles:!0,composed:!0,detail:{form:e,data:a}})),fetch(i,{method:s,body:a}).then((r)=>r.json()).then(m).then((r)=>{switch(r.status){case"success":this.#t="success",this.#s(r.data?.message),this.dispatchEvent(new CustomEvent("forma:success",{bubbles:!0,composed:!0,detail:{form:e,response:r}})),e.reset();break;case"fail":this.#t="fail",this.#a(r.data?.message),this.dispatchEvent(new CustomEvent("forma:fail",{bubbles:!0,composed:!0,detail:{form:e,response:r}}));break;case"error":throw Error("Forma: JSend: "+r.message)}}).catch((r)=>{throw this.#t="error",this.#n(),this.dispatchEvent(new CustomEvent("forma:error",{bubbles:!0,composed:!0,detail:{form:e,error:r}})),r}).finally(()=>{e.inert=!1})};handleAddClientInfo=(t)=>{t.detail.data.append("forma_client_info",JSON.stringify({url:window.location.href,title:document.title,timestamp:new Date().toISOString(),timezone:Intl.DateTimeFormat().resolvedOptions().timeZone,language:navigator.language,userAgent:navigator.userAgent}))};handleAddImNotARobot=(t)=>{let e=t.detail.form,i=this.getAttribute("imnotarobot")||e.dataset.imnotarobot;if(i)t.detail.data.append("forma_imnotarobot",i)};handleShowValidationErrors=(t)=>{let e=t.detail.response.data?.errors||null;if(!e)return;let i=t.detail.form;Array.from(i.elements).forEach((s)=>{if(s.setCustomValidity)s.setCustomValidity("")}),Object.entries(e).forEach(([s,a])=>{let r=Array.isArray(a)?a[0]:a,n=c(i,s);if(n)n.setCustomValidity(r)}),requestAnimationFrame(()=>{if(!i.checkValidity())i.reportValidity()})};handleClearValidationErrors=(t)=>{t.target.setCustomValidity("")};#r(){return this.#t==="submit"}#s(t){return this.#e("success",t)}#a(t){return this.#e("fail",t)}#n(t){return this.#e("error",t)}#e(t,e){this.#o();let i=this.querySelector(`forma-${t}`);if(!i)return!1;if(e=e||i.getAttribute("default"),!e)return!1;return i.innerHTML=e,!0}#o(){this.querySelectorAll("forma-success, forma-fail, forma-error").forEach((t)=>t.innerText="")}});function m(t){let e=t.status;if(e===void 0)throw Error("Forma: JSend: status field is not present");if(!e)throw Error("Forma: JSend: status field is empty");if(!["success","fail","error"].includes(e))throw Error("Forma: JSend: invalid status: "+e);if((e==="success"||e==="fail")&&t.data===void 0)throw Error("Forma: JSend: data field is not present");if(e==="error"&&t.message===void 0)throw Error("Forma: JSend: message field is not present");return t}function c(t,e){let i=e.split("."),s=t.querySelectorAll("[name]"),a=[];s.forEach((n)=>{let d=f(n.name);if(d.length!==i.length)return;let u=!0;for(let o=0;o