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