import{a6 as o,h as t,J as e,I as n,o as a,Q as s,a8 as r,g as u,bY as i,aT as c,n as l,b6 as f,p as d,bZ as m}from"./index.9313841a.js";var p=o({name:"QForm",props:{autofocus:Boolean,noErrorFocus:Boolean,noResetFocus:Boolean,greedy:Boolean,onSubmit:Function},emits:["reset","validationSuccess","validationError"],setup(o,{slots:p,emit:v}){const b=u(),y=t(null);let h=0;const x=[];function S(t){const e="boolean"==typeof t?t:!0!==o.noErrorFocus,n=++h,a=(o,t)=>{v("validation"+(!0===o?"Success":"Error"),t)},s=o=>{const t=o.validate();return"function"==typeof t.then?t.then((t=>({valid:t,comp:o})),(t=>({valid:!1,comp:o,err:t}))):Promise.resolve({valid:t,comp:o})};return(!0===o.greedy?Promise.all(x.map(s)).then((o=>o.filter((o=>!0!==o.valid)))):x.reduce(((o,t)=>o.then((()=>s(t).then((o=>{if(!1===o.valid)return Promise.reject(o)}))))),Promise.resolve()).catch((o=>[o]))).then((o=>{if(void 0===o||0===o.length)return n===h&&a(!0),!0;if(n===h){const{comp:t,err:n}=o[0];if(a(!1,t),!0===e){const t=o.find((({comp:o})=>"function"==typeof o.focus&&!1===i(o.$)));void 0!==t&&t.comp.focus()}}return!1}))}function g(){h++,x.forEach((o=>{"function"==typeof o.resetValidation&&o.resetValidation()}))}function F(t){void 0!==t&&c(t);const e=h+1;S().then((n=>{e===h&&!0===n&&(void 0!==o.onSubmit?v("submit",t):void 0!==(null==t?void 0:t.target)&&"function"==typeof t.target.submit&&t.target.submit())}))}function q(t){void 0!==t&&c(t),v("reset"),l((()=>{g(),!0===o.autofocus&&!0!==o.noResetFocus&&E()}))}function E(){f((()=>{if(null===y.value)return;const o=y.value.querySelector("[autofocus][tabindex], [data-autofocus][tabindex]")||y.value.querySelector("[autofocus] [tabindex], [data-autofocus] [tabindex]")||y.value.querySelector("[autofocus], [data-autofocus]")||Array.prototype.find.call(y.value.querySelectorAll("[tabindex]"),(o=>-1!==o.tabIndex));null==o||o.focus({preventScroll:!0})}))}d(m,{bindComponent(o){x.push(o)},unbindComponent(o){const t=x.indexOf(o);-1!==t&&x.splice(t,1)}});let B=!1;return e((()=>{B=!0})),n((()=>{!0===B&&!0===o.autofocus&&E()})),a((()=>{!0===o.autofocus&&E()})),Object.assign(b.proxy,{validate:S,resetValidation:g,submit:F,reset:q,focus:E,getValidationComponents:()=>x}),()=>s("form",{class:"q-form",ref:y,onSubmit:F,onReset:q},r(p.default))}});export{p as Q};
