"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxDevRuntime = require("react/jsx-dev-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__ */ jsxDevRuntime.jsxDEV(jsxDevRuntime.Fragment, { children: [
    /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Heading, { depth: 3, children: "Change password" }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/password/ChangePassword.tsx",
      lineNumber: 47,
      columnNumber: 3
    }, void 0),
    /* @__PURE__ */ jsxDevRuntime.jsxDEV("form", { onSubmit, children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Stack, { gap: "large", children: [
      /* @__PURE__ */ jsxDevRuntime.jsxDEV(
        ui.FieldContainer,
        {
          label: "Current password",
          errors: errors.currentPassword === void 0 ? void 0 : [{ message: errors.currentPassword }],
          children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.PasswordInput, { autoComplete: "password", ...register("currentPassword") }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/tenant/components/password/ChangePassword.tsx",
            lineNumber: 54,
            columnNumber: 6
          }, void 0)
        },
        void 0,
        false,
        {
          fileName: "/__w/interface/interface/packages/admin/src/tenant/components/password/ChangePassword.tsx",
          lineNumber: 50,
          columnNumber: 5
        },
        void 0
      ),
      /* @__PURE__ */ jsxDevRuntime.jsxDEV(
        ui.FieldContainer,
        {
          label: "New password",
          errors: errors.newPassword === void 0 ? void 0 : [{ message: errors.newPassword }],
          children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.PasswordInput, { autoComplete: "new-password", ...register("newPassword") }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/tenant/components/password/ChangePassword.tsx",
            lineNumber: 60,
            columnNumber: 6
          }, void 0)
        },
        void 0,
        false,
        {
          fileName: "/__w/interface/interface/packages/admin/src/tenant/components/password/ChangePassword.tsx",
          lineNumber: 56,
          columnNumber: 5
        },
        void 0
      ),
      /* @__PURE__ */ jsxDevRuntime.jsxDEV(
        ui.FieldContainer,
        {
          label: "Confirm new password",
          errors: errors.newPasswordAgain === void 0 ? void 0 : [{ message: errors.newPasswordAgain }],
          children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.PasswordInput, { autoComplete: "new-password", ...register("newPasswordAgain") }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/tenant/components/password/ChangePassword.tsx",
            lineNumber: 66,
            columnNumber: 6
          }, void 0)
        },
        void 0,
        false,
        {
          fileName: "/__w/interface/interface/packages/admin/src/tenant/components/password/ChangePassword.tsx",
          lineNumber: 62,
          columnNumber: 5
        },
        void 0
      ),
      /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Spacer, {}, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/tenant/components/password/ChangePassword.tsx",
        lineNumber: 68,
        columnNumber: 5
      }, void 0),
      /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Button, { intent: "primary", type: "submit", disabled: isSubmitting, children: "Change password" }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/tenant/components/password/ChangePassword.tsx",
        lineNumber: 69,
        columnNumber: 5
      }, void 0)
    ] }, void 0, true, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/password/ChangePassword.tsx",
      lineNumber: 49,
      columnNumber: 4
    }, void 0) }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/password/ChangePassword.tsx",
      lineNumber: 48,
      columnNumber: 3
    }, void 0)
  ] }, void 0, true, {
    fileName: "/__w/interface/interface/packages/admin/src/tenant/components/password/ChangePassword.tsx",
    lineNumber: 46,
    columnNumber: 9
  }, void 0);
};
exports.ChangePassword = ChangePassword;
//# sourceMappingURL=ChangePassword.cjs.map
