import R from"../../node_modules/.pnpm/async-validator@4.2.5/node_modules/async-validator/dist-web/index.mjs";import{cloneVNode as F}from"vue";import{isValidElement as v}from"../../_util/props-util/index.mjs";import{defaultValidateMessages as V}from"./messages.mjs";import{setValues as O}from"./valueUtil.mjs";const b=R;function j(r,n){return r.replace(/\$\{\w+\}/g,o=>{const c=o.slice(2,-1);return n[c]})}async function P(r,n,o,c,u){const s={...o};delete s.ruleIndex,delete s.trigger;let m=null;s&&s.type==="array"&&s.defaultField&&(m=s.defaultField,delete s.defaultField);const h=new b({[r]:[s]}),f=O({},V,c.validateMessages);h.messages(f);let e=[];try{await Promise.resolve(h.validate({[r]:n},{...c}))}catch(t){t.errors?e=t.errors.map(({message:i},d)=>v(i)?F(i,{key:`error_${d}`}):i):(console.error(t),e=[f.default()])}if(!e.length&&m)return(await Promise.all(n.map((i,d)=>P(`${r}.${d}`,i,m,c,u)))).reduce((i,d)=>[...i,...d],[]);const l={...o,name:r,enum:(o.enum||[]).join(", "),...u};return e.map(t=>typeof t=="string"?j(t,l):t)}function N(r,n,o,c,u,s){const m=r.join("."),h=o.map((e,l)=>{const a=e.validator,t={...e,ruleIndex:l};return a&&(t.validator=(i,d,y)=>{let g=!1;const p=a(i,d,(...w)=>{Promise.resolve().then(()=>{g||y(...w)})});g=p&&typeof p.then=="function"&&typeof p.catch=="function",g&&p.then(()=>{y()}).catch(w=>{y(w||" ")})}),t}).sort(({warningOnly:e,ruleIndex:l},{warningOnly:a,ruleIndex:t})=>!!e==!!a?l-t:e?1:-1);let f;if(u===!0)f=new Promise(async(e,l)=>{for(let a=0;a<h.length;a+=1){const t=h[a],i=await P(m,n,t,c,s);if(i.length){l([{errors:i,rule:t}]);return}}e([])});else{const e=h.map(l=>P(m,n,l,c,s).then(a=>({errors:a,rule:l})));f=(u?$(e):x(e)).then(l=>Promise.reject(l))}return f.catch(e=>e),f}async function x(r){return Promise.all(r).then(n=>[].concat(...n))}async function $(r){let n=0;return new Promise(o=>{r.forEach(c=>{c.then(u=>{u.errors.length&&o([u]),n+=1,n===r.length&&o([])})})})}export{N as validateRules};
