import{u as h}from"./index.83f60940.js";import{g as d,r as v,z as u,S as y}from"./entry.eb978f24.js";import"./index.d18b209b.js";const Z=d({props:{schema:{type:Object,default:void 0},state:{type:Object,required:!0},validate:{type:Function,default:()=>[]}},setup(t,{slots:n,expose:e}){const a=Math.random().toString(36).substring(7),o=h(`form-${a}`);o.on(async r=>{if(r.type==="blur"){const l=i.value.filter(s=>s.path!==r.path),m=(await c()).filter(s=>s.path===r.path);i.value=l.concat(m)}});const i=v([]);u("form-errors",i),u("form-events",o);async function c(){let r=await t.validate(t.state);if(t.schema)if(p(t.schema))r=r.concat(await S(t.state,t.schema));else if(g(t.schema))r=r.concat(await E(t.state,t.schema));else if(J(t.schema))r=r.concat(await F(t.state,t.schema));else throw new Error("Form validation failed: Unsupported form schema");return r}async function f(){if(i.value=await c(),i.value.length>0)throw new Error(`Form validation failed: ${JSON.stringify(i.value,null,2)}`);return t.state}return e({validate:f}),()=>y("form",n.default?.())}});function g(t){return t.validate&&t.__isYupSchema__}function w(t){return t.inner!==void 0}async function E(t,n){try{return await n.validate(t,{abortEarly:!1}),[]}catch(e){if(w(e))return e.inner.map(a=>({path:a.path??"",message:a.message}));throw e}}function p(t){return t.parse!==void 0}function b(t){return t.issues!==void 0}async function S(t,n){try{return n.parse(t),[]}catch(e){if(b(e))return e.issues.map(a=>({path:a.path.join("."),message:a.message}));throw e}}function J(t){return t.validateAsync!==void 0&&t.id!==void 0}function j(t){return t.isJoi===!0}async function F(t,n){try{return await n.validateAsync(t,{abortEarly:!1}),[]}catch(e){if(j(e))return e.details.map(a=>({path:a.path.join("."),message:a.message}));throw e}}export{Z as default};
