/*!
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
*/
import{Derived as t}from"../../../store/dist/esm/derived.js";import{batch as e}from"../../../store/dist/esm/scheduler.js";import{standardSchemaValidators as i,isStandardSchemaValidator as s}from"./standardSchemaValidator.js";import{defaultFieldMeta as o}from"./metaHelper.js";import{evaluate as a,mergeOpts as r,getSyncValidatorArray as n,determineFieldLevelErrorSourceAndValue as l,getAsyncValidatorArray as h}from"./utils.js";import{defaultValidationLogic as u}from"./ValidationLogic.js";class d{constructor(s){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(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,r(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,r(e,{dontRunListeners:!0})),e?.dontRunListeners||this.triggerOnChangeListener()},this.insertValue=(t,e,i)=>{this.form.insertFieldValue(this.name,t,e,r(i,{dontRunListeners:!0})),i?.dontRunListeners||this.triggerOnChangeListener()},this.replaceValue=(t,e,i)=>{this.form.replaceFieldValue(this.name,t,e,r(i,{dontRunListeners:!0})),i?.dontRunListeners||this.triggerOnChangeListener()},this.removeValue=(t,e)=>{this.form.removeFieldValue(this.name,t,r(e,{dontRunListeners:!0})),e?.dontRunListeners||this.triggerOnChangeListener()},this.swapValues=(t,e,i)=>{this.form.swapFieldValues(this.name,t,e,r(i,{dontRunListeners:!0})),i?.dontRunListeners||this.triggerOnChangeListener()},this.moveValue=(t,e,i)=>{this.form.moveFieldValues(this.name,t,e,r(i,{dontRunListeners:!0})),i?.dontRunListeners||this.triggerOnChangeListener()},this.clearValues=t=>{this.form.clearFieldValues(this.name,r(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:o}=s.instance.options.validators||{};"change"===t&&e?.includes(this.name)&&i.push(s.instance),"blur"===t&&o?.includes(this.name)&&i.push(s.instance)}return i},this.validateSync=(t,i)=>{const s=n(t,{...this.options,form:this.form,validationLogic:this.form.options.validationLogic||u}),o=this.getLinkedFields(t).reduce((e,i)=>{const s=n(t,{...i.options,form:i.form,validationLogic:i.form.options.validationLogic||u});return s.forEach(t=>{t.field=i}),e.concat(s)},[]);let a=!1;e(()=>{const t=(t,e)=>{const s=c(e.cause),o=e.validate?m(t.runValidator({validate:e.validate,value:{value:t.store.state.value,validationSource:"field",fieldApi:t},type:"validate"})):void 0,r=i[s],{newErrorValue:n,newSource:h}=l({formLevelError:r,fieldLevelError:o});t.state.meta.errorMap?.[s]!==n&&t.setMeta(t=>({...t,errorMap:{...t.errorMap,[s]:n},errorSourceMap:{...t.errorSourceMap,[s]:h}})),n&&(a=!0)};for(const e of s)t(this,e);for(const e of o)e.validate&&t(e.field,e)});const r=c("submit");return this.state.meta.errorMap?.[r]&&"submit"!==t&&!a&&this.setMeta(t=>({...t,errorMap:{...t.errorMap,[r]:void 0},errorSourceMap:{...t.errorSourceMap,[r]:void 0}})),{hasErrored:a}},this.validateAsync=async(t,e)=>{const i=h(t,{...this.options,form:this.form,validationLogic:this.form.options.validationLogic||u}),s=await e,o=this.getLinkedFields(t),a=o.reduce((e,i)=>{const s=h(t,{...i.options,form:i.form,validationLogic:i.form.options.validationLogic||u});return s.forEach(t=>{t.field=i}),e.concat(s)},[]),r=[],n=[],d=i.some(t=>t.validate)||a.some(t=>t.validate);if(d){this.state.meta.isValidating||this.setMeta(t=>({...t,isValidating:!0}));for(const t of o)t.setMeta(t=>({...t,isValidating:!0}))}const f=(t,e,i)=>{const o=c(e.cause),a=t.getInfo().validationMetaMap[o];a?.lastAbortController.abort();const r=new AbortController;this.getInfo().validationMetaMap[o]={lastAbortController:r},i.push(new Promise(async i=>{let a;try{a=await new Promise((i,s)=>{this.timeoutIds.validations[e.cause]&&clearTimeout(this.timeoutIds.validations[e.cause]),this.timeoutIds.validations[e.cause]=setTimeout(async()=>{if(r.signal.aborted)return i(void 0);try{i(await this.runValidator({validate:e.validate,value:{value:t.store.state.value,fieldApi:t,signal:r.signal,validationSource:"field"},type:"validateAsync"}))}catch(o){s(o)}},e.debounceMs)})}catch(c){a=c}if(r.signal.aborted)return i(void 0);const n=m(a),h=s[this.name]?.[o],{newErrorValue:u,newSource:d}=l({formLevelError:h,fieldLevelError:n});t.setMeta(t=>({...t,errorMap:{...t?.errorMap,[o]:u},errorSourceMap:{...t.errorSourceMap,[o]:d}})),i(u)}))};for(const l of i)l.validate&&f(this,l,r);for(const l of a)l.validate&&f(l.field,l,n);let p=[];if((r.length||n.length)&&(p=await Promise.all(r),await Promise.all(n)),d){this.setMeta(t=>({...t,isValidating:!1}));for(const t of o)t.setMeta(t=>({...t,isValidating:!1}))}return p.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[c(t)]?.lastAbortController.abort(),this.state.meta.errors;const o=e?.skipFormValidation?Promise.resolve({}):this.form.validateAsync(t);return this.validateAsync(t,o)},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.validate({value:this.state.value,validationSource:"field"},t),this.parseValueWithSchemaAsync=t=>i.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=s.form,this.name=s.name,this.options=s,this.timeoutIds={validations:{},listeners:{},formListeners:{}},this.store=new t({deps:[this.form.store],fn:({prevVal:t})=>{const e=t,i=this.form.getFieldMeta(this.name)??{...o,...s.defaultMeta};let r=this.form.getFieldValue(this.name);return i.isTouched||void 0!==r||void 0===this.options.defaultValue||a(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 s(t.validate)?i[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})}}function m(t){if(t)return t}function c(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"}}export{d as FieldApi};