"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const ui = require("@contember/ui");
const react = require("react");
const changePasswords = require("../../mutations/password/changePasswords.cjs");
const useForm = require("../../lib/useForm.cjs");
const initialValues = {
  currentPassword: "",
  newPassword: "",
  newPasswordAgain: ""
};
const ChangePassword = ({}) => {
  const changePassword = changePasswords.useChangePassword();
  const addToast = ui.useShowToast();
  const { register, errors, isSubmitting, onSubmit } = useForm.useForm(initialValues, react.useCallback(async (val, setError, setValues) => {
    if (val.newPassword !== val.newPasswordAgain) {
      return setError("newPasswordAgain", "Password confirmation doesn't match");
    }
    const response = await changePassword({ currentPassword: val.currentPassword, newPassword: val.newPassword });
    if (response.ok) {
      addToast({
        message: "Password changed",
        type: "success",
        dismiss: true
      });
      setValues(initialValues);
    } else {
      switch (response.error.code) {
        case "INVALID_PASSWORD":
          return setError("currentPassword", "Current password is invalid");
        case "TOO_WEAK":
          return setError("newPassword", "Password is too weak");
        default:
          return addToast({
            message: "Unknown error",
            type: "error",
            dismiss: true
          });
      }
    }
  }, [addToast, changePassword]));
  return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
    /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { depth: 3, children: "Change password" }),
    /* @__PURE__ */ jsxRuntime.jsx("form", { onSubmit, children: /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { gap: "large", children: [
      /* @__PURE__ */ jsxRuntime.jsx(
        ui.FieldContainer,
        {
          label: "Current password",
          errors: errors.currentPassword === void 0 ? void 0 : [{ message: errors.currentPassword }],
          children: /* @__PURE__ */ jsxRuntime.jsx(ui.PasswordInput, { autoComplete: "password", ...register("currentPassword") })
        }
      ),
      /* @__PURE__ */ jsxRuntime.jsx(
        ui.FieldContainer,
        {
          label: "New password",
          errors: errors.newPassword === void 0 ? void 0 : [{ message: errors.newPassword }],
          children: /* @__PURE__ */ jsxRuntime.jsx(ui.PasswordInput, { autoComplete: "new-password", ...register("newPassword") })
        }
      ),
      /* @__PURE__ */ jsxRuntime.jsx(
        ui.FieldContainer,
        {
          label: "Confirm new password",
          errors: errors.newPasswordAgain === void 0 ? void 0 : [{ message: errors.newPasswordAgain }],
          children: /* @__PURE__ */ jsxRuntime.jsx(ui.PasswordInput, { autoComplete: "new-password", ...register("newPasswordAgain") })
        }
      ),
      /* @__PURE__ */ jsxRuntime.jsx(ui.Spacer, {}),
      /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { intent: "primary", type: "submit", disabled: isSubmitting, children: "Change password" })
    ] }) })
  ] });
};
exports.ChangePassword = ChangePassword;
//# sourceMappingURL=ChangePassword.cjs.map
