import{r as e}from"./chunk-QTnfLwEv.js";import{o as t}from"./jsx-runtime-DHSfUA5-.js";var n=e(t(),1);function r(e,t){let n={...t};for(let t of e)n[t.name]===void 0&&(n[t.name]=i(t));return n}function i(e){if(e.type===`checkbox`)return!1;if(e.type!==`file`)return``}function a(e){return typeof e==`string`?!e.trim():e==null||e===!1}function o({fields:e,initialValues:t,requiredMessage:i=e=>`${e.label} is required.`,onChange:o}){let[l,u]=(0,n.useState)(()=>r(e,t)),[d,f]=(0,n.useState)({}),p=(0,n.useCallback)((t,n)=>{let r=e.find(e=>e.name===t);f(e=>e[t]?{...e,[t]:void 0}:e),u(e=>{let i={...e,[t]:n};return r&&o?.(i,r),i})},[o,e]),m=(0,n.useCallback)(e=>{u(t=>({...t,...e}))},[]),h=(0,n.useCallback)((e,t)=>{f(n=>({...n,[e]:t}))},[]),g=(0,n.useCallback)(()=>{f({})},[]),_=(0,n.useCallback)(n=>{u(r(e,n??t)),f({})},[t,e]),v=(0,n.useCallback)(()=>{let t={};for(let n of e){let e=l[n.name];if(n.required&&a(e)){t[n.name]=i(n);continue}let r=n.validate?.(e,l);if(r){t[n.name]=r;continue}n.type===`file`&&n.maxSize&&s(e)&&e.size>n.maxSize&&(t[n.name]=n.maxSizeMessage?n.maxSizeMessage(n,n.maxSize):`${n.label} must be no larger than ${c(n.maxSize)}.`)}return f(t),Object.values(t).every(e=>!e)},[e,i,l]);return{fields:(0,n.useMemo)(()=>e.map(e=>({...e,value:l[e.name],error:d[e.name],onChange:t=>p(e.name,t)})),[d,e,p,l]),values:l,errors:d,setValue:p,setValues:m,setError:h,verify:v,varify:v,clearErrors:g,reset:_}}function s(e){return typeof File<`u`&&e instanceof File}function c(e){let t=[`B`,`KB`,`MB`,`GB`],n=e,r=0;for(;n>=1024&&r<t.length-1;)n/=1024,r+=1;return`${Number.isInteger(n)?n:n.toFixed(1)}${t[r]}`}export{o as t};
//# sourceMappingURL=useForm-YVjhCM4e.js.map