import { Alert as I } from "./accelbyte-widgets123.mjs";
import { Button as D, ButtonType as N, ButtonStyle as w } from "./accelbyte-widgets107.mjs";
import { CommonError as S } from "./accelbyte-widgets124.mjs";
import { Dialog as b, DialogBody as T, DialogFooter as L } from "./accelbyte-widgets235.mjs";
import { Loading as R } from "./accelbyte-widgets136.mjs";
import { UIEvent as k } from "./accelbyte-widgets138.mjs";
import { Ctx as p } from "./accelbyte-widgets33.mjs";
import { Env as h } from "./accelbyte-widgets108.mjs";
import { FeatureFlags as v } from "./accelbyte-widgets57.mjs";
import { AlertType as A } from "./accelbyte-widgets128.mjs";
import { PaymentStatus as m } from "./accelbyte-widgets185.mjs";
import { EcomPath as C } from "./accelbyte-widgets59.mjs";
import { UrlHelper as g, DesktopChecker as P } from "@accelbyte/sdk";
import F from "./accelbyte-widgets140.mjs";
import { format as U, parseISO as _ } from "date-fns";
import e from "react";
import { Trans as l } from "react-i18next";
import { Prompt as B, Redirect as x } from "react-router-dom";
import { OrderHelper as f } from "./accelbyte-widgets298.mjs";
import { PaymentStatusSuccessDialog as O, PaymentStatusNeedMoreTimeProcessedDialog as W, PaymentStatusProcessedCountDownDialog as M, PaymentStatusFailedDialog as K, OrderExpiredDialog as H, OrderCancelledDialog as Y } from "./accelbyte-widgets299.mjs";
import "./accelbyte-widgets300.mjs";
import { PaymentContainer as $ } from "./accelbyte-widgets301.mjs";
import { LegacyPaymentStation as G } from "./accelbyte-widgets302.mjs";
import { OrderLogic as V } from "./accelbyte-widgets260.mjs";
import { OrderStatus as s } from "@accelbyte/sdk-platform";
const j = 5, X = 1e3;
class Pe extends e.Component {
  constructor(o) {
    var u;
    super(o), this.redirectToItemPage = () => {
      this.setState({ shouldRedirect: !0 });
    }, this.onClosePaymentSuccessfulDialog = () => {
      var t, n;
      this.redirectToItemPage(), this.logic.state.order && ((n = (t = this.props).onClosePaymentDialog) == null || n.call(t, { status: "payment-succeeded", order: this.logic.state.order }));
    }, this.onCloseOrderCancelledDialog = () => {
      var t, n;
      (n = (t = this.props).onClosePaymentDialog) == null || n.call(t, { status: "orders-cancelled" }), this.redirectToItemPage();
    }, this.onPaymentSuccess = async () => {
      const { orderNo: t } = this.props;
      await this.logic.fetchOrder(t), await p.getAppState().refreshWallet();
      const { order: n } = this.logic.state;
      if (this.setState({ paymentStatus: m.enum.DONE }), !!n)
        switch (n.status) {
          case s.enum.INIT:
            this.retryCheckOrderStatus(), this.setState({
              paymentStatusProcessedCountDownDialogIsOpen: !0
            });
            break;
          case s.enum.CHARGED:
          case s.enum.FULFILLED:
            this.setState({ paymentStatusSuccessIsOpen: !0 });
            break;
        }
    }, this.retryCheckOrderStatus = async (t = j, n = X) => {
      var a, d, y, E;
      const { orderNo: i } = this.props;
      await this.logic.fetchOrder(i);
      const { order: r } = this.logic.state;
      r && (r.status === s.enum.FULFILLED && (window.removeEventListener("beforeunload", this.onReloadPageWarningWhileStillInCountdown, !1), this.setState({
        paymentStatusProcessedCountDownDialogIsOpen: !1,
        paymentStatusSuccessIsOpen: !0
      }), (d = (a = this.props).onClosePaymentDialog) == null || d.call(a, { status: "payment-succeeded", order: r })), r.status === s.enum.INIT && t > 0 && setTimeout(() => {
        this.retryCheckOrderStatus(t - 1, n);
      }, n), t === 0 && (this.setState({
        paymentStatusProcessedCountDownDialogIsOpen: !1,
        paymentStatusNeedMoreTimeProcessedDialogIsOpen: !0
      }), (E = (y = this.props).onClosePaymentDialog) == null || E.call(y, { status: "payment-processing" })));
    }, this.renderPaymentStation = (t) => {
      const n = p.hasFeature(v.isPaymentV2Enabled), i = p.getAppState().state.appHistory, r = `${g.trimSlashFromStringStart(i.location.pathname)}${i.location.search}`;
      return n ? /* @__PURE__ */ e.createElement(
        $,
        {
          order: t,
          onPaymentProcessed: (a) => {
            switch (a) {
              case m.enum.DONE:
                f.handleOrderFulfillmentImplication(t), this.onPaymentSuccess();
                break;
              case m.enum.FAILED:
                this.setState({ paymentStatus: a });
                break;
            }
          },
          paymentStationUrl: P.isDesktopApp() ? h.LAUNCHER_PAYMENT_URL : void 0,
          returnUrl: P.isDesktopApp() ? `${h.REDIRECT_URL}${r}` : g.combineURLPaths(window.location.origin, h.PLAYER_PORTAL_BASE_URL, C.PaymentSuccessCallback.makePath())
        }
      ) : /* @__PURE__ */ e.createElement(
        G,
        {
          order: t,
          onPaymentProcessed: (a) => {
            switch (a) {
              case m.enum.DONE:
                f.handleOrderFulfillmentImplication(t), this.onPaymentSuccess();
                break;
              case m.enum.FAILED:
                this.setState({ paymentStatus: a });
                break;
            }
          },
          returnUrl: g.combineURLPaths(
            window.location.origin,
            h.PLAYER_PORTAL_BASE_URL,
            C.PaymentSuccessCallback.makePath()
          )
        }
      );
    }, this.renderOrderContent = (t, n) => {
      if (!t.expireTime)
        return;
      const { paymentStatus: i } = this.state, r = U(_(t.expireTime), "d MMM yyyy hh:mm a");
      return /* @__PURE__ */ e.createElement("div", { className: "orderContent" }, /* @__PURE__ */ e.createElement("div", { className: "orderCard" }, /* @__PURE__ */ e.createElement("div", { className: "orderHeader" }, /* @__PURE__ */ e.createElement(l, { i18nKey: "OrderPage.orderContent.title" }, "Payment")), /* @__PURE__ */ e.createElement("div", { className: "orderExpire" }, n.regionDataItem && /* @__PURE__ */ e.createElement(I, { type: A.info }, /* @__PURE__ */ e.createElement(l, { i18nKey: "OrderPage.orderExpire.title" }, "This order will expire on"), " ", r)), t && t.status === s.enum.INIT && i !== m.enum.DONE && /* @__PURE__ */ e.createElement("section", { className: "paymentSection", id: "paymentSection" }, this.renderPaymentStation(t), /* @__PURE__ */ e.createElement("div", { className: "btnContainer" }, /* @__PURE__ */ e.createElement(
        "button",
        {
          disabled: t.status !== s.enum.INIT,
          onClick: () => this.openCancellationDialog(),
          className: "btn btn-ghost"
        },
        /* @__PURE__ */ e.createElement(l, { i18nKey: "OrderPage.cancelOrder" }, "Cancel")
      )))), /* @__PURE__ */ e.createElement("button", { disabled: t.status !== s.enum.INIT, onClick: () => this.openCancellationDialog(), className: "btn btnCancel" }, /* @__PURE__ */ e.createElement(l, { i18nKey: "OrderPage.cancelOrder" }, "Cancel")));
    }, this.restartPayment = () => {
      this.setState({ paymentStatus: null, paymentStatusSuccessIsOpen: !1 });
    };
    const c = ((u = p.getAppState().getUser()) == null ? void 0 : u.userId) || "";
    this.logic = new V({
      orderNo: o.orderNo,
      userId: c
    }), this.logic.subscribe(() => this.setState({})), this.state = {
      paymentStatus: null,
      orderInfo: null,
      shouldRedirect: !1,
      cancellationDialogIsOpen: !1,
      cancellationNoticeIsOpen: !1,
      paymentStatusSuccessIsOpen: !1,
      paymentStatusProcessedCountDownDialogIsOpen: !1,
      paymentStatusNeedMoreTimeProcessedDialogIsOpen: !1
    };
  }
  componentDidMount() {
    window.addEventListener("beforeunload", this.onReloadPageWarningWhileStillInCountdown, !1), this.logic.init(), k.updateBackground(`url(${F})`);
  }
  componentWillUnmount() {
    window.removeEventListener("beforeunload", this.onReloadPageWarningWhileStillInCountdown, !1);
  }
  render() {
    const {
      shouldRedirect: o,
      paymentStatus: c,
      paymentStatusSuccessIsOpen: u,
      paymentStatusProcessedCountDownDialogIsOpen: t,
      paymentStatusNeedMoreTimeProcessedDialogIsOpen: n
    } = this.state, { isFetching: i, error: r, order: a } = this.logic.state, d = u || t || n;
    return a && o ? this.renderRedirection() : (a == null ? void 0 : a.status) === s.enum.FULFILLED ? (window.removeEventListener("beforeunload", this.onReloadPageWarningWhileStillInCountdown, !1), /* @__PURE__ */ e.createElement(
      O,
      {
        className: "orderDialog",
        orderInfo: a,
        onCloseDialog: this.onClosePaymentSuccessfulDialog,
        isOnlyShowCloseButton: this.props.isOnlyShowCloseButton
      }
    )) : /* @__PURE__ */ e.createElement("div", { className: "paymentBox" }, (a == null ? void 0 : a.status) === s.enum.INIT && /* @__PURE__ */ e.createElement(
      B,
      {
        message: () => /* @__PURE__ */ e.createElement(l, { i18nKey: "OrderPage.navigationPrompt" }, "You have not completed this transaction. You will need to finish or cancel this transaction before you can place another order. Are you sure you want to navigate away?")
      }
    ), n && /* @__PURE__ */ e.createElement(W, { onClose: this.redirectToItemPage }), t && /* @__PURE__ */ e.createElement(M, null), u && a && /* @__PURE__ */ e.createElement(
      O,
      {
        className: "orderDialog",
        orderInfo: a,
        onCloseDialog: this.onClosePaymentSuccessfulDialog,
        isOnlyShowCloseButton: this.props.isOnlyShowCloseButton
      }
    ), c === m.enum.FAILED && /* @__PURE__ */ e.createElement(K, { className: "orderDialog", onCloseDialog: () => this.restartPayment() }), this.renderExpiryDialog(), this.state.cancellationNoticeIsOpen && this.renderCancellationNotice(), this.state.cancellationDialogIsOpen && this.renderCancellationDialog(), i && /* @__PURE__ */ e.createElement(R, null), !i && /* @__PURE__ */ e.createElement(e.Fragment, null, r && /* @__PURE__ */ e.createElement("div", { className: "errorContainer" }, /* @__PURE__ */ e.createElement(S, { error: r })), (a == null ? void 0 : a.itemSnapshot) && !d && this.renderOrderContent(a, a.itemSnapshot)));
  }
  renderRedirection() {
    return this.props.redirectPath ? /* @__PURE__ */ e.createElement(x, { to: this.props.redirectPath }) : null;
  }
  openCancellationDialog() {
    this.setState({ cancellationDialogIsOpen: !0 });
  }
  closeCancellationDialog() {
    this.setState({ cancellationDialogIsOpen: !1 });
  }
  renderCancellationDialog() {
    const {
      state: { isCancelling: o, cancellationError: c }
    } = this.logic;
    return /* @__PURE__ */ e.createElement(
      b,
      {
        className: "orderDialog",
        title: /* @__PURE__ */ e.createElement(l, { i18nKey: "OrderPage.cancellationDialog.title" }, "Confirm Cancellation"),
        isOpen: !0
      },
      /* @__PURE__ */ e.createElement(T, null, /* @__PURE__ */ e.createElement("p", null, /* @__PURE__ */ e.createElement(l, { i18nKey: "OrderPage.cancellationDialog.message" }, "You're just one step away from completing payment, are you sure you want to cancel this order?")), c && /* @__PURE__ */ e.createElement("div", { className: "errorContainer" }, /* @__PURE__ */ e.createElement(S, { error: c }))),
      /* @__PURE__ */ e.createElement(L, { className: "orderCancellationDialogFooter" }, /* @__PURE__ */ e.createElement("div", { className: "btnContainer" }, /* @__PURE__ */ e.createElement(
        D,
        {
          disabled: o,
          onClick: () => this.closeCancellationDialog(),
          className: "btn btn-ghost uppercase",
          buttonType: N.TEXT,
          buttonStyle: w.NONE
        },
        /* @__PURE__ */ e.createElement(l, { i18nKey: "OrderPage.cancellationDialog.backButton" }, "Back")
      ), /* @__PURE__ */ e.createElement(
        D,
        {
          loadingIndicator: "SPINNER",
          className: "btn-primary uppercase",
          isLoading: o,
          onClick: () => this.cancelOrder()
        },
        /* @__PURE__ */ e.createElement(l, { i18nKey: "OrderPage.cancellationDialog.cancelButton" }, "Cancel Order")
      )))
    );
  }
  renderExpiryDialog() {
    const { order: o, orderIsExpired: c } = this.logic.state;
    if (!o)
      return null;
    if (!(o.status !== s.enum.CLOSED && !c))
      return /* @__PURE__ */ e.createElement(H, { className: "orderDialog", onCloseDialog: () => this.redirectToItemPage() });
  }
  renderCancellationNotice() {
    return /* @__PURE__ */ e.createElement(Y, { className: "orderDialog", onCloseDialog: this.onCloseOrderCancelledDialog });
  }
  async cancelOrder() {
    await this.logic.cancelAllOrders(), this.logic.state.cancellationError || this.setState({ cancellationNoticeIsOpen: !0, cancellationDialogIsOpen: !1 });
  }
  onReloadPageWarningWhileStillInCountdown(o) {
    o.preventDefault();
  }
}
export {
  Pe as default
};
