import { useState, useCallback } from "react";
const useForm = (initialValues, handler) => {
  const [values, setValues] = useState(initialValues);
  const [errors, setErrors] = useState({});
  const setError = useCallback((field, error) => {
    setErrors((errors2) => ({ ...errors2, [field]: error }));
  }, []);
  const [isSubmitting, setSubmitting] = useState(false);
  return {
    values,
    setValues,
    errors,
    isSubmitting,
    onSubmit: useCallback(async (e) => {
      e.preventDefault();
      setSubmitting(true);
      setErrors({});
      await handler?.(values, setError, setValues);
      setSubmitting(false);
    }, [handler, setError, values]),
    register: useCallback(
      (field, options = {}) => {
        return {
          name: field,
          value: values[field],
          onChange: (value) => {
            let nextValue = value;
            if (options.type === "number") {
              nextValue = Number(nextValue).toString(10) === nextValue ? Number(nextValue) : void 0;
            }
            setValues((values2) => ({ ...values2, [field]: nextValue }));
          }
        };
      },
      [values]
    )
  };
};
export {
  useForm
};
//# sourceMappingURL=useForm.js.map
