/*!
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/scheduler.js"),i=require("./standardSchemaValidator.js"),s=require("./metaHelper.js"),a=require("./utils.js"),r=require("./ValidationLogic.js");function o(t){if(t)return t}function n(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.FieldApi=class{constructor(l){this.options={},this.mount=()=>{const t=this.store.mount();void 0===this.options.defaultValue||this.getMeta().isTouched||this.form.setFieldValue(this.name,this.options.defaultValue,{dontUpdateMeta:!0});this.getInfo().instance=this,this.update(this.options);const{onMount:e}=this.options.validators||{};if(e){const t=this.runValidator({validate:e,value:{value:this.state.value,fieldApi:this,validationSource:"field"},type:"validate"});t&&this.setMeta(e=>({...e,errorMap:{...e?.errorMap,onMount:t},errorSourceMap:{...e?.errorSourceMap,onMount:"field"}}))}return this.options.listeners?.onMount?.({value:this.state.value,fieldApi:this}),t},this.update=t=>{if(this.options=t,this.name=t.name,!this.state.meta.isTouched&&void 0!==this.options.defaultValue){const e=this.form.getFieldValue(this.name);a.evaluate(e,t.defaultValue)||this.form.setFieldValue(this.name,t.defaultValue,{dontUpdateMeta:!0,dontValidate:!0,dontRunListeners:!0})}this.form.getFieldMeta(this.name)||this.form.setFieldMeta(this.name,this.state.meta)},this.getValue=()=>this.form.getFieldValue(this.name),this.setValue=(t,e)=>{this.form.setFieldValue(this.name,t,a.mergeOpts(e,{dontRunListeners:!0,dontValidate:!0})),e?.dontRunListeners||this.triggerOnChangeListener(),e?.dontValidate||this.validate("change")},this.getMeta=()=>this.store.state.meta,this.setMeta=t=>this.form.setFieldMeta(this.name,t),this.getInfo=()=>this.form.getFieldInfo(this.name),this.pushValue=(t,e)=>{this.form.pushFieldValue(this.name,t,a.mergeOpts(e,{dontRunListeners:!0})),e?.dontRunListeners||this.triggerOnChangeListener()},this.insertValue=(t,e,i)=>{this.form.insertFieldValue(this.name,t,e,a.mergeOpts(i,{dontRunListeners:!0})),i?.dontRunListeners||this.triggerOnChangeListener()},this.replaceValue=(t,e,i)=>{this.form.replaceFieldValue(this.name,t,e,a.mergeOpts(i,{dontRunListeners:!0})),i?.dontRunListeners||this.triggerOnChangeListener()},this.removeValue=(t,e)=>{this.form.removeFieldValue(this.name,t,a.mergeOpts(e,{dontRunListeners:!0})),e?.dontRunListeners||this.triggerOnChangeListener()},this.swapValues=(t,e,i)=>{this.form.swapFieldValues(this.name,t,e,a.mergeOpts(i,{dontRunListeners:!0})),i?.dontRunListeners||this.triggerOnChangeListener()},this.moveValue=(t,e,i)=>{this.form.moveFieldValues(this.name,t,e,a.mergeOpts(i,{dontRunListeners:!0})),i?.dontRunListeners||this.triggerOnChangeListener()},this.clearValues=t=>{this.form.clearFieldValues(this.name,a.mergeOpts(t,{dontRunListeners:!0})),t?.dontRunListeners||this.triggerOnChangeListener()},this.getLinkedFields=t=>{const e=Object.values(this.form.fieldInfo),i=[];for(const s of e){if(!s.instance)continue;const{onChangeListenTo:e,onBlurListenTo:a}=s.instance.options.validators||{};"change"===t&&e?.includes(this.name)&&i.push(s.instance),"blur"===t&&a?.includes(this.name)&&i.push(s.instance)}return i},this.validateSync=(t,i)=>{const s=a.getSyncValidatorArray(t,{...this.options,form:this.form,validationLogic:this.form.options.validationLogic||r.defaultValidationLogic}),l=this.getLinkedFields(t).reduce((e,i)=>{const s=a.getSyncValidatorArray(t,{...i.options,form:i.form,validationLogic:i.form.options.validationLogic||r.defaultValidationLogic});return s.forEach(t=>{t.field=i}),e.concat(s)},[]);let h=!1;e.batch(()=>{const t=(t,e)=>{const s=n(e.cause),r=e.validate?o(t.runValidator({validate:e.validate,value:{value:t.store.state.value,validationSource:"field",fieldApi:t},type:"validate"})):void 0,l=i[s],{newErrorValue:u,newSource:d}=a.determineFieldLevelErrorSourceAndValue({formLevelError:l,fieldLevelError:r});t.state.meta.errorMap?.[s]!==u&&t.setMeta(t=>({...t,errorMap:{...t.errorMap,[s]:u},errorSourceMap:{...t.errorSourceMap,[s]:d}})),u&&(h=!0)};for(const e of s)t(this,e);for(const e of l)e.validate&&t(e.field,e)});const u=n("submit");return this.state.meta.errorMap?.[u]&&"submit"!==t&&!h&&this.setMeta(t=>({...t,errorMap:{...t.errorMap,[u]:void 0},errorSourceMap:{...t.errorSourceMap,[u]:void 0}})),{hasErrored:h}},this.validateAsync=async(t,e)=>{const i=a.getAsyncValidatorArray(t,{...this.options,form:this.form,validationLogic:this.form.options.validationLogic||r.defaultValidationLogic}),s=await e,l=this.getLinkedFields(t),h=l.reduce((e,i)=>{const s=a.getAsyncValidatorArray(t,{...i.options,form:i.form,validationLogic:i.form.options.validationLogic||r.defaultValidationLogic});return s.forEach(t=>{t.field=i}),e.concat(s)},[]),u=[],d=[],m=i.some(t=>t.validate)||h.some(t=>t.validate);if(m){this.state.meta.isValidating||this.setMeta(t=>({...t,isValidating:!0}));for(const t of l)t.setMeta(t=>({...t,isValidating:!0}))}const c=(t,e,i)=>{const r=n(e.cause),l=t.getInfo().validationMetaMap[r];l?.lastAbortController.abort();const h=new AbortController;this.getInfo().validationMetaMap[r]={lastAbortController:h},i.push(new Promise(async i=>{let n;try{n=await new Promise((i,s)=>{this.timeoutIds.validations[e.cause]&&clearTimeout(this.timeoutIds.validations[e.cause]),this.timeoutIds.validations[e.cause]=setTimeout(async()=>{if(h.signal.aborted)return i(void 0);try{i(await this.runValidator({validate:e.validate,value:{value:t.store.state.value,fieldApi:t,signal:h.signal,validationSource:"field"},type:"validateAsync"}))}catch(a){s(a)}},e.debounceMs)})}catch(c){n=c}if(h.signal.aborted)return i(void 0);const l=o(n),u=s[this.name]?.[r],{newErrorValue:d,newSource:m}=a.determineFieldLevelErrorSourceAndValue({formLevelError:u,fieldLevelError:l});t.setMeta(t=>({...t,errorMap:{...t?.errorMap,[r]:d},errorSourceMap:{...t.errorSourceMap,[r]:m}})),i(d)}))};for(const a of i)a.validate&&c(this,a,u);for(const a of h)a.validate&&c(a.field,a,d);let f=[];if((u.length||d.length)&&(f=await Promise.all(u),await Promise.all(d)),m){this.setMeta(t=>({...t,isValidating:!1}));for(const t of l)t.setMeta(t=>({...t,isValidating:!1}))}return f.filter(Boolean)},this.validate=(t,e)=>{if(!this.state.meta.isTouched)return[];const{fieldsErrorMap:i}=e?.skipFormValidation?{fieldsErrorMap:{}}:this.form.validateSync(t),{hasErrored:s}=this.validateSync(t,i[this.name]??{});if(s&&!this.options.asyncAlways)return this.getInfo().validationMetaMap[n(t)]?.lastAbortController.abort(),this.state.meta.errors;const a=e?.skipFormValidation?Promise.resolve({}):this.form.validateAsync(t);return this.validateAsync(t,a)},this.handleChange=t=>{this.setValue(t)},this.handleBlur=()=>{this.state.meta.isTouched||this.setMeta(t=>({...t,isTouched:!0})),this.state.meta.isBlurred||this.setMeta(t=>({...t,isBlurred:!0})),this.validate("blur"),this.triggerOnBlurListener()},this.setErrorMap=t=>{this.setMeta(e=>({...e,errorMap:{...e.errorMap,...t}}))},this.parseValueWithSchema=t=>i.standardSchemaValidators.validate({value:this.state.value,validationSource:"field"},t),this.parseValueWithSchemaAsync=t=>i.standardSchemaValidators.validateAsync({value:this.state.value,validationSource:"field"},t),this.triggerOnChangeListener=()=>{const t=this.form.options.listeners?.onChangeDebounceMs;t&&t>0?(this.timeoutIds.formListeners.change&&clearTimeout(this.timeoutIds.formListeners.change),this.timeoutIds.formListeners.change=setTimeout(()=>{this.form.options.listeners?.onChange?.({formApi:this.form,fieldApi:this})},t)):this.form.options.listeners?.onChange?.({formApi:this.form,fieldApi:this});const e=this.options.listeners?.onChangeDebounceMs;e&&e>0?(this.timeoutIds.listeners.change&&clearTimeout(this.timeoutIds.listeners.change),this.timeoutIds.listeners.change=setTimeout(()=>{this.options.listeners?.onChange?.({value:this.state.value,fieldApi:this})},e)):this.options.listeners?.onChange?.({value:this.state.value,fieldApi:this})},this.form=l.form,this.name=l.name,this.options=l,this.timeoutIds={validations:{},listeners:{},formListeners:{}},this.store=new t.Derived({deps:[this.form.store],fn:({prevVal:t})=>{const e=t,i=this.form.getFieldMeta(this.name)??{...s.defaultFieldMeta,...l.defaultMeta};let r=this.form.getFieldValue(this.name);return i.isTouched||void 0!==r||void 0===this.options.defaultValue||a.evaluate(r,this.options.defaultValue)||(r=this.options.defaultValue),e&&e.value===r&&e.meta===i?e:{value:r,meta:i}}})}get state(){return this.store.state}runValidator(t){return i.isStandardSchemaValidator(t.validate)?i.standardSchemaValidators[t.type](t.value,t.validate):t.validate(t.value)}triggerOnBlurListener(){const t=this.form.options.listeners?.onBlurDebounceMs;t&&t>0?(this.timeoutIds.formListeners.blur&&clearTimeout(this.timeoutIds.formListeners.blur),this.timeoutIds.formListeners.blur=setTimeout(()=>{this.form.options.listeners?.onBlur?.({formApi:this.form,fieldApi:this})},t)):this.form.options.listeners?.onBlur?.({formApi:this.form,fieldApi:this});const e=this.options.listeners?.onBlurDebounceMs;e&&e>0?(this.timeoutIds.listeners.blur&&clearTimeout(this.timeoutIds.listeners.blur),this.timeoutIds.listeners.blur=setTimeout(()=>{this.options.listeners?.onBlur?.({value:this.state.value,fieldApi:this})},e)):this.options.listeners?.onBlur?.({value:this.state.value,fieldApi:this})}};