import{g as v,r as y,z as m,o as g,c as w,H as b,O as E}from"./entry.9e31e8a4.js";import{u as p}from"./index.dff89205.js";import{_ as S}from"./_plugin-vue_export-helper.c27b6911.js";import"./index.87a09f8b.js";const _=v({props:{schema:{type:Object,default:void 0},state:{type:Object,required:!0},validate:{type:Function,default:()=>[]},validateOn:{type:Array,default:()=>["blur","input","change","submit"]}},emits:["submit"],setup(e,{expose:s,emit:r}){const i=Math.random().toString(36).substring(7),o=p(`form-${i}`);o.on(async t=>{t.type!=="submit"&&e.validateOn?.includes(t.type)&&await c(t.path,{silent:!0})});const a=y([]);m("form-errors",a),m("form-events",o);async function u(){let t=await e.validate(e.state);if(e.schema)if(J(e.schema))t=t.concat(await A(e.state,e.schema));else if(O(e.schema))t=t.concat(await j(e.state,e.schema));else if(F(e.schema))t=t.concat(await k(e.state,e.schema));else if(B(e.schema))t=t.concat(await M(e.state,e.schema));else throw new Error("Form validation failed: Unsupported form schema");return t}async function c(t,n={silent:!1}){if(t){const l=a.value.filter(f=>f.path!==t),h=(await u()).filter(f=>f.path===t);a.value=l.concat(h)}else a.value=await u();if(!n.silent&&a.value.length>0)throw new Error(`Form validation failed: ${JSON.stringify(a.value,null,2)}`);return e.state}async function d(t){e.validateOn?.includes("submit")&&await c();const n=t;n.data=e.state,r("submit",t)}return s({validate:c,errors:a,setErrors(t,n){a.value=t,n?a.value=a.value.filter(l=>l.path!==n).concat(t):a.value=t},getErrors(t){return t?a.value.filter(n=>n.path===t):a.value},clear(t){t?a.value=a.value.filter(n=>n.path===t):a.value=[]}}),{onSubmit:d}}});function O(e){return e.validate&&e.__isYupSchema__}function $(e){return e.inner!==void 0}async function j(e,s){try{return await s.validate(e,{abortEarly:!1}),[]}catch(r){if($(r))return r.inner.map(i=>({path:i.path??"",message:i.message}));throw r}}function J(e){return e.parse!==void 0}async function A(e,s){const r=await s.safeParseAsync(e);return r.success===!1?r.error.issues.map(i=>({path:i.path.join("."),message:i.message})):[]}function F(e){return e.validateAsync!==void 0&&e.id!==void 0}function Y(e){return e.isJoi===!0}async function k(e,s){try{return await s.validateAsync(e,{abortEarly:!1}),[]}catch(r){if(Y(r))return r.details.map(i=>({path:i.path.join("."),message:i.message}));throw r}}function B(e){return e._parse!==void 0}async function M(e,s){const r=await s._parse(e);return r.issues?r.issues.map(i=>({path:i.path.map(o=>o.key).join("."),message:i.message})):[]}function V(e,s,r,i,o,a){return g(),w("form",{onSubmit:s[0]||(s[0]=E((...u)=>e.onSubmit&&e.onSubmit(...u),["prevent"]))},[b(e.$slots,"default")],32)}const H=S(_,[["render",V]]);export{H as default};
