import { jsx, jsxs } from "react/jsx-runtime";
import { g as get, s as set, a as appendErrors, b as useForm, F as FormProvider } from "../../index.esm-BuArIOt4.js";
import { forwardRef, useEffect, useCallback } from "react";
import { Button } from "../Button/Button.js";
import { ButtonGroup } from "../Button/ButtonGroup.js";
import { cn } from "../classnames.js";
const s = (e, s2, o2) => {
  if (e && "reportValidity" in e) {
    const r2 = get(o2, s2);
    e.setCustomValidity(r2 && r2.message || ""), e.reportValidity();
  }
}, o = (t2, e) => {
  for (const o2 in e.fields) {
    const r2 = e.fields[o2];
    r2 && r2.ref && "reportValidity" in r2.ref ? s(r2.ref, o2, t2) : r2.refs && r2.refs.forEach((e2) => s(e2, o2, t2));
  }
}, r = (s2, r2) => {
  r2.shouldUseNativeValidation && o(s2, r2);
  const f = {};
  for (const o2 in s2) {
    const n2 = get(r2.fields, o2), a = Object.assign(s2[o2] || {}, { ref: n2 && n2.ref });
    if (i(r2.names || Object.keys(s2), o2)) {
      const s3 = Object.assign({}, get(f, o2));
      set(s3, "root", a), set(f, o2, s3);
    } else set(f, o2, a);
  }
  return f;
}, i = (t2, e) => t2.some((t3) => t3.startsWith(e + "."));
var n = function(r2, e) {
  for (var n2 = {}; r2.length; ) {
    var t2 = r2[0], s2 = t2.code, i2 = t2.message, a = t2.path.join(".");
    if (!n2[a]) if ("unionErrors" in t2) {
      var u = t2.unionErrors[0].errors[0];
      n2[a] = { message: u.message, type: u.code };
    } else n2[a] = { message: i2, type: s2 };
    if ("unionErrors" in t2 && t2.unionErrors.forEach(function(e2) {
      return e2.errors.forEach(function(e3) {
        return r2.push(e3);
      });
    }), e) {
      var c = n2[a].types, f = c && c[t2.code];
      n2[a] = appendErrors(a, e, n2, s2, f ? [].concat(f, t2.message) : t2.message);
    }
    r2.shift();
  }
  return n2;
}, t = function(o$1, t2, s2) {
  return void 0 === s2 && (s2 = {}), function(i2, a, u) {
    try {
      return Promise.resolve(function(e, n2) {
        try {
          var a2 = Promise.resolve(o$1["sync" === s2.mode ? "parse" : "parseAsync"](i2, t2)).then(function(e2) {
            return u.shouldUseNativeValidation && o({}, u), { errors: {}, values: s2.raw ? i2 : e2 };
          });
        } catch (r2) {
          return n2(r2);
        }
        return a2 && a2.then ? a2.then(void 0, n2) : a2;
      }(0, function(r$1) {
        if (function(r2) {
          return Array.isArray(null == r2 ? void 0 : r2.errors);
        }(r$1)) return { values: {}, errors: r(n(r$1.errors, !u.shouldUseNativeValidation && "all" === u.criteriaMode), u) };
        throw r$1;
      }));
    } catch (r2) {
      return Promise.reject(r2);
    }
  };
};
const Form = forwardRef(function Form2({
  cancelButtonLabel = "Cancel",
  children,
  className,
  defaultClassName = "tw-flex tw-flex-col tw-gap-6",
  defaultValues,
  disabled = false,
  loading,
  onCancel: originalOnCancel,
  onSubmit: originalOnSubmit,
  schema,
  submitButtonLabel = "Save"
}, ref) {
  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 onCancel = useCallback(() => {
    originalOnCancel == null ? void 0 : originalOnCancel({ reset: methods.reset });
  }, [originalOnCancel, methods.reset]);
  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: cn(defaultClassName, className),
      onSubmit: handleSubmit(onSubmit),
      ref,
      children: [
        typeof children === "function" ? children({ context: methods }) : children,
        /* @__PURE__ */ jsxs(ButtonGroup, { 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
