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