/*!
Copyright (c) 2026 Lenovo SSG BASD. All rights reserved.
@license Proprietary

This software is the confidential and proprietary property of Lenovo SSG BASD.
You shall not disclose such Confidential Information and shall use it only in
accordance with the terms of the license agreement.

--- Build Info ---
Version: 1.1.1
Build Time: 2026/2/26 17:21:00
Build Commit: 81e556ed827e2f043668506dae6fae8dbb9ef395
*/
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("../../../store/dist/esm/derived.js"),e=require("../../../store/dist/esm/store.js"),i=require("../../../store/dist/esm/scheduler.js"),s=require("./utils.js"),a=require("./ValidationLogic.js"),r=require("./standardSchemaValidator.js"),o=require("./metaHelper.js"),l=require("./EventClient.js");function n(t){return{values:t.values??{},errorMap:t.errorMap??{},fieldMetaBase:t.fieldMetaBase??{},isSubmitted:t.isSubmitted??!1,isSubmitting:t.isSubmitting??!1,isValidating:t.isValidating??!1,submissionAttempts:t.submissionAttempts??0,isSubmitSuccessful:t.isSubmitSuccessful??!1,validationMetaMap:t.validationMetaMap??{onChange:void 0,onBlur:void 0,onSubmit:void 0,onMount:void 0,onServer:void 0,onDynamic:void 0}}}function d(t){if(t){if(s.isGlobalFormValidationError(t)){return{formError:d(t.form).formError,fieldErrors:t.fields}}return{formError:t}}return{formError:void 0}}function u(t){switch(t){case"submit":return"onSubmit";case"blur":return"onBlur";case"mount":return"onMount";case"server":return"onServer";case"dynamic":return"onDynamic";default:return"onChange"}}exports.FormApi=class{constructor(h){this.options={},this.fieldInfo={},this.mount=()=>{const t=this.fieldMetaDerived.mount(),e=this.store.mount(),i=this.store.subscribe(()=>{s.throttleFormState(this)}),a=l.formEventClient.on("request-form-state",t=>{t.payload.id===this._formId&&l.formEventClient.emit("form-api",{id:this._formId,state:this.store.state,options:this.options})}),r=l.formEventClient.on("request-form-reset",t=>{t.payload.id===this._formId&&this.reset()}),o=l.formEventClient.on("request-form-force-submit",t=>{t.payload.id===this._formId&&(this._devtoolsSubmissionOverride=!0,this.handleSubmit(),this._devtoolsSubmissionOverride=!1)}),n=()=>{o(),r(),a(),i(),t(),e(),l.formEventClient.emit("form-unmounted",{id:this._formId})};this.options.listeners?.onMount?.({formApi:this});const{onMount:d}=this.options.validators||{};return l.formEventClient.emit("form-api",{id:this._formId,state:this.store.state,options:this.options}),d?(this.validateSync("mount"),n):n},this.update=t=>{if(!t)return;const e=this.options;this.options=t;const a=t.defaultValues&&!s.evaluate(t.defaultValues,e.defaultValues)&&!this.state.isTouched,r=!s.evaluate(t.defaultState,e.defaultState)&&!this.state.isTouched;(a||r)&&(i.batch(()=>{this.baseStore.setState(()=>n(Object.assign({},this.state,r?t.defaultState:{},a?{values:t.defaultValues}:{})))}),l.formEventClient.emit("form-api",{id:this._formId,state:this.store.state,options:this.options}))},this.reset=(t,e)=>{const{fieldMeta:i}=this.state,s=this.resetFieldMeta(i);t&&!e?.keepDefaultValues&&(this.options={...this.options,defaultValues:t}),this.baseStore.setState(()=>n({...this.options.defaultState,values:t??this.options.defaultValues??this.options.defaultState?.values,fieldMetaBase:s}))},this.validateAllFields=async t=>{const e=[];i.batch(()=>{Object.values(this.fieldInfo).forEach(i=>{if(!i.instance)return;const s=i.instance;e.push(Promise.resolve().then(()=>s.validate(t,{skipFormValidation:!0}))),i.instance.state.meta.isTouched||i.instance.setMeta(t=>({...t,isTouched:!0}))})});return(await Promise.all(e)).flat()},this.validateArrayFieldsStartingFrom=async(t,e,s)=>{const a=this.getFieldValue(t),r=Array.isArray(a)?Math.max(a.length-1,0):null,o=[`${t}[${e}]`];for(let i=e+1;i<=(r??0);i++)o.push(`${t}[${i}]`);const l=Object.keys(this.fieldInfo).filter(t=>o.some(e=>t.startsWith(e))),n=[];i.batch(()=>{l.forEach(t=>{n.push(Promise.resolve().then(()=>this.validateField(t,s)))})});return(await Promise.all(n)).flat()},this.validateField=(t,e)=>{const i=this.fieldInfo[t]?.instance;if(!i){const{hasErrored:i}=this.validateSync(e);return i&&!this.options.asyncAlways?this.getFieldMeta(t)?.errors??[]:this.validateAsync(e).then(()=>this.getFieldMeta(t)?.errors??[])}return i.state.meta.isTouched||i.setMeta(t=>({...t,isTouched:!0})),i.validate(e)},this.validateSync=t=>{const e=s.getSyncValidatorArray(t,{...this.options,form:this,validationLogic:this.options.validationLogic||a.defaultValidationLogic});let r=!1;const l={};return i.batch(()=>{for(const t of e){if(!t.validate)continue;const e=this.runValidator({validate:t.validate,value:{value:this.state.values,formApi:this,validationSource:"form"},type:"validate"}),{formError:i,fieldErrors:a}=d(e),n=u(t.cause),h=new Set([...Object.keys(this.state.fieldMeta),...Object.keys(a||{})]);for(const t of h){if(void 0===this.baseStore.state.fieldMetaBase[t]&&!a?.[t])continue;const e=this.getFieldMeta(t)??o.defaultFieldMeta,{errorMap:i,errorSourceMap:r}=e,d=a?.[t],{newErrorValue:u,newSource:h}=s.determineFormLevelErrorSourceAndValue({newFormValidatorError:d,isPreviousErrorFromFormValidator:"form"===r?.[n],previousErrorValue:i?.[n]});"form"===h&&(l[t]={...l[t],[n]:d}),i?.[n]!==u&&this.setFieldMeta(t,(t=o.defaultFieldMeta)=>({...t,errorMap:{...t.errorMap,[n]:u},errorSourceMap:{...t.errorSourceMap,[n]:h}}))}this.state.errorMap?.[n]!==i&&this.baseStore.setState(t=>({...t,errorMap:{...t.errorMap,[n]:i}})),(i||a)&&(r=!0)}const i=u("submit");this.state.errorMap?.[i]&&"submit"!==t&&!r&&this.baseStore.setState(t=>({...t,errorMap:{...t.errorMap,[i]:void 0}}));const a=u("server");this.state.errorMap?.[a]&&"server"!==t&&!r&&this.baseStore.setState(t=>({...t,errorMap:{...t.errorMap,[a]:void 0}}))}),{hasErrored:r,fieldsErrorMap:l}},this.validateAsync=async t=>{const e=s.getAsyncValidatorArray(t,{...this.options,form:this,validationLogic:this.options.validationLogic||a.defaultValidationLogic});this.state.isFormValidating||this.baseStore.setState(t=>({...t,isFormValidating:!0}));const i=[];let r;for(const a of e){if(!a.validate)continue;const t=u(a.cause),e=this.state.validationMetaMap[t];e?.lastAbortController.abort();const o=new AbortController;this.state.validationMetaMap[t]={lastAbortController:o},i.push(new Promise(async t=>{let e;try{e=await new Promise((t,e)=>{setTimeout(async()=>{if(o.signal.aborted)return t(void 0);try{t(await this.runValidator({validate:a.validate,value:{value:this.state.values,formApi:this,validationSource:"form",signal:o.signal},type:"validateAsync"}))}catch(i){e(i)}},a.debounceMs)})}catch(h){e=h}const{formError:i,fieldErrors:l}=d(e);l&&(r=r?{...r,...l}:l);const n=u(a.cause);for(const a of Object.keys(this.state.fieldMeta)){if(void 0===this.baseStore.state.fieldMetaBase[a])continue;const t=this.getFieldMeta(a);if(!t)continue;const{errorMap:e,errorSourceMap:i}=t,o=r?.[a],{newErrorValue:l,newSource:d}=s.determineFormLevelErrorSourceAndValue({newFormValidatorError:o,isPreviousErrorFromFormValidator:"form"===i?.[n],previousErrorValue:e?.[n]});e?.[n]!==l&&this.setFieldMeta(a,t=>({...t,errorMap:{...t.errorMap,[n]:l},errorSourceMap:{...t.errorSourceMap,[n]:d}}))}this.baseStore.setState(t=>({...t,errorMap:{...t.errorMap,[n]:i}})),t(r?{fieldErrors:r,errorMapKey:n}:void 0)}))}let o=[];const l={};if(i.length){o=await Promise.all(i);for(const t of o)if(t?.fieldErrors){const{errorMapKey:e}=t;for(const[i,s]of Object.entries(t.fieldErrors)){const t={...l[i]||{},[e]:s};l[i]=t}}}return this.baseStore.setState(t=>({...t,isFormValidating:!1})),l},this.validate=t=>{const{hasErrored:e,fieldsErrorMap:i}=this.validateSync(t);return e&&!this.options.asyncAlways?i:this.validateAsync(t)},this._handleSubmit=async t=>{this.baseStore.setState(t=>({...t,isSubmitted:!1,submissionAttempts:t.submissionAttempts+1,isSubmitSuccessful:!1})),i.batch(()=>{Object.values(this.fieldInfo).forEach(t=>{t.instance&&(t.instance.state.meta.isTouched||t.instance.setMeta(t=>({...t,isTouched:!0})))})});const e=t??this.options.onSubmitMeta;if(!this.state.canSubmit&&!this._devtoolsSubmissionOverride)return void this.options.onSubmitInvalid?.({value:this.state.values,formApi:this,meta:e});this.baseStore.setState(t=>({...t,isSubmitting:!0}));const s=()=>{this.baseStore.setState(t=>({...t,isSubmitting:!1}))};if(await this.validateAllFields("submit"),!this.state.isFieldsValid)return s(),this.options.onSubmitInvalid?.({value:this.state.values,formApi:this,meta:e}),void l.formEventClient.emit("form-submission",{id:this._formId,submissionAttempt:this.state.submissionAttempts,successful:!1,stage:"validateAllFields",errors:Object.values(this.state.fieldMeta).map(t=>t.errors).flat()});if(await this.validate("submit"),!this.state.isValid)return s(),this.options.onSubmitInvalid?.({value:this.state.values,formApi:this,meta:e}),void l.formEventClient.emit("form-submission",{id:this._formId,submissionAttempt:this.state.submissionAttempts,successful:!1,stage:"validate",errors:this.state.errors});i.batch(()=>{Object.values(this.fieldInfo).forEach(t=>{t.instance?.options.listeners?.onSubmit?.({value:t.instance.state.value,fieldApi:t.instance})})}),this.options.listeners?.onSubmit?.({formApi:this,meta:e});try{await(this.options.onSubmit?.({value:this.state.values,formApi:this,meta:e})),i.batch(()=>{this.baseStore.setState(t=>({...t,isSubmitted:!0,isSubmitSuccessful:!0})),l.formEventClient.emit("form-submission",{id:this._formId,submissionAttempt:this.state.submissionAttempts,successful:!0}),s()})}catch(a){throw this.baseStore.setState(t=>({...t,isSubmitSuccessful:!1})),l.formEventClient.emit("form-submission",{id:this._formId,submissionAttempt:this.state.submissionAttempts,successful:!1,stage:"inflight",onError:a}),s(),a}},this.getFieldValue=t=>s.getBy(this.state.values,t),this.getFieldMeta=t=>this.state.fieldMeta[t],this.getFieldInfo=t=>this.fieldInfo[t]||={instance:null,validationMetaMap:{onChange:void 0,onBlur:void 0,onSubmit:void 0,onMount:void 0,onServer:void 0,onDynamic:void 0}},this.setFieldMeta=(t,e)=>{this.baseStore.setState(i=>({...i,fieldMetaBase:{...i.fieldMetaBase,[t]:s.functionalUpdate(e,i.fieldMetaBase[t])}}))},this.resetFieldMeta=t=>Object.keys(t).reduce((t,e)=>(t[e]=o.defaultFieldMeta,t),{}),this.setFieldValue=(t,e,a)=>{const r=a?.dontUpdateMeta??!1,o=a?.dontRunListeners??!1,l=a?.dontValidate??!1;i.batch(()=>{r||this.setFieldMeta(t,t=>({...t,isTouched:!0,isDirty:!0,errorMap:{...t?.errorMap,onMount:void 0}})),this.baseStore.setState(i=>({...i,values:s.setBy(i.values,t,e)}))}),o||this.getFieldInfo(t).instance?.triggerOnChangeListener(),l||this.validateField(t,"change")},this.deleteField=t=>{const e=[...Object.keys(this.fieldInfo).filter(e=>{const i=t.toString();return e!==i&&e.startsWith(i)}),t];this.baseStore.setState(t=>{const i={...t};return e.forEach(t=>{i.values=s.deleteBy(i.values,t),delete this.fieldInfo[t],delete i.fieldMetaBase[t]}),i})},this.pushFieldValue=(t,e,i)=>{this.setFieldValue(t,t=>[...Array.isArray(t)?t:[],e],i)},this.insertFieldValue=async(t,e,i,a)=>{this.setFieldValue(t,t=>[...t.slice(0,e),i,...t.slice(e)],s.mergeOpts(a,{dontValidate:!0}));const r=a?.dontValidate??!1;r||await this.validateField(t,"change"),o.metaHelper(this).handleArrayInsert(t,e),r||await this.validateArrayFieldsStartingFrom(t,e,"change")},this.replaceFieldValue=async(t,e,i,a)=>{this.setFieldValue(t,t=>t.map((t,s)=>s===e?i:t),s.mergeOpts(a,{dontValidate:!0}));(a?.dontValidate??!1)||(await this.validateField(t,"change"),await this.validateArrayFieldsStartingFrom(t,e,"change"))},this.removeFieldValue=async(t,e,i)=>{const a=this.getFieldValue(t),r=Array.isArray(a)?Math.max(a.length-1,0):null;if(this.setFieldValue(t,t=>t.filter((t,i)=>i!==e),s.mergeOpts(i,{dontValidate:!0})),o.metaHelper(this).handleArrayRemove(t,e),null!==r){const e=`${t}[${r}]`;this.deleteField(e)}(i?.dontValidate??!1)||(await this.validateField(t,"change"),await this.validateArrayFieldsStartingFrom(t,e,"change"))},this.swapFieldValues=(t,e,i,a)=>{this.setFieldValue(t,t=>{const a=t[e],r=t[i];return s.setBy(s.setBy(t,`${e}`,r),`${i}`,a)},s.mergeOpts(a,{dontValidate:!0})),o.metaHelper(this).handleArraySwap(t,e,i);(a?.dontValidate??!1)||(this.validateField(t,"change"),this.validateField(`${t}[${e}]`,"change"),this.validateField(`${t}[${i}]`,"change"))},this.moveFieldValues=(t,e,i,a)=>{this.setFieldValue(t,t=>{const s=[...t];return s.splice(i,0,s.splice(e,1)[0]),s},s.mergeOpts(a,{dontValidate:!0})),o.metaHelper(this).handleArrayMove(t,e,i);(a?.dontValidate??!1)||(this.validateField(t,"change"),this.validateField(`${t}[${e}]`,"change"),this.validateField(`${t}[${i}]`,"change"))},this.clearFieldValues=(t,e)=>{const i=this.getFieldValue(t),a=Array.isArray(i)?Math.max(i.length-1,0):null;if(this.setFieldValue(t,[],s.mergeOpts(e,{dontValidate:!0})),null!==a)for(let s=0;s<=a;s++){const e=`${t}[${s}]`;this.deleteField(e)}(e?.dontValidate??!1)||this.validateField(t,"change")},this.resetField=t=>{this.baseStore.setState(e=>({...e,fieldMetaBase:{...e.fieldMetaBase,[t]:o.defaultFieldMeta},values:this.options.defaultValues?s.setBy(e.values,t,s.getBy(this.options.defaultValues,t)):e.values}))},this.setErrorMap=t=>{i.batch(()=>{Object.entries(t).forEach(([t,e])=>{const i=t;if(s.isGlobalFormValidationError(e)){const{formError:t,fieldErrors:s}=d(e);for(const e of Object.keys(this.fieldInfo)){this.getFieldMeta(e)&&this.setFieldMeta(e,t=>({...t,errorMap:{...t.errorMap,[i]:s?.[e]},errorSourceMap:{...t.errorSourceMap,[i]:"form"}}))}this.baseStore.setState(e=>({...e,errorMap:{...e.errorMap,[i]:t}}))}else this.baseStore.setState(t=>({...t,errorMap:{...t.errorMap,[i]:e}}))})})},this.getAllErrors=()=>({form:{errors:this.state.errors,errorMap:this.state.errorMap},fields:Object.entries(this.state.fieldMeta).reduce((t,[e,i])=>(Object.keys(i).length&&i.errors.length&&(t[e]={errors:i.errors,errorMap:i.errorMap}),t),{})}),this.parseValuesWithSchema=t=>r.standardSchemaValidators.validate({value:this.state.values,validationSource:"form"},t),this.parseValuesWithSchemaAsync=t=>r.standardSchemaValidators.validateAsync({value:this.state.values,validationSource:"form"},t),this.timeoutIds={validations:{},listeners:{},formListeners:{}},this._formId=h?.formId??s.uuid(),this._devtoolsSubmissionOverride=!1;let c=n({...h?.defaultState,values:h?.defaultValues??h?.defaultState?.values});if(h?.transform){c=h.transform({state:c}).state;for(const t of Object.keys(c.errorMap)){const e=c.errorMap[t];if(void 0!==e&&s.isGlobalFormValidationError(e))for(const i of Object.keys(e.fields)){const s=e.fields[i];if(void 0===s)continue;const a=c.fieldMetaBase[i];c.fieldMetaBase[i]={isTouched:!1,isValidating:!1,isBlurred:!1,isDirty:!1,...a??{},errorSourceMap:{...a?.errorSourceMap??{},onChange:"form"},errorMap:{...a?.errorMap??{},[t]:s}}}}}this.baseStore=new e.Store(c),this.fieldMetaDerived=new t.Derived({deps:[this.baseStore],fn:({prevDepVals:t,currDepVals:e,prevVal:i})=>{const a=i,r=t?.[0],o=e[0];let l=0;const n={};for(const d of Object.keys(o.fieldMetaBase)){const t=o.fieldMetaBase[d],e=r?.fieldMetaBase[d],i=a?.[d],u=s.getBy(o.values,d);let h=i?.errors;if(!e||t.errorMap!==e.errorMap){h=Object.values(t.errorMap??{}).filter(t=>void 0!==t);const e=this.getFieldInfo(d)?.instance;e&&e.options.disableErrorFlat||(h=h.flat(1))}const c=!s.isNonEmptyArray(h),f=!t.isDirty,m=s.evaluate(u,s.getBy(this.options.defaultValues,d))||s.evaluate(u,this.getFieldInfo(d)?.instance?.options.defaultValue);i&&i.isPristine===f&&i.isValid===c&&i.isDefaultValue===m&&i.errors===h&&t===e?(n[d]=i,l++):n[d]={...t,errors:h??[],isPristine:f,isValid:c,isDefaultValue:m}}return Object.keys(o.fieldMetaBase).length&&a&&l===Object.keys(o.fieldMetaBase).length?a:n}}),this.store=new t.Derived({deps:[this.baseStore,this.fieldMetaDerived],fn:({prevDepVals:t,currDepVals:e,prevVal:i})=>{const a=i,r=t?.[0],o=e[0],l=e[1],n=Object.values(l).filter(Boolean),d=n.some(t=>t.isValidating),u=n.every(t=>t.isValid),h=n.some(t=>t.isTouched),c=n.some(t=>t.isBlurred),f=n.every(t=>t.isDefaultValue),m=h&&o.errorMap?.onMount,v=n.some(t=>t.isDirty),p=!v,M=Boolean(o.errorMap?.onMount||n.some(t=>t?.errorMap?.onMount)),S=!!d;let b=a?.errors??[];r&&o.errorMap===r.errorMap||(b=Object.values(o.errorMap).reduce((t,e)=>void 0===e?t:e&&s.isGlobalFormValidationError(e)?(t.push(e.form),t):(t.push(e),t),[]));const V=0===b.length,g=u&&V,F=this.options.canSubmitWhenInvalid??!1,y=0===o.submissionAttempts&&!h&&!M||!S&&!o.isSubmitting&&g||F;let A=o.errorMap;if(m&&(b=b.filter(t=>t!==o.errorMap.onMount),A=Object.assign(A,{onMount:void 0})),a&&r&&a.errorMap===A&&a.fieldMeta===this.fieldMetaDerived.state&&a.errors===b&&a.isFieldsValidating===d&&a.isFieldsValid===u&&a.isFormValid===V&&a.isValid===g&&a.canSubmit===y&&a.isTouched===h&&a.isBlurred===c&&a.isPristine===p&&a.isDefaultValue===f&&a.isDirty===v&&s.evaluate(r,o))return a;return{...o,errorMap:A,fieldMeta:this.fieldMetaDerived.state,errors:b,isFieldsValidating:d,isFieldsValid:u,isFormValid:V,isValid:g,canSubmit:y,isTouched:h,isBlurred:c,isPristine:p,isDefaultValue:f,isDirty:v}}}),this.handleSubmit=this.handleSubmit.bind(this),this.update(h||{})}get state(){return this.store.state}get formId(){return this._formId}runValidator(t){return r.isStandardSchemaValidator(t.validate)?r.standardSchemaValidators[t.type](t.value,t.validate):t.validate(t.value)}handleSubmit(t){return this._handleSubmit(t)}};