import{At as e,Q as t,T as n,at as r,dt as i,et as a,ht as o,ot as s,s as c,u as l,ut as u,w as d}from"./dom-CdSnbvZ6.js";import{a as f}from"./private.use-form-BL2f1xto.js";import{N as p}from"./settings-BF7JcItU.js";var m=n({name:`QForm`,props:{autofocus:Boolean,noErrorFocus:Boolean,noResetFocus:Boolean,greedy:Boolean,onSubmit:Function},emits:[`reset`,`validationSuccess`,`validationError`],setup(n,{slots:m,emit:h}){let g=t(),_=e(null),v=0,y=[];function b(e){let t=typeof e==`boolean`?e:n.noErrorFocus!==!0,r=++v,i=(e,t)=>{h(`validation${e===!0?`Success`:`Error`}`,t)},a=e=>{let t=e.validate();return typeof t.then==`function`?t.then(t=>({valid:t,comp:e}),t=>({valid:!1,comp:e,err:t})):Promise.resolve({valid:t,comp:e})};return(n.greedy===!0?Promise.all(y.map(a)).then(e=>e.filter(e=>e.valid!==!0)):y.reduce((e,t)=>e.then(()=>a(t).then(e=>{if(e.valid===!1)return Promise.reject(e)})),Promise.resolve()).catch(e=>[e])).then(e=>{if(e===void 0||e.length===0)return r===v&&i(!0),!0;if(r===v){let{comp:n,err:r}=e[0];if(r!==void 0&&console.error(r),i(!1,n),t===!0){let t=e.find(({comp:e})=>typeof e.focus==`function`&&p(e.$)===!1);t!==void 0&&t.comp.focus()}}return!1})}function x(){v++,y.forEach(e=>{typeof e.resetValidation==`function`&&e.resetValidation()})}function S(e){e!==void 0&&d(e);let t=v+1;b().then(r=>{t===v&&r===!0&&(n.onSubmit===void 0?e?.target!==void 0&&typeof e.target.submit==`function`&&e.target.submit():h(`submit`,e))})}function C(e){e!==void 0&&d(e),h(`reset`),r(()=>{x(),n.autofocus===!0&&n.noResetFocus!==!0&&w()})}function w(){f(()=>{_.value!==null&&(_.value.querySelector(`[autofocus][tabindex], [data-autofocus][tabindex]`)||_.value.querySelector(`[autofocus] [tabindex], [data-autofocus] [tabindex]`)||_.value.querySelector(`[autofocus], [data-autofocus]`)||Array.prototype.find.call(_.value.querySelectorAll(`[tabindex]`),e=>e.tabIndex!==-1))?.focus({preventScroll:!0})})}o(l,{bindComponent(e){y.push(e)},unbindComponent(e){let t=y.indexOf(e);t!==-1&&y.splice(t,1)}});let T=!1;return u(()=>{T=!0}),s(()=>{T===!0&&n.autofocus===!0&&w()}),i(()=>{n.autofocus===!0&&w()}),Object.assign(g.proxy,{validate:b,resetValidation:x,submit:S,reset:C,focus:w,getValidationComponents:()=>y}),()=>a(`form`,{class:`q-form`,ref:_,onSubmit:S,onReset:C},c(m.default))}});export{m as t};