import { ba as CircleCheckIcon, bg as DangerIcon, f as Button, m as mod, v as LoginPath, G as GlobalPath, x as getCurrentLanguage, h as AlertType, i as CommonError, t, j as Alert, r as ErrorPath, T as Trans, a as Ctx, k as UIEvent, l as bgCommon, be as logo } from "./index.js";
import React__default from "react";
import "./dateFns.js";
import "./validator.js";
import "./reactPlayer.js";
import "react-router-dom";
import "./reactSlick.js";
class VerifyUserMessage extends React__default.Component {
  constructor(props) {
    super(props);
  }
  render() {
    const { isSuccess, onClickButton, title, description, buttonText, buttonDataQa = null, titleDataQa = null, isLoading } = this.props;
    return /* @__PURE__ */ React__default.createElement(React__default.Fragment, null, /* @__PURE__ */ React__default.createElement("div", {
      className: "flex justify-center"
    }, isSuccess ? /* @__PURE__ */ React__default.createElement(CircleCheckIcon, {
      checked: true,
      fill: true
    }) : /* @__PURE__ */ React__default.createElement(DangerIcon, {
      className: "dangerIcon"
    })), /* @__PURE__ */ React__default.createElement("div", {
      className: "mb-4 text-center"
    }, /* @__PURE__ */ React__default.createElement("p", {
      className: "messageTitle my-4",
      "data-qa-id": titleDataQa
    }, title), /* @__PURE__ */ React__default.createElement("p", {
      className: "messageDesc"
    }, description)), /* @__PURE__ */ React__default.createElement(Button, {
      loadingIndicator: "SPINNER",
      className: "btn btn-primary w-full",
      onClick: onClickButton,
      "data-qa-id": buttonDataQa,
      isLoading
    }, buttonText));
  }
}
var VerifyUserPage$1 = "";
const VERIFY_USER_ERROR = mod.enum([
  "failed_to_verify_invalid_link",
  "failed_to_verify_server_error",
  "failed_to_verify_user_not_found",
  "failed_to_verify_expired_link"
]);
const PAGE_STATE = mod.enum(["message", "checkEmail"]);
class VerifyUserPage extends React__default.Component {
  constructor(props) {
    super(props);
    this.redirectToLogin = () => {
      const { appHistory } = this.appState.state;
      appHistory.push(LoginPath.Login.link);
    };
    this.redirectToHome = () => {
      const { appHistory } = this.appState.state;
      appHistory.push(GlobalPath.Home.link);
    };
    this.logoutAndRedirectToLogin = async () => {
      await this.appState.logout();
      this.redirectToLogin();
    };
    this.resendVerificationLink = async () => {
      const userApi = this.appState.getSdk().IAM.User();
      try {
        if (this.state.isResending)
          return;
        this.setState({ isResending: true });
        const result = await userApi.sendVerificationLink(getCurrentLanguage());
        if (result.error)
          throw result.error;
        this.setState({ page: PAGE_STATE.enum.checkEmail, alert: null });
      } catch (error) {
        console.error(error);
        this.setState({
          alert: {
            type: AlertType.danger,
            element: /* @__PURE__ */ React__default.createElement(CommonError, {
              error,
              defaultMessage: t("verifyUserPage.error.alert.resend.failed")
            })
          }
        });
      } finally {
        this.setState({ isResending: false });
      }
    };
    this.dismissAlert = () => {
      this.setState({ alert: null });
    };
    this.renderAlert = (_errorMsg) => {
      const { alert } = this.state;
      if (!alert) {
        return null;
      }
      return /* @__PURE__ */ React__default.createElement(Alert, {
        type: alert.type,
        onDismiss: this.dismissAlert,
        className: "mb-4"
      }, alert.element);
    };
    this.renderSuccessMessage = () => {
      return /* @__PURE__ */ React__default.createElement(VerifyUserMessage, {
        isSuccess: true,
        title: t("verifyUserPage.success.title"),
        description: t("verifyUserPage.success.desc"),
        buttonText: t("navigation.links.login"),
        onClickButton: this.logoutAndRedirectToLogin
      });
    };
    this.renderErrorMessage = (errorMessage, email) => {
      const user = this.appState.getUser();
      const isLoggedIn = !!(user == null ? void 0 : user.userId);
      const userEmail = user == null ? void 0 : user.emailAddress;
      const { isResending } = this.state;
      switch (true) {
        case errorMessage === VERIFY_USER_ERROR.enum.failed_to_verify_user_not_found:
          return /* @__PURE__ */ React__default.createElement(VerifyUserMessage, {
            isSuccess: false,
            title: t("verifyUserPage.error.failed.userNotFound.title"),
            description: t("verifyUserPage.error.failed.userNotFound.desc"),
            buttonText: t("ErrorPage401.buttonAction"),
            onClickButton: this.redirectToHome
          });
        case errorMessage === VERIFY_USER_ERROR.enum.failed_to_verify_server_error:
          this.appState.state.appHistory.push(ErrorPath.Error500.makePath());
          return;
        case !isLoggedIn:
          return /* @__PURE__ */ React__default.createElement(VerifyUserMessage, {
            isSuccess: false,
            title: t("verifyUserPage.error.failed.title"),
            description: t("verifyUserPage.error.failed.notLogIn.desc"),
            buttonText: t("navigation.links.login"),
            onClickButton: this.redirectToLogin
          });
        case (!!email && userEmail !== email):
          return /* @__PURE__ */ React__default.createElement(VerifyUserMessage, {
            isSuccess: false,
            title: t("verifyUserPage.error.failed.title"),
            description: t("verifyUserPage.error.failed.wrongAccount.desc"),
            buttonText: t("navigation.links.logout"),
            onClickButton: this.logoutAndRedirectToLogin
          });
        case errorMessage === VERIFY_USER_ERROR.enum.failed_to_verify_expired_link:
          return /* @__PURE__ */ React__default.createElement(VerifyUserMessage, {
            isSuccess: false,
            title: t("verifyUserPage.error.expired.title"),
            description: t("verifyUserPage.error.expired.desc"),
            buttonText: t("verifyUserPage.error.button.resend"),
            onClickButton: this.resendVerificationLink,
            isLoading: isResending
          });
        case errorMessage === VERIFY_USER_ERROR.enum.failed_to_verify_invalid_link:
        default:
          return /* @__PURE__ */ React__default.createElement(VerifyUserMessage, {
            isSuccess: false,
            title: t("verifyUserPage.error.invalid.title"),
            description: t("verifyUserPage.error.invalid.desc"),
            buttonText: t("verifyUserPage.error.button.resend"),
            onClickButton: this.resendVerificationLink,
            isLoading: isResending
          });
      }
    };
    this.renderCheckEmail = () => {
      const email = new URLSearchParams(location.search).get("email");
      return /* @__PURE__ */ React__default.createElement("div", {
        className: "text-center"
      }, /* @__PURE__ */ React__default.createElement("p", {
        className: "messageTitle mb-4"
      }, t("verifyUserPage.checkEmail.title")), /* @__PURE__ */ React__default.createElement("p", {
        className: "messageDesc"
      }, /* @__PURE__ */ React__default.createElement(Trans, {
        i18nKey: "verifyUserPage.checkEmail.desc"
      }, "We have sent a verification email to", /* @__PURE__ */ React__default.createElement("span", {
        className: "email"
      }, { email }))), /* @__PURE__ */ React__default.createElement("hr", {
        className: "my-5 text-[color:rgb(var(--od-rgb-text),0.65)]"
      }), /* @__PURE__ */ React__default.createElement("p", {
        className: "messageDesc"
      }, t("verifyUserPage.checkEmail.question")), /* @__PURE__ */ React__default.createElement("a", {
        onClick: this.resendVerificationLink,
        className: "cursor-pointer text-sm"
      }, t("verifyUserPage.checkEmail.resend")));
    };
    this.appState = Ctx.getAppState();
    this.state = {
      page: PAGE_STATE.enum.message,
      alert: null,
      isResending: false
    };
  }
  componentDidMount() {
    UIEvent.updateBackground(`url(${bgCommon})`);
  }
  componentWillUnmount() {
    UIEvent.updateBackground(null);
  }
  render() {
    const { page } = this.state;
    const searchParam = new URLSearchParams(location.search);
    const isSuccess = searchParam.get("success") === "true";
    const email = searchParam.get("email") || "";
    const errorMessage = searchParam.get("error_message") || "";
    return /* @__PURE__ */ React__default.createElement("div", {
      className: "verifyUserPage"
    }, /* @__PURE__ */ React__default.createElement("div", {
      className: "verifyUserContainer"
    }, /* @__PURE__ */ React__default.createElement("div", {
      className: "flex justify-center"
    }, /* @__PURE__ */ React__default.createElement("img", {
      src: logo,
      loading: "lazy"
    })), /* @__PURE__ */ React__default.createElement("div", {
      className: "verifyUserCard"
    }, this.renderAlert(errorMessage), page === PAGE_STATE.enum.message && /* @__PURE__ */ React__default.createElement(React__default.Fragment, null, isSuccess && this.renderSuccessMessage(), !isSuccess && this.renderErrorMessage(errorMessage, email)), page === PAGE_STATE.enum.checkEmail && this.renderCheckEmail())));
  }
}
export { VerifyUserPage as default };
