import { jsxs, Fragment, jsx } from "react/jsx-runtime";
import { useShowToast, Heading, Stack, FieldContainer, PasswordInput, Spacer, Button } from "@contember/ui";
import { useCallback } from "react";
import { useChangePassword } from "../../mutations/password/changePasswords.js";
import { useForm } from "../../lib/useForm.js";
const initialValues = {
  currentPassword: "",
  newPassword: "",
  newPasswordAgain: ""
};
const ChangePassword = ({}) => {
  const changePassword = useChangePassword();
  const addToast = useShowToast();
  const { register, errors, isSubmitting, onSubmit } = useForm(initialValues, 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__ */ jsxs(Fragment, { children: [
    /* @__PURE__ */ jsx(Heading, { depth: 3, children: "Change password" }),
    /* @__PURE__ */ jsx("form", { onSubmit, children: /* @__PURE__ */ jsxs(Stack, { gap: "large", children: [
      /* @__PURE__ */ jsx(
        FieldContainer,
        {
          label: "Current password",
          errors: errors.currentPassword === void 0 ? void 0 : [{ message: errors.currentPassword }],
          children: /* @__PURE__ */ jsx(PasswordInput, { autoComplete: "password", ...register("currentPassword") })
        }
      ),
      /* @__PURE__ */ jsx(
        FieldContainer,
        {
          label: "New password",
          errors: errors.newPassword === void 0 ? void 0 : [{ message: errors.newPassword }],
          children: /* @__PURE__ */ jsx(PasswordInput, { autoComplete: "new-password", ...register("newPassword") })
        }
      ),
      /* @__PURE__ */ jsx(
        FieldContainer,
        {
          label: "Confirm new password",
          errors: errors.newPasswordAgain === void 0 ? void 0 : [{ message: errors.newPasswordAgain }],
          children: /* @__PURE__ */ jsx(PasswordInput, { autoComplete: "new-password", ...register("newPasswordAgain") })
        }
      ),
      /* @__PURE__ */ jsx(Spacer, {}),
      /* @__PURE__ */ jsx(Button, { intent: "primary", type: "submit", disabled: isSubmitting, children: "Change password" })
    ] }) })
  ] });
};
export {
  ChangePassword
};
//# sourceMappingURL=ChangePassword.js.map
