text: !function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],r):r((e="undefined"!=typeof globalThis?globalThis:e||self).ReactHookForm={},e.React)}(this,(function(e,r){"use strict";var t=e=>e instanceof HTMLElement;const n="blur",s="change",c="input",u="onBlur",i="onChange",a="onSubmit",o="onTouched",l="all",f="undefined",d="max",y="min",g="maxLength",b="minLength",m="pattern",h="required",v="validate";var p=e=>null==e;const O=e=>"object"==typeof e;var A=e=>!p(e)&&!Array.isArray(e)&&O(e)&&!(e instanceof Date),R=e=>/^\w*$/.test(e),V=e=>e.filter(Boolean),j=e=>V(e.replace(/["|']/g,"").replace(/\[/g,".").replace(/\]/g,"").split("."));function F(e,r,t){let n=-1;const s=R(r)?[r]:j(r),c=s.length,u=c-1;for(;++n<c;){const r=s[n];let c=t;if(n!==u){const t=e[r];c=A(t)||Array.isArray(t)?t:isNaN(+s[n+1])?{}:[]}e[r]=c,e=e[r]}return e}var C=(e,r={})=>{for(const t in e)R(t)?r[t]=e[t]:F(r,t,e[t]);return r},k=e=>void 0===e,S=(e={},r,t)=>{const n=V(r.split(/[,[\].]+?/)).reduce((e,r)=>p(e)?e:e[r],e);return k(n)||n===e?k(e[r])?t:e[r]:n},x=(e,r)=>{t(e)&&e.removeEventListener&&(e.removeEventListener(c,r),e.removeEventListener(s,r),e.removeEventListener(n,r))};const w={isValid:!1,value:null};var D=e=>Array.isArray(e)?e.reduce((e,r)=>r&&r.ref.checked?{isValid:!0,value:r.ref.value}:e,w):w,E=e=>"radio"===e.type,B=e=>"file"===e.type,N=e=>"checkbox"===e.type,T=e=>"select-multiple"===e.type;const L={value:!1,isValid:!1},W={value:!0,isValid:!0};var M=e=>{if(Array.isArray(e)){if(e.length>1){const r=e.filter(e=>e&&e.ref.checked).map(({ref:{value:e}})=>e);return{value:r,isValid:!!r.length}}const{checked:r,value:t,attributes:n}=e[0].ref;return r?n&&!k(n.value)?k(t)||""===t?W:{value:t,isValid:!0}:W:L}return L};function P(e,r,t,n,s){const c=e.current[r];if(c){const{ref:{value:e,disabled:r},ref:t,valueAsNumber:i,valueAsDate:a,setValueAs:o}=c;if(r&&n)return;return B(t)?t.files:E(t)?D(c.options).value:T(t)?(u=t.options,[...u].filter(({selected:e})=>e).map(({value:e})=>e)):N(t)?M(c.options).value:s?e:i?""===e?NaN:+e:a?t.valueAsDate:o?o(e):e}var u;if(t)return S(t.current,r)}function $(e){return!e||e instanceof HTMLElement&&e.nodeType!==Node.DOCUMENT_NODE&&$(e.parentNode)}var H=e=>A(e)&&!Object.keys(e).length,U=e=>"boolean"==typeof e;function q(e,r){const t=R(r)?[r]:j(r),n=1==t.length?e:function(e,r){const t=r.slice(0,-1).length;let n=0;for(;n<t;)e=k(e)?n++:e[r[n++]];return e}(e,t),s=t[t.length-1];let c;n&&delete n[s];for(let r=0;r<t.slice(0,-1).length;r++){let n,s=-1;const u=t.slice(0,-(r+1)),i=u.length-1;for(r>0&&(c=e);++s<u.length;){const r=u[s];n=n?n[r]:e[r],i===s&&(A(n)&&H(n)||Array.isArray(n)&&!n.filter(e=>A(e)&&!H(e)||U(e)).length)&&(c?delete c[r]:delete e[r]),c=n}}return e}const I=(e,r)=>e&&e.ref===r;var _=e=>p(e)||!O(e);function z(e,r){if(_(e)||_(r))return r;for(const t in r){const n=e[t],s=r[t];try{e[t]=A(n)&&A(s)||Array.isArray(n)&&Array.isArray(s)?z(n,s):s}catch(e){}}return e}function G(e,t,n){if(_(e)||_(t)||e instanceof Date||t instanceof Date)return e===t;if(!r.isValidElement(e)){const r=Object.keys(e),s=Object.keys(t);if(r.length!==s.length)return!1;for(const s of r){const r=e[s];if(!n||"ref"!==s){const e=t[s];if((A(r)||Array.isArray(r))&&(A(e)||Array.isArray(e))?!G(r,e,n):r!==e)return!1}}}return!0}function J(e,r,t,n,s){let c=-1;for(;++c<e.length;){for(const n in e[c])Array.isArray(e[c][n])?(!t[c]&&(t[c]={}),t[c][n]=[],J(e[c][n],S(r[c]||{},n,[]),t[c][n],t[c],n)):G(S(r[c]||{},n),e[c][n])?F(t[c]||{},n):t[c]=Object.assign(Object.assign({},t[c]),{[n]:!0});n&&!t.length&&delete n[s]}return t}var K=(e,r,t)=>z(J(e,r,t.slice(0,e.length)),J(r,e,t.slice(0,e.length))),Q=e=>"string"==typeof e,X=(e,r,t,n,s)=>{const c={};for(const r in e.current)(k(s)||(Q(s)?r.startsWith(s):Array.isArray(s)&&s.find(e=>r.startsWith(e))))&&(c[r]=P(e,r,void 0,n));return t?C(c):z(r,C(c))},Y=e=>e instanceof RegExp,Z=e=>A(e)&&!Y(e)?e:{value:e,message:""},ee=e=>"function"==typeof e,re=e=>Q(e)||r.isValidElement(e);function te(e,r,t="validate"){if(re(e)||U(e)&&!e)return{type:t,message:re(e)?e:"",ref:r}}var ne=(e,r,t,n,s)=>r?Object.assign(Object.assign({},t[e]),{types:Object.assign(Object.assign({},t[e]&&t[e].types?t[e].types:{}),{[n]:s||!0})}):{},se=async(e,r,{ref:t,ref:{value:n},options:s,required:c,maxLength:u,minLength:i,min:a,max:o,pattern:l,validate:f},O)=>{const R=t.name,V={},j=E(t),F=N(t),C=j||F,k=""===n,S=ne.bind(null,R,r,V),x=(e,r,n,s=g,c=b)=>{const u=e?r:n;V[R]=Object.assign({type:e?s:c,message:u,ref:t},S(e?s:c,u))};if(c&&(!j&&!F&&(k||p(n))||U(n)&&!n||F&&!M(s).isValid||j&&!D(s).isValid)){const{value:n,message:s}=re(c)?{value:!!c,message:c}:Z(c);if(n&&(V[R]=Object.assign({type:h,message:s,ref:C?((e.current[R].options||[])[0]||{}).ref:t},S(h,s)),!r))return V}if(!(p(a)&&p(o)||""===n)){let e,s;const c=Z(o),u=Z(a);if(isNaN(n)){const r=t.valueAsDate||new Date(n);Q(c.value)&&(e=r>new Date(c.value)),Q(u.value)&&(s=r<new Date(u.value))}else{const r=t.valueAsNumber||parseFloat(n);p(c.value)||(e=r>c.value),p(u.value)||(s=r<u.value)}if((e||s)&&(x(!!e,c.message,u.message,d,y),!r))return V}if(Q(n)&&!k&&(u||i)){const e=Z(u),t=Z(i),s=!p(e.value)&&n.length>e.value,c=!p(t.value)&&n.length<t.value;if((s||c)&&(x(s,e.message,t.message),!r))return V}if(Q(n)&&l&&!k){const{value:e,message:s}=Z(l);if(Y(e)&&!e.test(n)&&(V[R]=Object.assign({type:m,message:s,ref:t},S(m,s)),!r))return V}if(f){const n=P(e,R,O,!1,!0),c=C&&s?s[0].ref:t;if(ee(f)){const e=te(await f(n),c);if(e&&(V[R]=Object.assign(Object.assign({},e),S(v,e.message)),!r))return V}else if(A(f)){let e={};for(const[t,s]of Object.entries(f)){if(!H(e)&&!r)break;const u=te(await s(n),c,t);u&&(e=Object.assign(Object.assign({},u),S(t,u.message)),r&&(V[R]=e))}if(!H(e)&&(V[R]=Object.assign({ref:c},e),!r))return V}}return V};const ce=(e,r,t=[])=>{for(const n in r){const s=e+(A(r)?"."+n:`[${n}]`);_(r[n])?t.push(s):ce(s,r[n],t)}return t};var ue=(e,r,t,n,s)=>{let c=void 0;return t.add(r),H(e)||(c=S(e,r),(A(c)||Array.isArray(c))&&ce(r,c).forEach(e=>t.add(e))),k(c)?s?n:S(n,r):c},ie=({isOnBlur:e,isOnChange:r,isOnTouch:t,isTouched:n,isReValidateOnBlur:s,isReValidateOnChange:c,isBlurEvent:u,isSubmitted:i,isOnAll:a})=>!a&&(!i&&t?!(n||u):(i?s:e)?!u:!(i?c:r)||u),ae=e=>e.substring(0,e.indexOf("["));const oe=(e,r)=>RegExp(`^${r}([|.)\\d+`.replace(/\[/g,"\\[").replace(/\]/g,"\\]")).test(e);var le=(e,r)=>[...e].some(e=>oe(r,e));var fe=typeof window!==f&&typeof document!==f;function de(e){var r;let n;if(_(e)||fe&&(e instanceof File||t(e)))return e;if(!["Set","Map","Object","Date","Array"].includes(null===(r=e.constructor)||void 0===r?void 0:r.name))return e;if(e instanceof Date)return n=new Date(e.getTime()),n;if(e instanceof Set){n=new Set;for(const r of e)n.add(r);return n}if(e instanceof Map){n=new Map;for(const r of e.keys())n.set(r,de(e.get(r)));return n}n=Array.isArray(e)?[]:{};for(const r in e)n[r]=de(e[r]);return n}var ye=e=>({isOnSubmit:!e||e===a,isOnBlur:e===u,isOnChange:e===i,isOnAll:e===l,isOnTouch:e===o}),ge=e=>E(e)||N(e);const be=typeof window===f,me=fe?"Proxy"in window:typeof Proxy!==f;function he(e,r){var t={};for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&r.indexOf(n)<0&&(t[n]=e[n]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var s=0;for(n=Object.getOwnPropertySymbols(e);s<n.length;s++)r.indexOf(n[s])<0&&Object.prototype.propertyIsEnumerable.call(e,n[s])&&(t[n[s]]=e[n[s]])}return t}const ve=r.createContext(null);ve.displayName="RHFContext";const pe=()=>r.useContext(ve);var Oe=()=>{const e=typeof performance===f?Date.now():1e3*performance.now();return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,r=>{const t=(16*Math.random()+e)%16|0;return("x"==r?t:3&t|8).toString(16)})};var Ae=(e,r)=>k(r)?[]:function(e,r){let t=0;const n=[...e];for(const e of r)n.splice(e-t,1),t++;return V(n).length?n:[]}(e,(Array.isArray(r)?r:[r]).sort((e,r)=>e-r)),Re=(e,r,t)=>Array.isArray(e)?(k(e[t])&&(e[t]=void 0),e.splice(t,0,e.splice(r,1)[0]),e):[],Ve=(e,r,t)=>{const n=[e[t],e[r]];e[r]=n[0],e[t]=n[1]};function je(e,r){return[...Array.isArray(r)?r:[r||void 0],...e]}function Fe(e,r,t){return[...e.slice(0,r),...Array.isArray(t)?t:[t||void 0],...e.slice(r)]}var Ce=e=>Array.isArray(e)?Array(e.length).fill(void 0):void 0,ke=e=>(Array.isArray(e)?e:[e]).map(e=>{if(A(e)){const r={};for(const t in e)r[t]=!0;return r}return!0});const Se=(e=[],r,t)=>e.map(e=>Object.assign({[r]:e[r]||Oe()},e));function xe({name:e,rules:t,defaultValue:n,control:s,onFocus:c}){const u=pe(),{defaultValuesRef:i,setValue:a,register:o,unregister:l,trigger:f,mode:d,reValidateMode:{isReValidateOnBlur:y,isReValidateOnChange:g},formState:b,formStateRef:{current:{isSubmitted:m,touched:h,errors:v}},updateFormState:p,readFormStateRef:O,fieldsRef:R,fieldArrayNamesRef:V,shallowFieldsStateRef:j}=s||u.control,C=!le(V.current,e),x=()=>!k(S(j.current,e))&&C?S(j.current,e):k(n)?S(i.current,e):n,[w,D]=r.useState(x()),E=r.useRef(w),B=r.useRef({focus:()=>null}),N=r.useRef(c||(()=>{ee(B.current.focus)&&B.current.focus()})),T=r.useCallback(r=>!ie(Object.assign({isBlurEvent:r,isReValidateOnBlur:y,isReValidateOnChange:g,isSubmitted:m,isTouched:!!S(h,e)},d)),[y,g,m,h,e,d]),L=r.useCallback(([e])=>{const r=(e=>_(e)||!A(e.target)||A(e.target)&&!e.type?e:k(e.target.value)?e.target.checked:e.target.value)(e);return D(r),E.current=r,r},[]),W=r.useCallback(r=>{R.current[e]?R.current[e]=Object.assign({ref:R.current[e].ref},t):(o(Object.defineProperties({name:e,focus:N.current},{value:{set(e){D(e),E.current=e},get:()=>E.current}}),t),r=k(S(i.current,e))),r&&C&&D(x())},[t,e,o]);r.useEffect(()=>()=>l(e),[e]),r.useEffect(()=>{W()},[W]),r.useEffect(()=>{!R.current[e]&&W(!0)});const M=r.useCallback(()=>{O.current.touched&&!S(h,e)&&(F(h,e,!0),p({touched:h})),T(!0)&&f(e)},[e,p,T,f,O]);return{field:{onChange:r.useCallback((...r)=>a(e,L(r),{shouldValidate:T(),shouldDirty:!0}),[a,e,T]),onBlur:M,name:e,value:w,ref:B},meta:Object.defineProperties({invalid:!!S(v,e)},{isDirty:{get:()=>!!S(b.dirtyFields,e)},isTouched:{get:()=>!!S(b.touched,e)}})}}e.Controller=e=>{const{rules:t,as:n,render:s,defaultValue:c,control:u,onFocus:i}=e,a=he(e,["rules","as","render","defaultValue","control","onFocus"]),{field:o,meta:l}=xe(e),f=Object.assign(Object.assign({},a),o);return n?r.isValidElement(n)?r.cloneElement(n,f):r.createElement(n,f):s?s(o,l):null},e.FormProvider=e=>{var{children:t}=e,n=he(e,["children"]);return r.createElement(ve.Provider,{value:Object.assign({},n)},t)},e.appendErrors=ne,e.get=S,e.transformToNestObject=C,e.useController=xe,e.useFieldArray=({control:e,name:t,keyName:n="id"})=>{const s=pe(),c=r.useRef(-1),u=r.useRef(!1),{isFormDirty:i,updateWatchedValue:a,resetFieldArrayFunctionRef:o,fieldArrayNamesRef:l,fieldsRef:f,defaultValuesRef:d,removeFieldEventListener:y,formStateRef:g,shallowFieldsStateRef:b,updateFormState:m,readFormStateRef:h,validFieldsRef:v,fieldsWithValidationRef:p,fieldArrayDefaultValuesRef:O,validateResolver:A,getValues:R,shouldUnregister:j,fieldArrayValuesRef:C}=e||s.control,k=e=>S(j?e:b.current,t,[]),x=ae(t),w=r.useRef([...S(O.current,x)?k(O.current):k(d.current)]),[D,E]=r.useState(Se(w.current,n));F(C.current,t,D);const B=e=>e.map((e={})=>{var r=n;e[r];return he(e,["symbol"==typeof r?r:r+""])});l.current.add(t);const N=r.useCallback(()=>S(C.current,t,[]),[t]),T=()=>Se(S(R(),t,N()).map((e,r)=>Object.assign(Object.assign({},N()[r]),e)),n);l.current.add(t),x&&!S(O.current,x)&&F(O.current,x,de(S(d.current,x)));const L=e=>{if(E(e),F(C.current,t,e),h.current.isValid&&A){const r=R();F(r,t,e),A(r)}},W=()=>{for(const e in f.current)oe(e,t)&&(y(f.current[e],!0),delete f.current[e])},M=e=>!V(S(e,t,[])).length&&q(e,t),P=e=>{e&&F(g.current.dirtyFields,t,K(B(e),S(d.current,t,[]),S(g.current.dirtyFields,t,[])))},$=(e,r,n,s=[],c=!0,a=!1)=>{if(S(b.current,t)){const n=e(S(b.current,t),r.argA,r.argB);c&&F(b.current,t,n)}if(S(O.current,t)){const n=e(S(O.current,t),r.argA,r.argB);c&&F(O.current,t,n)}if(Array.isArray(S(g.current.errors,t))){const n=e(S(g.current.errors,t),r.argA,r.argB);c&&F(g.current.errors,t,n),M(g.current.errors)}if(h.current.touched&&S(g.current.touched,t)){const n=e(S(g.current.touched,t),r.argA,r.argB);c&&F(g.current.touched,t,n),M(g.current.touched)}(h.current.dirtyFields||h.current.isDirty)&&(F(g.current.dirtyFields,t,K(B(s),S(d.current,t,[]),S(g.current.dirtyFields,t,[]))),P(n),M(g.current.dirtyFields)),a&&h.current.isValid&&!A&&(F(v.current,t,e(S(v.current,t,[]),r.argA)),M(v.current),F(p.current,t,e(S(p.current,t,[]),r.argA)),M(p.current)),!u.current&&h.current.isDirty&&m({isDirty:i(t,B(s))})},H=e=>{const r=T(),t=Ae(r,e);L(t),W(),$(Ae,{argA:e,argC:e},t,Ae(r,e),!0,!0)};return r.useEffect(()=>{const e=S(O.current,t);if(e&&D.length<e.length&&F(O.current,t,e.slice(1)),a(t),c.current>-1)for(const e in f.current){const r=f.current[e];if(e.startsWith(`${t}[${c.current}]`)&&r.ref.focus){r.ref.focus();break}}c.current=-1},[D,t]),r.useEffect(()=>{const e=o.current,r=l.current;return ae(t)||(e[t]=e=>{W(),!e&&q(O.current,t),q(b.current,t),w.current=S(e||d.current,t),u.current||E(Se(w.current,n))}),()=>{u.current=!0,j&&H(),W(),delete e[t],q(C.current,t),r.delete(t)}},[]),{swap:r.useCallback((e,r)=>{const t=T();Ve(t,e,r),W(),L([...t]),$(Ve,{argA:e,argB:r,argC:e,argD:r},void 0,t,!1)},[t]),move:r.useCallback((e,r)=>{const t=T();Re(t,e,r),W(),L([...t]),$(Re,{argA:e,argB:r,argC:e,argD:r},void 0,t,!1)},[t]),prepend:r.useCallback((e,r=!0)=>{const t=Ce(e),s=je(T(),Se(Array.isArray(e)?e:[e],n));L(s),W(),$(je,{argA:t,argC:ke(e)},s),c.current=r?0:-1},[t]),append:r.useCallback((e,r=!0)=>{const s=Array.isArray(e)?e:[e],u=[...T(),...Se(s,n)];L(u),(h.current.dirtyFields||h.current.isDirty)&&(P(u),m({isDirty:!0,dirtyFields:g.current.dirtyFields})),!j&&F(b.current,t,[...S(b.current,t)||[],...de(s)]),c.current=r?S(C.current,t).length-1:-1},[t]),remove:r.useCallback(H,[t]),insert:r.useCallback((e,r,t=!0)=>{const s=Ce(r),u=T(),i=Fe(u,e,Se(Array.isArray(r)?r:[r],n));L(i),W(),$(Fe,{argA:e,argB:s,argC:e,argD:ke(r)},i,Fe(u,e)),c.current=t?e:-1},[t]),fields:D}},e.useForm=function({mode:e=a,reValidateMode:u=i,resolver:o,context:f,defaultValues:d={},shouldFocusError:y=!0,shouldUnregister:g=!0,criteriaMode:b}={}){const m=r.useRef({}),h=r.useRef({}),v=r.useRef({}),O=r.useRef(new Set),j=r.useRef({}),w=r.useRef({}),D=r.useRef({}),L=r.useRef({}),W=r.useRef(d),M=r.useRef(!1),U=r.useRef(!1),z=r.useRef(),J=r.useRef({}),Y=r.useRef({}),Z=r.useRef(f),re=r.useRef(o),te=r.useRef(new Set),ne=r.useRef(ye(e)),{isOnSubmit:oe,isOnTouch:he}=ne.current,ve=b===l,[pe,Oe]=r.useState({isDirty:!1,isValidating:!1,dirtyFields:{},isSubmitted:!1,submitCount:0,touched:{},isSubmitting:!1,isSubmitSuccessful:!1,isValid:!oe,errors:{}}),Ae=r.useRef({isDirty:!me,dirtyFields:!me,touched:!me||he,isValidating:!me,isSubmitting:!me,isValid:!me}),Re=r.useRef(pe),Ve=r.useRef(),{isOnBlur:je,isOnChange:Fe}=r.useRef(ye(u)).current;Z.current=f,re.current=o,Re.current=pe,J.current=g?{}:H(J.current)?de(d):J.current;const Ce=r.useCallback((e={})=>{M.current||(Re.current=Object.assign(Object.assign({},Re.current),e),Oe(Re.current))},[]),ke=()=>Ae.current.isValidating&&Ce({isValidating:!0}),Se=r.useCallback((e,r,t=!1,n={},s)=>{let c=t||(({errors:e,name:r,error:t,validFields:n,fieldsWithValidation:s})=>{const c=k(t),u=S(e,r);return c&&!!u||!c&&!G(u,t,!0)||c&&S(s,r)&&!S(n,r)})({errors:Re.current.errors,error:r,name:e,validFields:L.current,fieldsWithValidation:D.current});const u=S(Re.current.errors,e);r?(q(L.current,e),c=c||!u||!G(u,r,!0),F(Re.current.errors,e,r)):((S(D.current,e)||re.current)&&(F(L.current,e,!0),c=c||u),q(Re.current.errors,e)),(c&&!p(t)||!H(n)||Ae.current.isValidating)&&Ce(Object.assign(Object.assign(Object.assign({},n),re.current?{isValid:!!s}:{}),{isValidating:!1}))},[]),xe=r.useCallback((e,r)=>{const{ref:n,options:s}=m.current[e],c=fe&&t(n)&&p(r)?"":r;E(n)?(s||[]).forEach(({ref:e})=>e.checked=e.value===c):B(n)&&!Q(c)?n.files=c:T(n)?[...n.options].forEach(e=>e.selected=c.includes(e.value)):N(n)&&s?s.length>1?s.forEach(({ref:e})=>e.checked=Array.isArray(c)?!!c.find(r=>r===e.value):c===e.value):s[0].ref.checked=!!c:n.value=c},[]),we=r.useCallback((e,r)=>{if(Ae.current.isDirty){const t=$e();return e&&r&&F(t,e,r),!G(t,W.current)}return!1},[]),De=r.useCallback((e,r=!0)=>{if(Ae.current.isDirty||Ae.current.dirtyFields){const t=!G(S(W.current,e),P(m,e,J)),n=S(Re.current.dirtyFields,e),s=Re.current.isDirty;t?F(Re.current.dirtyFields,e,!0):q(Re.current.dirtyFields,e);const c={isDirty:we(),dirtyFields:Re.current.dirtyFields},u=Ae.current.isDirty&&s!==c.isDirty||Ae.current.dirtyFields&&n!==S(Re.current.dirtyFields,e);return u&&r&&Ce(c),u?c:{}}return{}},[]),Ee=r.useCallback(async(e,r)=>{const t=(await se(m,ve,m.current[e],J))[e];return Se(e,t,r),k(t)},[Se,ve]),Be=r.useCallback(async e=>{const{errors:r}=await re.current($e(),Z.current,ve),t=Re.current.isValid;if(Array.isArray(e)){const t=e.map(e=>{const t=S(r,e);return t?F(Re.current.errors,e,t):q(Re.current.errors,e),!t}).every(Boolean);return Ce({isValid:H(r),isValidating:!1}),t}{const n=S(r,e);return Se(e,n,t!==H(r),{},H(r)),!n}},[Se,ve]),Ne=r.useCallback(async e=>{const r=e||Object.keys(m.current);if(ke(),re.current)return Be(r);if(Array.isArray(r)){!e&&(Re.current.errors={});const t=await Promise.all(r.map(async e=>await Ee(e,null)));return Ce({isValidating:!1}),t.every(Boolean)}return await Ee(r)},[Be,Ee]),Te=r.useCallback((e,r,{shouldDirty:t,shouldValidate:n})=>{const s={};F(s,e,r);for(const c of ce(e,r))m.current[c]&&(xe(c,S(s,c)),t&&De(c),n&&Ne(c))},[Ne,xe,De]),Le=r.useCallback((e,r,t)=>{if(!g&&!_(r)&&F(J.current,e,Array.isArray(r)?[...r]:Object.assign({},r)),m.current[e])xe(e,r),t.shouldDirty&&De(e),t.shouldValidate&&Ne(e);else if(!_(r)&&(Te(e,r,t),te.current.has(e))){const n=ae(e)||e;F(h.current,e,r),Y.current[n]({[n]:S(h.current,n)}),(Ae.current.isDirty||Ae.current.dirtyFields)&&t.shouldDirty&&(F(Re.current.dirtyFields,e,K(r,S(W.current,e,[]),S(Re.current.dirtyFields,e,[]))),Ce({isDirty:!G(Object.assign(Object.assign({},$e()),{[e]:r}),W.current)}))}!g&&F(J.current,e,r)},[De,xe,Te]),We=e=>U.current||O.current.has(e)||O.current.has((e.match(/\w+/)||[])[0]),Me=e=>{let r=!0;if(!H(j.current))for(const t in j.current)e&&j.current[t].size&&!j.current[t].has(e)&&!j.current[t].has(ae(e))||(w.current[t](),r=!1);return r};function Pe(e){if(!g){let r=de(e);for(const e of te.current)R(e)&&!r[e]&&(r=Object.assign(Object.assign({},r),{[e]:[]}));return r}return e}function $e(e){if(Q(e))return P(m,e,J);if(Array.isArray(e)){const r={};for(const t of e)F(r,t,P(m,t,J));return r}return Pe(X(m,de(J.current),g))}z.current=z.current?z.current:async({type:e,target:r})=>{let t=r.name;const s=m.current[t];let c,u;if(s){const i=e===n,a=ie(Object.assign({isBlurEvent:i,isReValidateOnChange:Fe,isReValidateOnBlur:je,isTouched:!!S(Re.current.touched,t),isSubmitted:Re.current.isSubmitted},ne.current));let o=De(t,!1),l=!H(o)||!i&&We(t);if(i&&!S(Re.current.touched,t)&&Ae.current.touched&&(F(Re.current.touched,t,!0),o=Object.assign(Object.assign({},o),{touched:Re.current.touched})),!g&&N(r)&&F(J.current,t,P(m,t)),a)return!i&&Me(t),(!H(o)||l&&H(o))&&Ce(o);if(ke(),re.current){const{errors:e}=await re.current($e(),Z.current,ve),n=Re.current.isValid;if(c=S(e,t),N(r)&&!c&&re.current){const r=ae(t),n=S(e,r,{});n.type&&n.message&&(c=n),r&&(n||S(Re.current.errors,r))&&(t=r)}u=H(e),n!==u&&(l=!0)}else c=(await se(m,ve,s,J))[t];!i&&Me(t),Se(t,c,l,o,u)}};const He=r.useCallback(async(e={})=>{const r=H(m.current)?W.current:{},{errors:t}=await re.current(Object.assign(Object.assign(Object.assign({},r),$e()),e),Z.current,ve)||{},n=H(t);Re.current.isValid!==n&&Ce({isValid:n})},[ve]),Ue=r.useCallback((e,r)=>{!function(e,r,t,n,s,c){const{ref:u,ref:{name:i}}=t,a=e.current[i];if(!s){const r=P(e,i,n);!k(r)&&F(n.current,i,r)}u.type&&a?E(u)||N(u)?Array.isArray(a.options)&&a.options.length?(V(a.options).forEach((e={},t)=>{($(e.ref)&&I(e,e.ref)||c)&&(x(e.ref,r),q(a.options,`[${t}]`))}),a.options&&!V(a.options).length&&delete e.current[i]):delete e.current[i]:($(u)&&I(a,u)||c)&&(x(u,r),delete e.current[i]):delete e.current[i]}(m,z.current,e,J,g,r),g&&(q(L.current,e.ref.name),q(D.current,e.ref.name))},[g]),qe=r.useCallback(e=>{if(U.current)Ce();else{for(const r of O.current)if(r.startsWith(e)){Ce();break}Me(e)}},[]),Ie=r.useCallback((e,r)=>{e&&(Ue(e,r),g&&!V(e.options||[]).length&&(q(Re.current.errors,e.ref.name),F(Re.current.dirtyFields,e.ref.name,!0),Ce({isDirty:we()}),Ae.current.isValid&&re.current&&He(),qe(e.ref.name)))},[He,Ue]),_e=r.useCallback((e,r,t)=>{const n=t?j.current[t]:O.current;let s=X(m,de(J.current),g,!1,e);if(Q(e)){const t=ae(e)||e;return te.current.has(t)&&(s=Object.assign(Object.assign({},v.current),s)),ue(s,e,n,k(S(W.current,e))?r:S(W.current,e),!0)}const c=k(r)?W.current:r;return Array.isArray(e)?e.reduce((e,r)=>Object.assign(Object.assign({},e),{[r]:ue(s,r,n,c)}),{}):(U.current=k(t),C(!H(s)&&s||c))},[]);function ze(e,r={}){const{name:u,type:i,value:a}=e,o=Object.assign({ref:e},r),l=m.current,f=ge(e),d=le(te.current,u),y=r=>fe&&(!t(e)||r===e);let b,h=l[u],v=!0;if(h&&(f?Array.isArray(h.options)&&V(h.options).find(e=>a===e.ref.value&&y(e.ref)):y(h.ref)))return void(l[u]=Object.assign(Object.assign({},h),r));h=i?f?Object.assign({options:[...V(h&&h.options||[]),{ref:e}],ref:{type:i,name:u}},r):Object.assign({},o):o,l[u]=h;const p=k(S(J.current,u));H(W.current)&&p||(b=S(p?W.current:J.current,u),v=k(b),v||d||xe(u,b)),H(r)||(F(D.current,u,!0),!oe&&Ae.current.isValid&&se(m,ve,h,J).then(e=>{const r=Re.current.isValid;H(e)?F(L.current,u,!0):q(L.current,u),r!==H(e)&&Ce()})),!g||d&&v||!d&&q(Re.current.dirtyFields,u),i&&function({ref:e},r,u){t(e)&&u&&(e.addEventListener(r?s:c,u),e.addEventListener(n,u))}(f&&h.options?h.options[h.options.length-1]:h,f||"select-one"===e.type,z.current)}const Ge=r.useCallback((e,r)=>async t=>{t&&t.preventDefault&&(t.preventDefault(),t.persist());let n={},s=Pe(X(m,de(J.current),g,!0));Ae.current.isSubmitting&&Ce({isSubmitting:!0});try{if(re.current){const{errors:e,values:r}=await re.current(s,Z.current,ve);Re.current.errors=n=e,s=r}else for(const e of Object.values(m.current))if(e){const{name:r}=e.ref,t=await se(m,ve,e,J);t[r]?(F(n,r,t[r]),q(L.current,r)):S(D.current,r)&&(q(Re.current.errors,r),F(L.current,r,!0))}H(n)&&Object.keys(Re.current.errors).every(e=>e in m.current)?(Ce({errors:{},isSubmitting:!0}),await e(s,t)):(Re.current.errors=Object.assign(Object.assign({},Re.current.errors),n),r&&await r(Re.current.errors,t),y&&((e,r)=>{for(const t in e)if(S(r,t)){const r=e[t];if(r){if(r.ref.focus&&k(r.ref.focus()))break;if(r.options){r.options[0].ref.focus();break}}}})(m.current,Re.current.errors))}finally{Re.current.isSubmitting=!1,Ce({isSubmitted:!0,isSubmitting:!1,isSubmitSuccessful:H(Re.current.errors),submitCount:Re.current.submitCount+1})}},[y,ve]);r.useEffect(()=>{o&&Ae.current.isValid&&He(),Ve.current=Ve.current||!fe?Ve.current:function(e,r){const t=new MutationObserver(()=>{for(const t of Object.values(e.current))if(t&&t.options)for(const e of t.options)e&&e.ref&&$(e.ref)&&r(t);else t&&$(t.ref)&&r(t)});return t.observe(window.document,{childList:!0,subtree:!0}),t}(m,Ie)},[Ie,W.current]),r.useEffect(()=>()=>{Ve.current&&Ve.current.disconnect(),M.current=!0,Object.values(m.current).forEach(e=>Ie(e,!0))},[]),!o&&Ae.current.isValid&&(pe.isValid=G(L.current,D.current)&&H(Re.current.errors));const Je={trigger:Ne,setValue:r.useCallback((function(e,r,t){Le(e,r,t||{}),We(e)&&Ce(),Me(e)}),[Le,Ne]),getValues:r.useCallback($e,[]),register:r.useCallback((function(e,r){if(!be)if(Q(e))ze({name:e},r);else{if(!A(e)||!("name"in e))return r=>r&&ze(r,e);ze(e,r)}}),[W.current]),unregister:r.useCallback((function(e){for(const r of Array.isArray(e)?e:[e])Ie(m.current[r],!0)}),[]),formState:me?new Proxy(pe,{get:(e,r)=>{if(r in e)return Ae.current[r]=!0,e[r]}}):pe},Ke=r.useMemo(()=>Object.assign({isFormDirty:we,updateWatchedValue:qe,shouldUnregister:g,updateFormState:Ce,removeFieldEventListener:Ue,watchInternal:_e,mode:ne.current,reValidateMode:{isReValidateOnBlur:je,isReValidateOnChange:Fe},validateResolver:o?He:void 0,fieldsRef:m,resetFieldArrayFunctionRef:Y,useWatchFieldsRef:j,useWatchRenderFunctionsRef:w,fieldArrayDefaultValuesRef:h,validFieldsRef:L,fieldsWithValidationRef:D,fieldArrayNamesRef:te,readFormStateRef:Ae,formStateRef:Re,defaultValuesRef:W,shallowFieldsStateRef:J,fieldArrayValuesRef:v},Je),[W.current,qe,g,Ue,_e]);return Object.assign({watch:function(e,r){return _e(e,r)},control:Ke,handleSubmit:Ge,reset:r.useCallback((e,r={})=>{if(fe)for(const e of Object.values(m.current))if(e){const{ref:r,options:n}=e,s=ge(r)&&Array.isArray(n)?n[0].ref:r;if(t(s))try{s.closest("form").reset();break}catch(e){}}m.current={},W.current=Object.assign({},e||W.current),e&&Me(""),Object.values(Y.current).forEach(e=>ee(e)&&e()),J.current=g?{}:de(e||W.current),(({errors:e,isDirty:r,isSubmitted:t,touched:n,isValid:s,submitCount:c,dirtyFields:u})=>{s||(L.current={},D.current={}),h.current={},O.current=new Set,U.current=!1,Ce({submitCount:c?Re.current.submitCount:0,isDirty:!!r&&Re.current.isDirty,isSubmitted:!!t&&Re.current.isSubmitted,isValid:!!s&&Re.current.isValid,dirtyFields:u?Re.current.dirtyFields:{},touched:n?Re.current.touched:{},errors:e?Re.current.errors:{},isSubmitting:!1,isSubmitSuccessful:!1})})(r)},[]),clearErrors:r.useCallback((function(e){e&&(Array.isArray(e)?e:[e]).forEach(e=>m.current[e]&&R(e)?delete Re.current.errors[e]:q(Re.current.errors,e)),Ce({errors:e?Re.current.errors:{}})}),[]),setError:r.useCallback((function(e,r){const t=(m.current[e]||{}).ref;F(Re.current.errors,e,Object.assign(Object.assign({},r),{ref:t})),Ce({isValid:!1}),r.shouldFocus&&t&&t.focus&&t.focus()}),[]),errors:pe.errors},Je)},e.useFormContext=pe,e.useWatch=function({control:e,name:t,defaultValue:n}){const s=pe(),{useWatchFieldsRef:c,useWatchRenderFunctionsRef:u,watchInternal:i,defaultValuesRef:a}=e||s.control,o=r.useState()[1],l=r.useRef(),f=r.useRef(n);return r.useEffect(()=>{const e=l.current=Oe(),r=u.current,n=c.current;return n[e]=new Set,r[e]=()=>o({}),i(t,f.current,e),()=>{delete n[e],delete r[e]}},[t,u,c,i,f]),l.current?i(t,f.current,l.current):k(n)?Q(t)?S(a.current,t):Array.isArray(t)?t.reduce((e,r)=>Object.assign(Object.assign({},e),{[r]:S(a.current,r)}),{}):a.current:n},Object.defineProperty(e,"__esModule",{value:!0})}));
  //# sourceMappingURL=index.umd.production.min.js.map
  