import{f as b,q as p,z as E,r as w,y as v,o as g,c as S,H as _,P as O}from"./entry.2xM5grDS.js";import{u as $}from"./index.xpopmiYL.js";import{u as j}from"./uid.KLLbMoZc.js";import{_ as J}from"./_plugin-vue_export-helper.x3n3nnut.js";import"./index.OjlVAl1H.js";class m extends Error{constructor(n){super(n),this.message=n,Object.setPrototypeOf(this,m.prototype)}}const A=b({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","error"],setup(e,{expose:n,emit:r}){const s=$(`form-${j()}`);p(()=>{s.on(async t=>{var i;t.type!=="submit"&&((i=e.validateOn)!=null&&i.includes(t.type))&&await d(t.path,{silent:!0})})}),E(()=>{s.reset()});const a=w([]);v("form-errors",a),v("form-events",s);const c=w({});v("form-inputs",c);async function f(){let t=await e.validate(e.state);if(e.schema)if(B(e.schema))t=t.concat(await P(e.state,e.schema));else if(F(e.schema))t=t.concat(await k(e.state,e.schema));else if(q(e.schema))t=t.concat(await U(e.state,e.schema));else if(V(e.schema))t=t.concat(await Z(e.state,e.schema));else throw new Error("Form validation failed: Unsupported form schema");return t}async function d(t,i={silent:!1}){if(t){const o=a.value.filter(l=>l.path!==t),u=(await f()).filter(l=>l.path===t);a.value=o.concat(u)}else a.value=await f();if(!i.silent&&a.value.length>0)throw new m(`Form validation failed: ${JSON.stringify(a.value,null,2)}`);return e.state}async function h(t){var o;const i=t;try{(o=e.validateOn)!=null&&o.includes("submit")&&await d();const u={...i,data:e.state};r("submit",u)}catch(u){if(!(u instanceof m))throw u;const l={...i,errors:a.value.map(y=>({...y,id:c.value[y.path]}))};r("error",l)}}return n({validate:d,errors:a,setErrors(t,i){a.value=t,i?a.value=a.value.filter(o=>o.path!==i).concat(t):a.value=t},async submit(){await h(new Event("submit"))},getErrors(t){return t?a.value.filter(i=>i.path===t):a.value},clear(t){t?a.value=a.value.filter(i=>i.path!==t):a.value=[]}}),{onSubmit:h}}});function F(e){return e.validate&&e.__isYupSchema__}function Y(e){return e.inner!==void 0}async function k(e,n){try{return await n.validate(e,{abortEarly:!1}),[]}catch(r){if(Y(r))return r.inner.map(s=>({path:s.path??"",message:s.message}));throw r}}function B(e){return e.parse!==void 0}async function P(e,n){const r=await n.safeParseAsync(e);return r.success===!1?r.error.issues.map(s=>({path:s.path.join("."),message:s.message})):[]}function q(e){return e.validateAsync!==void 0&&e.id!==void 0}function M(e){return e.isJoi===!0}async function U(e,n){try{return await n.validateAsync(e,{abortEarly:!1}),[]}catch(r){if(M(r))return r.details.map(s=>({path:s.path.join("."),message:s.message}));throw r}}function V(e){return e._parse!==void 0}async function Z(e,n){const r=await n._parse(e);return r.issues?r.issues.map(s=>{var a;return{path:((a=s.path)==null?void 0:a.map(c=>c.key).join("."))||"",message:s.message}}):[]}function x(e,n,r,s,a,c){return g(),S("form",{onSubmit:n[0]||(n[0]=O((...f)=>e.onSubmit&&e.onSubmit(...f),["prevent"]))},[_(e.$slots,"default")],32)}const G=J(A,[["render",x]]);export{G as default};
