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