import{A as e,P as t,T as n,_ as r,b as i,et as a,k as o,w as s}from"./runtime-core.esm-bundler-Babufkcf.js";import{t as c}from"./create-CGAkPJkS.js";import{m as l}from"./dom-BXiDqs7-.js";import{n as u}from"./symbols-0I_wUpZS.js";import{a as d}from"./QIcon-cTVL6ikA.js";import{f}from"./QBtn-DkiaKEsC.js";import{t as p}from"./focus-manager-IyiFCKRs.js";function m(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})}var h=c({name:`QForm`,props:{autofocus:Boolean,noErrorFocus:Boolean,noResetFocus:Boolean,greedy:Boolean,onSubmit:Function},emits:[`reset`,`validationSuccess`,`validationError`],setup(c,{slots:h,emit:g}){let _=r(),v=a(null),y=0,b=[];function x(e){let t=typeof e==`boolean`?e:!c.noErrorFocus,n=++y,r=(e,t)=>{g(`validation${e?`Success`:`Error`}`,t)};return(c.greedy?Promise.all(b.map(m)).then(e=>e.filter(e=>!e.valid)):b.reduce((e,t)=>e.then(()=>m(t)).then(e=>{if(!e.valid)throw e}),Promise.resolve()).catch(e=>[e])).then(e=>{if(e===void 0||e.length===0)return n===y&&r(!0),!0;if(n===y){let{comp:n,err:i}=e[0];if(i!==void 0&&console.error(i),r(!1,n),t){let t=e.find(({comp:e})=>typeof e.focus==`function`&&!f(e.$));t!==void 0&&t.comp.focus()}}return!1})}function S(){y++,b.forEach(e=>{typeof e.resetValidation==`function`&&e.resetValidation()})}function C(e){e!==void 0&&l(e);let t=y+1;x().then(n=>{t===y&&n&&(c.onSubmit===void 0?e?.target!==void 0&&typeof e.target.submit==`function`&&e.target.submit():g(`submit`,e))})}function w(e){e!==void 0&&l(e),g(`reset`),s(()=>{S(),c.autofocus&&!c.noResetFocus&&T()})}function T(){p(()=>{v.value!==null&&(v.value.querySelector(`[autofocus][tabindex], [data-autofocus][tabindex]`)||v.value.querySelector(`[autofocus] [tabindex], [data-autofocus] [tabindex]`)||v.value.querySelector(`[autofocus], [data-autofocus]`)||Array.prototype.find.call(v.value.querySelectorAll(`[tabindex]`),e=>e.tabIndex!==-1))?.focus({preventScroll:!0})})}t(u,{bindComponent(e){b.push(e)},unbindComponent(e){let t=b.indexOf(e);t!==-1&&b.splice(t,1)}});let E=!1;return o(()=>{E=!0}),n(()=>{E&&c.autofocus&&T()}),e(()=>{c.autofocus&&T()}),Object.assign(_.proxy,{validate:x,resetValidation:S,submit:C,reset:w,focus:T,getValidationComponents:()=>b}),()=>i(`form`,{class:`q-form`,ref:v,onSubmit:C,onReset:w},d(h.default))}});export{h as t};