import{$ as f,C as l,a3 as u,t as p}from"./index-BeCeuELg.js";const y=(o,t,i)=>{p(o,"_name",t,i)},A=o=>o==="KOL-BUTTON"||o==="KOL-COMBOBOX"||o==="KOL-INPUT-CHECKBOX"||o==="KOL-INPUT-COLOR"||o==="KOL-INPUT-DATE"||o==="KOL-INPUT-EMAIL"||o==="KOL-INPUT-FILE"||o==="KOL-INPUT-NUMBER"||o==="KOL-INPUT-PASSWORD"||o==="KOL-INPUT-RADIO"||o==="KOL-INPUT-RANGE"||o==="KOL-INPUT-TEXT"||o==="KOL-SELECT"||o==="KOL-SINGLE-SELECT"||o==="KOL-TEXTAREA";class m{constructor(t,i,e){var s,c,r;if(this.experimentalMode=f(),this.setFormAssociatedValue=a=>{var n;const d=(n=this.formAssociated)===null||n===void 0?void 0:n.getAttribute("name");(d===null||d==="")&&l(` The form field (${this.type}) must have a name attribute to be form-associated. Please define the _name attribute.`);const h=this.tryToStringifyValue(a);this.syncValue(a,h,this.formAssociated),this.syncValue(a,h,this.syncToOwnInput)},this.component=t,this.host=this.findHostWithShadowRoot(e),this.type=i,this.experimentalMode&&A((s=this.host)===null||s===void 0?void 0:s.tagName)&&t._name){switch((c=this.host)===null||c===void 0||c.querySelectorAll("input,select,textarea").forEach(a=>{var n;(n=this.host)===null||n===void 0||n.removeChild(a)}),this.type){case"button":case"color":case"date":case"email":case"file":case"number":case"password":case"radio":case"range":case"text":this.formAssociated=document.createElement("input"),this.formAssociated.setAttribute("type",this.type);break;case"select":this.formAssociated=document.createElement("select"),this.formAssociated.setAttribute("multiple","");break;case"textarea":this.formAssociated=document.createElement("textarea");break;default:this.formAssociated=document.createElement("input"),this.formAssociated.setAttribute("type","hidden")}this.formAssociated.setAttribute("data-form-associated",""),this.formAssociated.setAttribute("hidden",""),(r=this.host)===null||r===void 0||r.appendChild(this.formAssociated)}}findHostWithShadowRoot(t){for(;t?.shadowRoot===null&&t!==document.body;)t=t?.parentNode,t.host&&(t=t.host);return t}setAttribute(t,i,e){if(this.experimentalMode)try{if(e=typeof e=="object"&&e!==null?JSON.stringify(e):e,typeof e=="boolean"||typeof e=="number"||typeof e=="string")i?.setAttribute(t,`${e}`);else throw new Error(`Invalid value type: ${typeof e}`)}catch{i?.removeAttribute(t)}}tryToStringifyValue(t){try{return typeof t=="object"&&t!==null?JSON.stringify(t).toString():t==null?null:t.toString()}catch(i){return u(`The form field raw value is not able to stringify! ${i}`),""}}syncValue(t,i,e){if(e)switch(this.type){case"file":e.files=t;break;case"select":e.querySelectorAll("option").forEach(s=>{e.removeChild(s)}),Array.isArray(t)&&t.forEach(s=>{const c=this.tryToStringifyValue(s);if(typeof c=="string"){const r=document.createElement("option");r.setAttribute("value",c),r.setAttribute("selected",""),e.appendChild(r)}});break;case"radio":typeof i=="string"&&(e.setAttribute("value",i),e.setAttribute("checked",""),e.value=i);break;default:typeof i=="string"?(e.setAttribute("value",i),e.value=i):(e.removeAttribute("value"),e.value="")}}validateName(t){y(this.component,t,{hooks:{afterPatch:()=>{this.setAttribute("name",this.formAssociated,this.component.state._name)}}}),typeof t>"u"&&l("A name on input fields or switches is not strictly required, but it might be relevant for the autocomplete function and for the static submission of the input field.")}validateSyncValueBySelector(t){if(this.experimentalMode&&typeof t=="string"){const i=document.querySelector(t);i&&(this.syncToOwnInput=i)}}componentWillLoad(){this.validateName(this.component._name),this.validateSyncValueBySelector(this.component._syncValueBySelector)}}export{m as A};
//# sourceMappingURL=associated.controller-b3473c2a-BVdUJnUP.js.map
