import { r as reactExports, j as jsxRuntimeExports, E as compilerRuntimeExports } from "./vendor-react-DYNCg6N5.js";
import { S as Se } from "./vendor-icons-xWhXK_Ui.js";
import { c as createStyles, s as showNotification, b as Box, T as Text, G as Group, B as Button, V as LoadingOverlay, n as useDisclosure, k as Tooltip, h as ActionIcon } from "./vendor-mantine-h0WlM2Mj.js";
import { q as useLazyVerifyEmailServersQuery, c2 as useLazyEmailActivationQuery, t as extractErrorCode, v as generateErrorMessage, c3 as useCloseseSsionsMutation, m as universalNotification } from "./index-DfebxkL9.js";
import { u as useTranslation, T as Trans } from "./vendor-i18n-wisIXUFi.js";
import { M as Modal } from "./index-DqcA8P_y.js";
import { C as Controls } from "./index-DMTgSMwy.js";
const useStyles = createStyles((theme) => ({
  modalContainer: {
    width: "100%",
    height: "100%",
    display: "flex",
    flexDirection: "column",
    alignItems: "center",
    justifyContent: "space-between",
    background: "white"
  },
  modalContent: {
    textAlign: "start"
  },
  modalControls: {
    background: theme.white,
    width: "100%"
  }
}));
const useVerifyEmailAndOpenModal = () => {
  const {
    t
  } = useTranslation();
  const [getVerifiedList] = useLazyVerifyEmailServersQuery();
  const handleCheckCustomerServerSettings = async (CustomerId, handleOpenModal) => {
    try {
      if (CustomerId) {
        const {
          isSuccess,
          data
        } = await getVerifiedList([CustomerId]);
        if (isSuccess) {
          const currentCustomerSettings = data == null ? void 0 : data.Customers.find((customer) => customer.CustomerId === CustomerId);
          if (currentCustomerSettings && currentCustomerSettings.HasSettings && handleOpenModal) {
            handleOpenModal();
          } else {
            showNotification({
              title: t("requestsErrors.notConfiguredCustomersEmail"),
              message: "",
              autoClose: true,
              color: "orange"
            });
          }
        }
      }
    } catch (error) {
    }
  };
  return {
    handleCheckCustomerServerSettings
  };
};
const useEmailActivation = () => {
  const {
    t
  } = useTranslation();
  const [fetchEmailActivation, {
    isLoading
  }] = useLazyEmailActivationQuery();
  const sendEmailActivation = reactExports.useCallback(async (selection) => {
    const numbersSelection = (selection == null ? void 0 : selection.map((item) => Number(item))) ?? [];
    try {
      const {
        isSuccess,
        isError,
        error
      } = await fetchEmailActivation(numbersSelection);
      if (error && isError) {
        const codeError = extractErrorCode(error);
        showNotification({
          title: t("sendLetter.errors.notSent"),
          message: generateErrorMessage(codeError),
          autoClose: true,
          color: "red"
        });
      }
      if (isSuccess) {
        showNotification({
          title: t(`sendLetter.${(numbersSelection == null ? void 0 : numbersSelection.length) === 1 ? "oneEmailActivationSuccessMessage" : "multipleEmailsActivationSuccessMessage"}`),
          message: "",
          autoClose: true,
          color: "green"
        });
      }
    } catch (err) {
      console.warn("fetchEmailActivation ERROR", err);
    }
  }, []);
  return {
    sendEmailActivation,
    isLoading
  };
};
const OneUserActivationModal = ({
  open,
  selectedUsers,
  email,
  handleClose
}) => {
  const {
    t
  } = useTranslation();
  const {
    sendEmailActivation,
    isLoading: isSendingActivation
  } = useEmailActivation();
  const handleConfirm = reactExports.useCallback(async () => {
    await sendEmailActivation(selectedUsers);
    if (handleClose) {
      handleClose();
    }
  }, [selectedUsers.length]);
  return /* @__PURE__ */ jsxRuntimeExports.jsxs(Modal, { size: "500px", zIndex: 1400, opened: open, onClose: () => handleClose(), title: t("users.actionList.emailConfirmation"), children: [
    /* @__PURE__ */ jsxRuntimeExports.jsx(Box, { p: "24px", children: /* @__PURE__ */ jsxRuntimeExports.jsx(Text, { children: /* @__PURE__ */ jsxRuntimeExports.jsx(Trans, { i18nKey: "sendLetter.sendActivationToEmail", values: {
      email
    } }) }) }),
    /* @__PURE__ */ jsxRuntimeExports.jsx(Controls, { children: /* @__PURE__ */ jsxRuntimeExports.jsx(Group, { grow: true, children: /* @__PURE__ */ jsxRuntimeExports.jsx(Button, { onClick: handleConfirm, children: t("send") }) }) }),
    /* @__PURE__ */ jsxRuntimeExports.jsx(LoadingOverlay, { visible: isSendingActivation, overlayProps: {
      blur: 2
    } })
  ] });
};
const EmailActivationControl = (t0) => {
  const $ = compilerRuntimeExports.c(29);
  const {
    Id,
    Email,
    customerId
  } = t0;
  const {
    t
  } = useTranslation();
  const {
    theme
  } = useStyles();
  const [opened, t1] = useDisclosure(false);
  const {
    open,
    close
  } = t1;
  const {
    handleCheckCustomerServerSettings
  } = useVerifyEmailAndOpenModal();
  let t2;
  if ($[0] !== close) {
    t2 = () => {
      close();
    };
    $[0] = close;
    $[1] = t2;
  } else {
    t2 = $[1];
  }
  const closeModal = t2;
  let t3;
  if ($[2] !== open) {
    t3 = () => {
      open();
    };
    $[2] = open;
    $[3] = t3;
  } else {
    t3 = $[3];
  }
  const handleOpen = t3;
  let t4;
  if ($[4] !== customerId || $[5] !== handleCheckCustomerServerSettings || $[6] !== handleOpen) {
    t4 = async (e) => {
      e.stopPropagation();
      await handleCheckCustomerServerSettings(customerId, handleOpen);
    };
    $[4] = customerId;
    $[5] = handleCheckCustomerServerSettings;
    $[6] = handleOpen;
    $[7] = t4;
  } else {
    t4 = $[7];
  }
  const handleVerifyEmail = t4;
  if (!Id || !Email || !customerId) {
    return null;
  }
  let t5;
  if ($[8] !== theme.colors.brandGray[1] || $[9] !== theme.spacing.xs) {
    t5 = {
      background: theme.colors.brandGray[1],
      borderRadius: theme.spacing.xs,
      padding: "8px"
    };
    $[8] = theme.colors.brandGray[1];
    $[9] = theme.spacing.xs;
    $[10] = t5;
  } else {
    t5 = $[10];
  }
  let t6;
  if ($[11] !== t) {
    t6 = t("users.actionList.confirmEmail");
    $[11] = t;
    $[12] = t6;
  } else {
    t6 = $[12];
  }
  let t7;
  if ($[13] === Symbol.for("react.memo_cache_sentinel")) {
    t7 = /* @__PURE__ */ jsxRuntimeExports.jsx(Se, { size: "20", color: "red", stroke: 1.5, cursor: "pointer" });
    $[13] = t7;
  } else {
    t7 = $[13];
  }
  let t8;
  if ($[14] !== handleVerifyEmail) {
    t8 = /* @__PURE__ */ jsxRuntimeExports.jsx(ActionIcon, { variant: "transparent", onClick: handleVerifyEmail, children: t7 });
    $[14] = handleVerifyEmail;
    $[15] = t8;
  } else {
    t8 = $[15];
  }
  let t9;
  if ($[16] !== t5 || $[17] !== t6 || $[18] !== t8 || $[19] !== theme.colors.dark[8]) {
    t9 = /* @__PURE__ */ jsxRuntimeExports.jsx(Tooltip, { sx: t5, color: "gray", label: t6, zIndex: "1101", c: theme.colors.dark[8], children: t8 });
    $[16] = t5;
    $[17] = t6;
    $[18] = t8;
    $[19] = theme.colors.dark[8];
    $[20] = t9;
  } else {
    t9 = $[20];
  }
  let t10;
  if ($[21] !== Email || $[22] !== Id || $[23] !== closeModal || $[24] !== opened) {
    t10 = opened && /* @__PURE__ */ jsxRuntimeExports.jsx(OneUserActivationModal, { open: opened, handleClose: closeModal, selectedUsers: [Id], email: Email });
    $[21] = Email;
    $[22] = Id;
    $[23] = closeModal;
    $[24] = opened;
    $[25] = t10;
  } else {
    t10 = $[25];
  }
  let t11;
  if ($[26] !== t10 || $[27] !== t9) {
    t11 = /* @__PURE__ */ jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [
      t9,
      t10
    ] });
    $[26] = t10;
    $[27] = t9;
    $[28] = t11;
  } else {
    t11 = $[28];
  }
  return t11;
};
const useTerminateSessions = () => {
  const {
    t
  } = useTranslation();
  const [fetchCloseSessions, {
    isLoading,
    isSuccess,
    isError,
    error
  }] = useCloseseSsionsMutation();
  const closeSessions = async (options, fn) => {
    const {
      ids,
      userId
    } = options;
    const successText = t(`profile.${!ids ? "allConnectionsTerminatedSuccess" : "oneConnectionTerminatedSuccess"}`);
    try {
      const requestData = {
        Ids: ids,
        UserId: userId
      };
      const res = await fetchCloseSessions(requestData);
      universalNotification({
        isSuccess,
        isError,
        error,
        successTitle: successText
      });
      if ("error" in res) {
        universalNotification({
          error: `${res == null ? void 0 : res.error}`,
          isError: true,
          isSuccess: false
        });
      } else {
        fn && fn();
        universalNotification({
          isSuccess: true,
          isError: false,
          error: null,
          successTitle: successText
        });
      }
    } catch (err) {
      console.warn("useTerminateSessions ERROR", err);
    }
  };
  return {
    closeSessions,
    isLoading,
    isSuccess,
    isError
  };
};
export {
  EmailActivationControl as E,
  OneUserActivationModal as O,
  useTerminateSessions as a,
  useVerifyEmailAndOpenModal as b,
  useEmailActivation as u
};
//# sourceMappingURL=useTerminateSessions-Cb3ukMTY.js.map
