import{r as t,c as i,h as s,g as a,H as e}from"./p-21516f8c.js";import{f as h}from"./p-36e2fdf2.js";const l=class{constructor(s){t(this,s);this.luFormValidationValueChange=i(this,"luFormValidationValueChange",7);this.luFormValidationDidLoad=i(this,"luFormValidationDidLoad",7);this.luFormValidationDidUnload=i(this,"luFormValidationDidUnload",7);this.formValidationId=`lu-form-validation-${n++}`;this.formGroups=[];this.wrappingValidationIsValid=false;this.wrappingValidationIsInvalid=false;this.failedWrappingValidatorsData=[];this.wrappingValidation=[];this.value=[];this.useId=this.formValidationId;this.isValid=false;this.isInvalid=false;this.mutationCallback=(t,i)=>{t.forEach((t=>{if(t.type==="childList"){for(let i=0;i<t.addedNodes.length;i++){const s=h(t.addedNodes[i],"lu-form-group");if(s)s.forEach((t=>this.handleNewFormGroup(t,false)))}for(let i=0;i<t.removedNodes.length;i++){const s=h(t.removedNodes[i],"lu-form-group");if(s)s.forEach(this.luFormGroupDidUnload)}}}))};this.handleNewFormGroup=async(t,i=false)=>{t.addEventListener("luFormGroupDidUpdate",(t=>this.luFormGroupDidUpdate(t.target)));if(!i){this.formGroups=[...this.formGroups,t]}this.updateAllValues()};this.luFormGroupDidUpdate=async t=>{this.updateAllValues()};this.luFormGroupDidUnload=t=>{this.unloadCommon(t);this.updateAllValues()};this.unloadCommon=t=>{t.removeEventListener("luFormGroupDidUpdate",(t=>this.luFormGroupDidUpdate(t.target)));const i=this.formGroups.indexOf(t);if(i>-1){this.formGroups.splice(i,1)}}}async valueUpdated(){await this.validateFormWithCurrentValues();this.luFormValidationValueChange.emit(await this.getValidationValue())}async updateValues(t){t.forEach((t=>{const i=this.value.filter((i=>i.name===t.name))[0];if(i){const s=this.formGroups.filter((t=>t.id===i.id))[0];s.updateValue(t.value)}else{console.error("Attempted to set value with an invalid name.")}}))}async forceValidation(){const t=[];this.formGroups.forEach((i=>{t.push(i.validate())}));await Promise.all(t);await this.sleep(100);return this.getValidationValue()}async connectedCallback(){this.formGroups=Array.from(this.el.querySelectorAll("lu-form-group"));this.formGroups.forEach((t=>this.handleNewFormGroup(t,true)));const t=this.el;this.mutationO=new MutationObserver(this.mutationCallback);this.mutationO.observe(t,{childList:true,subtree:true})}async disconnectedCallback(){this.formGroups.forEach((t=>{t.removeEventListener("luFormGroupDidUpdate",(t=>this.luFormGroupDidUpdate(t.target)))}));this.formGroups=[];this.mutationO.disconnect();this.mutationO=undefined}async updateAllValues(){let t=[];this.formGroups.forEach((i=>{t.push(i.getValidationValues())}));this.value=await Promise.all(t);return this.value}async getElementValidationState(t){return await t.getValidationValues()}async getValidationValue(){return{id:this.useId,primaryCompoundId:this.primaryCompoundId,isValid:this.isValid,isInvalid:this.isInvalid,value:this.value,wrappingValidation:{isValid:this.wrappingValidationIsValid,isInvalid:this.wrappingValidationIsInvalid,failedWrappingValidations:this.failedWrappingValidatorsData}}}componentDidLoad(){this.luFormValidationDidLoad.emit()}componentDidUnload(){this.luFormValidationDidUnload.emit()}async validateWrappingValidators(){this.failedWrappingValidatorsData=this.wrappingValidation.filter((t=>!t.function(this.value))).map((t=>({name:t.name,message:t.message})));if(this.failedWrappingValidatorsData.length>0)this.setWrappingValidationStateAsInvalid();if(this.failedWrappingValidatorsData.length===0||this.wrappingValidation.length===0)this.setWrappingValidationStateAsValid()}async validateFormWithCurrentValues(){await this.validateWrappingValidators();this.isInvalid=this.value.filter((({isInvalid:t})=>t===true)).length>0||this.value.filter((({isValid:t})=>t===false)).length>0||this.wrappingValidationIsInvalid;this.isValid=this.value.filter((({isInvalid:t})=>t===true)).length===0&&this.value.filter((({isValid:t})=>t===false)).length===0&&this.wrappingValidationIsValid}setWrappingValidationStateAsInvalid(){this.wrappingValidationIsValid=false;this.wrappingValidationIsInvalid=true}setWrappingValidationStateAsValid(){this.wrappingValidationIsValid=true;this.wrappingValidationIsInvalid=false}sleep(t){return new Promise((i=>setTimeout(i,t)))}render(){return s(e,{id:this.useId})}get el(){return a(this)}static get watchers(){return{value:["valueUpdated"]}}};let n=0;export{l as lu_form_validation};
//# sourceMappingURL=p-3b837dfa.entry.js.map