"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const react = require("react");
const useForm = (initialValues, handler) => {
  const [values, setValues] = react.useState(initialValues);
  const [errors, setErrors] = react.useState({});
  const setError = react.useCallback((field, error) => {
    setErrors((errors2) => ({ ...errors2, [field]: error }));
  }, []);
  const [isSubmitting, setSubmitting] = react.useState(false);
  return {
    values,
    setValues,
    errors,
    isSubmitting,
    onSubmit: react.useCallback(async (e) => {
      e.preventDefault();
      setSubmitting(true);
      setErrors({});
      await handler?.(values, setError, setValues);
      setSubmitting(false);
    }, [handler, setError, values]),
    register: react.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]
    )
  };
};
exports.useForm = useForm;
//# sourceMappingURL=useForm.cjs.map
