import { jsx, jsxs } from "react/jsx-runtime";
import { useEffect } from "react";
import { g as get, a as appendErrors, b as useForm, F as FormProvider } from "../../index.esm-D7jRhDaG.js";
import { Button } from "../Button/Button.js";
import { ButtonGroup } from "../Button/ButtonGroup.js";
var t$1 = function(t2, n2, e2) {
  if (t2 && "reportValidity" in t2) {
    var i2 = get(e2, n2);
    t2.setCustomValidity(i2 && i2.message || ""), t2.reportValidity();
  }
}, n$1 = function(r, n2) {
  var e2 = function(e3) {
    var i3 = n2.fields[e3];
    i3 && i3.ref && "reportValidity" in i3.ref ? t$1(i3.ref, e3, r) : i3.refs && i3.refs.forEach(function(n3) {
      return t$1(n3, e3, r);
    });
  };
  for (var i2 in n2.fields) e2(i2);
}, e = function(r) {
  return r instanceof Date;
}, i = function(r) {
  return null == r;
}, a = function(r) {
  return "object" == typeof r;
}, o = function(r) {
  return !i(r) && !Array.isArray(r) && a(r) && !e(r);
}, f = function(r) {
  return /^\w*$/.test(r);
}, s = function(r, t2, n2) {
  for (var e2 = -1, i2 = f(t2) ? [t2] : function(r2) {
    return t3 = r2.replace(/["|']|\]/g, "").split(/\.|\[/), Array.isArray(t3) ? t3.filter(Boolean) : [];
    var t3;
  }(t2), a2 = i2.length, s2 = a2 - 1; ++e2 < a2; ) {
    var u2 = i2[e2], c2 = n2;
    if (e2 !== s2) {
      var l = r[u2];
      c2 = o(l) || Array.isArray(l) ? l : isNaN(+i2[e2 + 1]) ? {} : [];
    }
    r[u2] = c2, r = r[u2];
  }
  return r;
}, u = function(t2, e2) {
  e2.shouldUseNativeValidation && n$1(t2, e2);
  var i2 = {};
  for (var a2 in t2) {
    var o2 = get(e2.fields, a2), f2 = Object.assign(t2[a2] || {}, { ref: o2 && o2.ref });
    if (c(e2.names || Object.keys(t2), a2)) {
      var u2 = Object.assign({}, get(i2, a2));
      s(u2, "root", f2), s(i2, a2, u2);
    } else s(i2, a2, f2);
  }
  return i2;
}, c = function(r, t2) {
  return r.some(function(r2) {
    return r2.startsWith(t2 + ".");
  });
};
var n = function(e2, o2) {
  for (var n2 = {}; e2.length; ) {
    var t2 = e2[0], s2 = t2.code, i2 = t2.message, a2 = t2.path.join(".");
    if (!n2[a2]) if ("unionErrors" in t2) {
      var u2 = t2.unionErrors[0].errors[0];
      n2[a2] = { message: u2.message, type: u2.code };
    } else n2[a2] = { message: i2, type: s2 };
    if ("unionErrors" in t2 && t2.unionErrors.forEach(function(r) {
      return r.errors.forEach(function(r2) {
        return e2.push(r2);
      });
    }), o2) {
      var c2 = n2[a2].types, f2 = c2 && c2[t2.code];
      n2[a2] = appendErrors(a2, o2, n2, s2, f2 ? [].concat(f2, t2.message) : t2.message);
    }
    e2.shift();
  }
  return n2;
}, t = function(r, t2, s2) {
  return void 0 === s2 && (s2 = {}), function(i2, a2, u$1) {
    try {
      return Promise.resolve(function(o2, n2) {
        try {
          var a3 = Promise.resolve(r["sync" === s2.mode ? "parse" : "parseAsync"](i2, t2)).then(function(r2) {
            return u$1.shouldUseNativeValidation && n$1({}, u$1), { errors: {}, values: s2.raw ? i2 : r2 };
          });
        } catch (r2) {
          return n2(r2);
        }
        return a3 && a3.then ? a3.then(void 0, n2) : a3;
      }(0, function(r2) {
        if (function(r3) {
          return null != r3.errors;
        }(r2)) return { values: {}, errors: u(n(r2.errors, !u$1.shouldUseNativeValidation && "all" === u$1.criteriaMode), u$1) };
        throw r2;
      }));
    } catch (r2) {
      return Promise.reject(r2);
    }
  };
};
const Form = ({
  cancelButtonLabel = "Cancel",
  children,
  className,
  defaultValues,
  disabled = false,
  loading,
  onCancel,
  onSubmit: originalOnSubmit,
  schema,
  submitButtonLabel = "Save"
}) => {
  const methods = useForm({
    defaultValues,
    disabled: disabled || loading,
    resolver: schema !== void 0 ? t(schema) : void 0
  });
  const { control, formState, handleSubmit } = methods;
  useEffect(() => {
    control._disableForm(disabled);
  }, [control, disabled]);
  const onSubmit = async (data) => {
    await (originalOnSubmit == null ? void 0 : originalOnSubmit(data));
  };
  const isFormLoading = loading ?? (formState.isLoading || formState.isSubmitting);
  return /* @__PURE__ */ jsx(FormProvider, { ...methods, children: /* @__PURE__ */ jsxs(
    "form",
    {
      className,
      onSubmit: handleSubmit(onSubmit),
      children: [
        typeof children === "function" ? children({ context: methods }) : children,
        /* @__PURE__ */ jsxs(ButtonGroup, { className: "tw-mt-4", children: [
          typeof onSubmit === "function" && /* @__PURE__ */ jsx(
            Button,
            {
              disabled: formState.disabled || formState.isValidating,
              loading: isFormLoading,
              type: "submit",
              children: submitButtonLabel
            }
          ),
          typeof onCancel === "function" && /* @__PURE__ */ jsx(
            Button,
            {
              disabled: formState.disabled || isFormLoading || formState.isValidating,
              level: "secondary",
              onClick: onCancel,
              children: cancelButtonLabel
            }
          )
        ] })
      ]
    }
  ) });
};
export {
  Form
};
//# sourceMappingURL=Form.js.map
