import { r as reactExports } from "./vendor-react-DYNCg6N5.js";
import { bS as useUpdateUserInfoMutation, c as useAppSelector, bT as isAdmin, t as extractErrorCode, bU as ErrorsEnum, v as generateErrorMessage, a6 as clearApiCacheByTags, ae as usersApi, ay as userApi } from "./index-DfebxkL9.js";
import { t } from "./vendor-i18n-wisIXUFi.js";
import { s as showNotification } from "./vendor-mantine-h0WlM2Mj.js";
var UsersActionTableEnum = /* @__PURE__ */ ((UsersActionTableEnum2) => {
  UsersActionTableEnum2["role"] = "role";
  UsersActionTableEnum2["department"] = "department";
  UsersActionTableEnum2["status"] = "status";
  UsersActionTableEnum2["sendActivationLinkAgain"] = "send-activation-link-again";
  UsersActionTableEnum2["writeLetter"] = "write-letter";
  UsersActionTableEnum2["removedUser"] = "removed-user";
  UsersActionTableEnum2["export"] = "export";
  UsersActionTableEnum2["addRoles"] = "add-roles";
  UsersActionTableEnum2["resetPassword"] = "reset-password";
  UsersActionTableEnum2["delete"] = "delete";
  UsersActionTableEnum2["filial"] = "filial";
  return UsersActionTableEnum2;
})(UsersActionTableEnum || {});
var ProfileUpdateTypeEnum = /* @__PURE__ */ ((ProfileUpdateTypeEnum2) => {
  ProfileUpdateTypeEnum2[ProfileUpdateTypeEnum2["None"] = 0] = "None";
  ProfileUpdateTypeEnum2[ProfileUpdateTypeEnum2["Credentials"] = 1] = "Credentials";
  ProfileUpdateTypeEnum2[ProfileUpdateTypeEnum2["Common"] = 2] = "Common";
  ProfileUpdateTypeEnum2[ProfileUpdateTypeEnum2["Customer"] = 3] = "Customer";
  ProfileUpdateTypeEnum2[ProfileUpdateTypeEnum2["Positions"] = 4] = "Positions";
  ProfileUpdateTypeEnum2[ProfileUpdateTypeEnum2["Roles"] = 5] = "Roles";
  ProfileUpdateTypeEnum2[ProfileUpdateTypeEnum2["Quota"] = 6] = "Quota";
  ProfileUpdateTypeEnum2[ProfileUpdateTypeEnum2["Status"] = 7] = "Status";
  ProfileUpdateTypeEnum2[ProfileUpdateTypeEnum2["Email"] = 8] = "Email";
  ProfileUpdateTypeEnum2[ProfileUpdateTypeEnum2["Avatar"] = 9] = "Avatar";
  ProfileUpdateTypeEnum2[ProfileUpdateTypeEnum2["CredentialEmail"] = 10] = "CredentialEmail";
  ProfileUpdateTypeEnum2[ProfileUpdateTypeEnum2["AdditionalEmail"] = 11] = "AdditionalEmail";
  ProfileUpdateTypeEnum2[ProfileUpdateTypeEnum2["TimeZone"] = 12] = "TimeZone";
  ProfileUpdateTypeEnum2[ProfileUpdateTypeEnum2["Language"] = 13] = "Language";
  return ProfileUpdateTypeEnum2;
})(ProfileUpdateTypeEnum || {});
var ProfileSuccessMessageEnum = /* @__PURE__ */ ((ProfileSuccessMessageEnum2) => {
  ProfileSuccessMessageEnum2[ProfileSuccessMessageEnum2["credentialEmailUpdate"] = 10] = "credentialEmailUpdate";
  return ProfileSuccessMessageEnum2;
})(ProfileSuccessMessageEnum || {});
var ProfileSectionEnum = /* @__PURE__ */ ((ProfileSectionEnum2) => {
  ProfileSectionEnum2["None"] = "None";
  ProfileSectionEnum2["Credentials"] = "Credentials";
  ProfileSectionEnum2["Common"] = "Common";
  ProfileSectionEnum2["Customer"] = "Customer";
  ProfileSectionEnum2["Positions"] = "Positions";
  ProfileSectionEnum2["Roles"] = "Roles";
  ProfileSectionEnum2["Quota"] = "Quota";
  ProfileSectionEnum2["Status"] = "Status";
  ProfileSectionEnum2["LinkedEmail"] = "LinkedEmail";
  ProfileSectionEnum2["Avatar"] = "Avatar";
  ProfileSectionEnum2["CredentialEmail"] = "CredentialEmail";
  ProfileSectionEnum2["TimeZone"] = "TimeZone";
  ProfileSectionEnum2["Language"] = "Culture";
  return ProfileSectionEnum2;
})(ProfileSectionEnum || {});
const getProfileSuccessMessage = (updateType) => {
  if (updateType in ProfileSuccessMessageEnum) {
    return t(`profile.success.${ProfileSuccessMessageEnum[updateType]}`);
  } else {
    return t("saved");
  }
};
const useUpdateInfo = ({
  isSilent = false
} = {}) => {
  const [updateInfo, {
    isSuccess,
    isLoading,
    isError
  }] = useUpdateUserInfoMutation();
  const currentUser = useAppSelector((state) => state.userInfo.User);
  const isLimited = !isAdmin(currentUser);
  const saveUpdate = reactExports.useCallback(async ({
    Type,
    Data
  }) => {
    try {
      const res = await updateInfo({
        Type,
        Data
      });
      if ("error" in res) {
        const codeError = extractErrorCode(res.error);
        showNotification({
          title: codeError === ErrorsEnum.NotConfiguredCustomersEmail && isLimited ? t("profile.error.userAddEmail") : generateErrorMessage(codeError),
          message: "",
          autoClose: true,
          color: "red"
        });
      } else {
        clearApiCacheByTags(usersApi, ["Users"]);
        if ((currentUser == null ? void 0 : currentUser.Id) === Data.Id) {
          clearApiCacheByTags(userApi, ["CurrentUser"]);
        }
        if (!isSilent) {
          showNotification({
            title: isAdmin(currentUser) && Type == ProfileSuccessMessageEnum.credentialEmailUpdate ? t("saved") : getProfileSuccessMessage(Type),
            message: "",
            autoClose: true,
            color: "green"
          });
        }
      }
    } catch (err) {
      console.warn(t("profile.savedMessageError"), err);
    }
  }, []);
  return {
    saveUpdate,
    isError,
    isLoading,
    isSuccess
  };
};
export {
  ProfileSectionEnum as P,
  UsersActionTableEnum as U,
  ProfileUpdateTypeEnum as a,
  useUpdateInfo as u
};
//# sourceMappingURL=useUpdateUser-innD962A.js.map
