import { L as Loading, w as LoadingSize, T as Trans, i as CommonError, al as IamHelper, G as GlobalPath, F as FeUtils, E as ErrorHelper, v as LoginPath, t } from "./index.js";
import React__default from "react";
import { Link } from "react-router-dom";
import "./validator.js";
import "./dateFns.js";
import "./reactPlayer.js";
import "./reactSlick.js";
const errorContainer = "errorContainer _errorContainer_eea22";
const sessionExpiredContainer = "sessionExpiredContainer _sessionExpiredContainer_c9836";
var styles = {
  errorContainer,
  sessionExpiredContainer
};
class AuthorizationCodeExchanger extends React__default.Component {
  constructor(props) {
    super(props);
    this.state = {
      error: null
    };
  }
  componentDidMount() {
    this.exchange();
  }
  clearParams() {
    const { appHistory } = this.props.appState.state;
    appHistory.replace(`${appHistory.location.pathname}`);
  }
  async exchange() {
    try {
      await this.exchangeAuthorizationCode();
      this.props.appState.markAuthCodeExchangeTrialDone();
    } catch (error) {
      console.error(error);
      this.clearParams();
      this.setState({ error });
    }
  }
  render() {
    const { error } = this.state;
    return /* @__PURE__ */ React__default.createElement(React__default.Fragment, null, !error && /* @__PURE__ */ React__default.createElement(Loading, {
      className: LoadingSize.medium
    }, /* @__PURE__ */ React__default.createElement(Trans, {
      i18nKey: "InitialUserFetchPage.notice"
    })), error && /* @__PURE__ */ React__default.createElement("div", {
      className: styles.errorContainer
    }, /* @__PURE__ */ React__default.createElement(CommonError, {
      error
    })), error && /* @__PURE__ */ React__default.createElement(AuthorizationCodeExchangerError, {
      error
    }));
  }
  async exchangeAuthorizationCode() {
    const { appHistory } = this.props.appState.state;
    const { code, error, state } = IamHelper.getAuthorizationCodeParams(appHistory.location.search);
    const sdk = this.props.appState.getSdk();
    try {
      const userAuthorization = sdk.IAM.UserAuthorization();
      const exchangeResult = await userAuthorization.exchangeAuthorizationCode({
        code,
        error,
        state
      });
      if (!exchangeResult) {
        return;
      }
      this.clearParams();
      const { mfaData, returnPath } = exchangeResult;
      if (mfaData) {
        this.props.appState.getUserManager().updateMFAData(mfaData);
      } else {
        await this.props.appState.fetchAllUserInformation();
      }
      if (returnPath) {
        appHistory.replace(returnPath);
      } else {
        appHistory.replace(GlobalPath.Home.link);
      }
    } catch (error2) {
      FeUtils.redirectToErrorPage(appHistory, error2);
    }
  }
}
const AuthorizationCodeExchangerError = ({ error }) => {
  var _a;
  if (!error)
    return null;
  if (ErrorHelper.isAxiosError(error) && ((_a = error == null ? void 0 : error.response) == null ? void 0 : _a.status)) {
    const { status } = error.response;
    if (status === 401) {
      return /* @__PURE__ */ React__default.createElement("div", {
        className: styles.sessionExpiredContainer
      }, /* @__PURE__ */ React__default.createElement(Trans, {
        i18nKey: "AuthorizationCodeExchanger.error.sessionExpired"
      }, "Session Expired"), /* @__PURE__ */ React__default.createElement(Link, {
        to: LoginPath.Login.makePath()
      }, t("HomePage.login")));
    }
  }
  return /* @__PURE__ */ React__default.createElement(CommonError, {
    error
  });
};
export { AuthorizationCodeExchanger as default };
