const t=new WeakMap,i=new WeakMap,s=new WeakSet,h=new WeakMap;class e{constructor(t,i){(this.host=t).addController(this),this.options={form:t=>{if(t.hasAttribute("form")&&""!==t.getAttribute("form")){const i=t.getRootNode(),s=t.getAttribute("form");if(s)return i.getElementById(s)}return t.closest("form")},name:t=>t.name,value:t=>t.value,defaultValue:t=>t.defaultValue,disabled:t=>t.disabled??!1,reportValidity:t=>"function"!=typeof t.reportValidity||t.reportValidity(),setValue:(t,i)=>t.value=i,assumeInteractionOn:["nile-input"],...i},this.handleFormData=this.handleFormData.bind(this),this.handleFormSubmit=this.handleFormSubmit.bind(this),this.handleFormReset=this.handleFormReset.bind(this),this.reportFormValidity=this.reportFormValidity.bind(this),this.handleInteraction=this.handleInteraction.bind(this)}hostConnected(){const t=this.options?.form(this.host);t&&this.attachForm(t),h.set(this.host,[]),this.options?.assumeInteractionOn.forEach((t=>{this.host.addEventListener(t,this.handleInteraction)}))}hostDisconnected(){this.detachForm(),h.delete(this.host),this.options?.assumeInteractionOn.forEach((t=>{this.host.removeEventListener(t,this.handleInteraction)}))}hostUpdated(){const t=this.options.form(this.host);t||this.detachForm(),t&&this.form!==t&&(this.detachForm(),this.attachForm(t)),this.host.hasUpdated&&this.setValidity(this.host.validity.valid)}attachForm(s){s?(this.form=s,t.has(this.form)?t.get(this.form).add(this.host):t.set(this.form,new Set([this.host])),this.form.addEventListener("formdata",this.handleFormData),this.form.addEventListener("submit",this.handleFormSubmit),this.form.addEventListener("reset",this.handleFormReset),i.has(this.form)||(i.set(this.form,this.form.reportValidity),this.form.reportValidity=()=>this.reportFormValidity())):this.form=void 0}detachForm(){this.form&&(t.get(this.form)?.delete(this.host),this.form.removeEventListener("formdata",this.handleFormData),this.form.removeEventListener("submit",this.handleFormSubmit),this.form.removeEventListener("reset",this.handleFormReset),i.has(this.form)&&(this.form.reportValidity=i.get(this.form),i.delete(this.form))),this.form=void 0}handleFormData(t){const i=this.options.disabled(this.host),s=this.options.name(this.host),h=this.options.value(this.host),e="nile-button"===this.host.tagName.toLowerCase();!i&&!e&&"string"==typeof s&&s.length>0&&void 0!==h&&(Array.isArray(h)?h.forEach((i=>{t.formData.append(s,i.toString())})):t.formData.append(s,h.toString()))}handleFormSubmit(i){const s=this.options.disabled(this.host),h=this.options.reportValidity;this.form&&!this.form.noValidate&&t.get(this.form)?.forEach((t=>{this.setUserInteracted(t,!0)})),!this.form||this.form.noValidate||s||h(this.host)||(i.preventDefault(),i.stopImmediatePropagation())}handleFormReset(){this.options.setValue(this.host,this.options.defaultValue(this.host)),this.setUserInteracted(this.host,!1),h.set(this.host,[])}handleInteraction(t){const i=h.get(this.host);i.includes(t.type)||i.push(t.type),i.length===this.options.assumeInteractionOn.length&&this.setUserInteracted(this.host,!0)}reportFormValidity(){if(this.form&&!this.form.noValidate){const t=this.form.querySelectorAll("*");for(const i of t)if("function"==typeof i.reportValidity&&!i.reportValidity())return!1}return!0}setUserInteracted(t,i){i?s.add(t):s.delete(t),t.requestUpdate()}doAction(t,i){if(this.form){const s=document.createElement("button");s.type=t,s.style.position="absolute",s.style.width="0",s.style.height="0",s.style.clipPath="inset(50%)",s.style.overflow="hidden",s.style.whiteSpace="nowrap",i&&(s.name=i.name,s.value=i.value,["formaction","formenctype","formmethod","formnovalidate","formtarget"].forEach((t=>{i.hasAttribute(t)&&s.setAttribute(t,i.getAttribute(t))}))),this.form.append(s),s.click(),s.remove()}}getForm(){return this.form??null}reset(t){this.doAction("reset",t)}submit(t){this.doAction("submit",t)}setValidity(t){const i=this.host,h=Boolean(s.has(i)),e=Boolean(i.required);i.toggleAttribute("data-required",e),i.toggleAttribute("data-optional",!e),i.toggleAttribute("data-invalid",!t),i.toggleAttribute("data-valid",t),i.toggleAttribute("data-user-invalid",!t&&h),i.toggleAttribute("data-user-valid",t&&h)}updateValidity(){const t=this.host;this.setValidity(t.validity.valid)}emitInvalidEvent(t){const i=new CustomEvent("nile-invalid",{bubbles:!1,composed:!1,cancelable:!0,detail:{}});t||i.preventDefault(),this.host.dispatchEvent(i)||t?.preventDefault()}}const o=Object.freeze({badInput:!1,customError:!1,patternMismatch:!1,rangeOverflow:!1,rangeUnderflow:!1,stepMismatch:!1,tooLong:!1,tooShort:!1,typeMismatch:!1,valid:!0,valueMissing:!1});Object.freeze({...o,valid:!1,valueMissing:!0}),Object.freeze({...o,valid:!1,customError:!0});export{e as F,o as v};
