"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const j=require("lodash-es/cloneDeep"),C=require("lodash-es/debounce"),K=require("lodash-es/intersection"),W=require("lodash-es/isEqual"),$=require("lodash-es/omit"),c=require("vue"),z=require("./utils/asyncUtil.js"),G=require("./utils/messages.js"),H=require("./utils/validateUtil.js");function I(n){let l=!1;return n&&n.length&&n.every(f=>f.required?(l=!0,!1):!0),l}function T(n){return n==null?[]:Array.isArray(n)?n:[n]}function F(n,l,f){let u=n;l=l.replace(/\[(\w+)\]/g,".$1"),l=l.replace(/^\./,"");const r=l.split(".");let d=0;for(let y=r.length;d<y-1&&!(!u&&!f);++d){const q=r[d];if(q in u)u=u[q];else{if(f)throw new Error("please transfer a valid name path to validate!");break}}return{o:u,k:r[d],v:u?u[r[d]]:null,isValid:u&&r[d]in u}}function J(n,l=c.ref({}),f){const u=j(c.unref(n)),r=c.reactive({}),d=c.shallowRef([]),y=e=>{Object.assign(c.unref(n),{...j(u),...e}),c.nextTick(()=>{Object.keys(r).forEach(t=>{r[t]={autoLink:!1,required:I(c.unref(l)[t])}})})},q=(e=[],t)=>t.length?e.filter(i=>{const o=T(i.trigger||"change");return K(o,t).length}):e;let b=null;const D=(e,t={},i)=>{const o=[],a={};for(let h=0;h<e.length;h++){const g=e[h],p=F(c.unref(n),g,i);if(!p.isValid)continue;a[g]=p.v;const L=q(c.unref(l)[g],T(t&&t.trigger));L.length&&o.push(V(g,p.v,L,t||{}).then(()=>({name:g,errors:[],warnings:[]})).catch(x=>{const w=[],O=[];return x.forEach(({rule:{warningOnly:B},errors:M})=>{B?O.push(...M):w.push(...M)}),w.length?Promise.reject({name:g,errors:w,warnings:O}):{name:g,errors:w,warnings:O}}))}const s=z.allPromiseFinish(o);b=s;const v=s.then(()=>b===s?Promise.resolve(a):Promise.reject([])).catch(h=>{const g=h.filter(p=>p&&p.errors.length);return g.length?Promise.reject({values:a,errorFields:g,outOfDate:b!==s}):Promise.resolve(a)});return v.catch(h=>h),v},V=(e,t,i,o={})=>{const a=H.validateRules([e],t,i,{validateMessages:G.defaultValidateMessages,...o},!!o.validateFirst);return r[e]?(r[e].validateStatus="validating",a.catch(s=>s).then((s=[])=>{if(r[e].validateStatus==="validating"){const v=s.filter(h=>h&&h.errors.length);r[e].validateStatus=v.length?"error":"success",r[e].help=v.length?v.map(h=>h.errors):null,f?.onValidate?.(e,!v.length,v.length?c.toRaw(r[e].help[0]):null)}}),a):a.catch(s=>s)},P=(e,t)=>{let i=[],o=!0;e?Array.isArray(e)?i=e:i=[e]:(o=!1,i=d.value);const a=D(i,t||{},o);return a.catch(s=>s),a},U=e=>{let t=[];e?Array.isArray(e)?t=e:t=[e]:t=d.value,t.forEach(i=>{r[i]&&Object.assign(r[i],{validateStatus:"",help:null})})},_=e=>{const t={autoLink:!1},i=[],o=Array.isArray(e)?e:[e];for(let a=0;a<o.length;a++){const s=o[a];s?.validateStatus==="error"&&(t.validateStatus="error",s.help&&i.push(s.help)),t.required=t.required||s?.required}return t.help=i,t};let A=u,E=!0;const S=e=>{const t=[];d.value.forEach(i=>{const o=F(e,i,!1),a=F(A,i,!1);(E&&f?.immediate&&o.isValid||!W(o.v,a.v))&&t.push(i)}),P(t,{trigger:"change"}),E=!1,A=j(c.toRaw(e))},m=f?.debounce;let k=!0;return c.watch(l,()=>{d.value=l?Object.keys(c.unref(l)):[],!k&&f&&f.validateOnRuleChange&&P(),k=!1},{deep:!0,immediate:!0}),c.watch(d,()=>{const e={};d.value.forEach(t=>{e[t]=Object.assign({},r[t],{autoLink:!1,required:I(c.unref(l)[t])}),delete r[t]});for(const t in r)Object.prototype.hasOwnProperty.call(r,t)&&delete r[t];Object.assign(r,e)},{immediate:!0}),c.watch(n,m&&m.wait?C(S,m.wait,$(m,["wait"])):S,{immediate:f&&!!f.immediate,deep:!0}),{modelRef:n,rulesRef:l,initialModel:u,validateInfos:r,resetFields:y,validate:P,validateField:V,mergeValidateInfo:_,clearValidate:U}}exports.default=J;
