const e=require("../_virtual/_rolldown/runtime.js"),t=require("./utils/messages.js"),n=require("./utils/validateUtil.js"),r=require("./utils/asyncUtil.js");let i=require("vue"),a=require("lodash-es/isEqual");a=e.__toESM(a,1);let o=require("lodash-es/cloneDeep");o=e.__toESM(o,1);let s=require("lodash-es/debounce");s=e.__toESM(s,1);let c=require("lodash-es/intersection");c=e.__toESM(c,1);let l=require("lodash-es/omit");l=e.__toESM(l,1);function u(e){let t=!1;return e&&e.length&&e.every(e=>e.required?(t=!0,!1):!0),t}function d(e){return e==null?[]:Array.isArray(e)?e:[e]}function f(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 p(e,p=(0,i.ref)({}),m){let h=(0,o.default)((0,i.unref)(e)),g=(0,i.reactive)({}),_=(0,i.shallowRef)([]),v=t=>{Object.assign((0,i.unref)(e),{...(0,o.default)(h),...t}),(0,i.nextTick)(()=>{Object.keys(g).forEach(e=>{g[e]={autoLink:!1,required:u((0,i.unref)(p)[e])}})})},y=(e=[],t)=>t.length?e.filter(e=>(0,c.default)(d(e.trigger||`change`),t).length):e,b=null,x=(t,n={},a)=>{let o=[],s={};for(let r=0;r<t.length;r++){let c=t[r],l=f((0,i.unref)(e),c,a);if(!l.isValid)continue;s[c]=l.v;let u=y((0,i.unref)(p)[c],d(n&&n.trigger));u.length&&o.push(S(c,l.v,u,n||{}).then(()=>({name:c,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:c,errors:t,warnings:n}):{name:c,errors:t,warnings:n}}))}let c=r.allPromiseFinish(o);b=c;let l=c.then(()=>b===c?Promise.resolve(s):Promise.reject([])).catch(e=>{let t=e.filter(e=>e&&e.errors.length);return t.length?Promise.reject({values:s,errorFields:t,outOfDate:b!==c}):Promise.resolve(s)});return l.catch(e=>e),l},S=(e,r,a,o={})=>{let s=n.validateRules([e],r,a,{validateMessages:t.defaultValidateMessages,...o},!!o.validateFirst);return g[e]?(g[e].validateStatus=`validating`,s.catch(e=>e).then((t=[])=>{if(g[e].validateStatus===`validating`){let n=t.filter(e=>e&&e.errors.length);g[e].validateStatus=n.length?`error`:`success`,g[e].help=n.length?n.map(e=>e.errors):null,m?.onValidate?.(e,!n.length,n.length?(0,i.toRaw)(g[e].help[0]):null)}}),s):s.catch(e=>e)},C=(e,t)=>{let n=[],r=!0;e?n=Array.isArray(e)?e:[e]:(r=!1,n=_.value);let i=x(n,t||{},r);return i.catch(e=>e),i},w=e=>{let t=[];t=e?Array.isArray(e)?e:[e]:_.value,t.forEach(e=>{g[e]&&Object.assign(g[e],{validateStatus:``,help:null})})},T=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},E=h,D=!0,O=e=>{let t=[];_.value.forEach(n=>{let r=f(e,n,!1),i=f(E,n,!1);(D&&m?.immediate&&r.isValid||!(0,a.default)(r.v,i.v))&&t.push(n)}),C(t,{trigger:`change`}),D=!1,E=(0,o.default)((0,i.toRaw)(e))},k=m?.debounce,A=!0;return(0,i.watch)(p,()=>{_.value=p?Object.keys((0,i.unref)(p)):[],!A&&m&&m.validateOnRuleChange&&C(),A=!1},{deep:!0,immediate:!0}),(0,i.watch)(_,()=>{let e={};_.value.forEach(t=>{e[t]=Object.assign({},g[t],{autoLink:!1,required:u((0,i.unref)(p)[t])}),delete g[t]});for(let e in g)Object.prototype.hasOwnProperty.call(g,e)&&delete g[e];Object.assign(g,e)},{immediate:!0}),(0,i.watch)(e,k&&k.wait?(0,s.default)(O,k.wait,(0,l.default)(k,[`wait`])):O,{immediate:m&&!!m.immediate,deep:!0}),{modelRef:e,rulesRef:p,initialModel:h,validateInfos:g,resetFields:v,validate:C,validateField:S,mergeValidateInfo:T,clearValidate:w}}exports.default=p;