import { Alert as b } from "./accelbyte-widgets123.mjs";
import { Button as w } from "./accelbyte-widgets107.mjs";
import { CommonError as v, BannedMessage as B } from "./accelbyte-widgets124.mjs";
import { Dialog as F, DialogBody as K, DialogFooter as x } from "./accelbyte-widgets235.mjs";
import { RadioGroup as _, Radio as q } from "./accelbyte-widgets538.mjs";
import { Loading as M } from "./accelbyte-widgets136.mjs";
import $ from "./accelbyte-widgets210.mjs";
import { Ctx as O } from "./accelbyte-widgets33.mjs";
import { Env as H } from "./accelbyte-widgets108.mjs";
import { AlertType as g } from "./accelbyte-widgets128.mjs";
import { CurrencyType as S } from "./accelbyte-widgets185.mjs";
import { EcomPath as N } from "./accelbyte-widgets59.mjs";
import { OrderAppManager as J } from "./accelbyte-widgets403.mjs";
import { DesktopChecker as Y, UrlHelper as G } from "@accelbyte/sdk";
import e from "react";
import { Trans as n } from "react-i18next";
import { Link as V } from "react-router-dom";
import { OrderHelper as f } from "./accelbyte-widgets298.mjs";
import "./accelbyte-widgets539.mjs";
import { mockPaymentConstraint as Q } from "./accelbyte-widgets209.mjs";
import { OrderStatus as W } from "@accelbyte/sdk-platform";
import { IamHelper as X } from "@accelbyte/sdk-iam";
import { MAX_INTEGER as j } from "./accelbyte-widgets339.mjs";
class z extends e.Component {
  constructor(a) {
    var s;
    super(a), this.isUserBanned = () => {
      const t = this.appState.getUser();
      return X.isUserBanned(t);
    }, this.handlePurchase = (t) => {
      const { onPurchase: i } = this.props;
      i(t);
    }, this.notifyPurchaseError = (t) => {
      const { onPurchaseError: i } = this.props;
      i && i(this.state.selectedRegionData), this.setState({
        alert: {
          type: g.danger,
          element: /* @__PURE__ */ e.createElement(v, { error: t })
        }
      });
    }, this.notifyUserBanned = () => {
      this.setState({
        alert: {
          type: g.danger,
          element: /* @__PURE__ */ e.createElement(B, null)
        }
      });
    }, this.purchase = async () => {
      const { selectedRegionData: t, quantity: i } = this.state;
      if (!t)
        return;
      await this.logic.purchase({
        regionData: t,
        quantity: i,
        returnUrl: Y.isDesktopApp() ? f.AVOID_REDIRECT_TO_FILE_PROTOCOL : G.combineURLPaths(window.location.origin, H.PLAYER_PORTAL_BASE_URL)
      });
      const { createdOrder: o, purchaseError: c } = this.logic.state;
      o && (this.handlePurchase(o), f.handleOrderCreationImplication(o), o.status === W.enum.FULFILLED && f.handleOrderFulfillmentImplication(o)), c && this.notifyPurchaseError(c);
    }, this.appState = O.getAppState(), this.logic = a.purchaseLogic || new J(O.getAppState().getSdk(), {
      item: a.item,
      userId: (s = this.appState.getUser()) == null ? void 0 : s.userId
    }), this.logic.subscribe(() => this.setState({})), this.state = {
      quantity: 1,
      selectedRegionData: a.preSelectedRegionData || (a.item.regionData || [])[0] || null,
      alert: null
    };
  }
  componentDidMount() {
    this.props.isPreviewMode || (this.logic.init(), this.isUserBanned() && this.notifyUserBanned());
  }
  render() {
    const { alert: a, selectedRegionData: s, quantity: t } = this.state, { item: i, title: o, onClose: c, customItemNameTransform: P, isOpen: R = !0, isPreviewMode: D } = this.props, { isPurchasing: h, purchaseConstraint: A, isFetching: C } = this.logic.state, { pendingOrders: m } = this.logic.state, l = D ? Q : A, r = l && l.paymentConstraints && l.paymentConstraints.find(
      (E) => JSON.stringify(E.paymentOption.regionData) === JSON.stringify(s)
    ), d = l && l.entitlementConstraint, u = r && r.paymentOption, k = r && r.walletConstraint.maxPurchase < t;
    return /* @__PURE__ */ e.createElement(F, { className: "purchaseDialog", title: o, isOpen: R, isPreviewMode: D }, /* @__PURE__ */ e.createElement(K, null, k && /* @__PURE__ */ e.createElement(b, { type: g.danger }, /* @__PURE__ */ e.createElement("div", { className: "alertContainer" }, /* @__PURE__ */ e.createElement(n, { i18nKey: "PurchaseDialog.notice.insufficientBalance" }, "Insufficient Balance"), /* @__PURE__ */ e.createElement("button", { className: "btnRecharge", onClick: () => this.appState.state.appHistory.push(N.Recharge.link) }, /* @__PURE__ */ e.createElement(n, { i18nKey: "PurchaseDialog.button.recharge" }, "Recharge")))), /* @__PURE__ */ e.createElement("table", null, /* @__PURE__ */ e.createElement("tbody", null, /* @__PURE__ */ e.createElement("tr", null, /* @__PURE__ */ e.createElement("th", null, /* @__PURE__ */ e.createElement(n, { i18nKey: "PurchaseDialog.key.item" }, "Item")), /* @__PURE__ */ e.createElement("td", null, P && P(i) || i.title)), C && /* @__PURE__ */ e.createElement("tr", null, /* @__PURE__ */ e.createElement("td", { colSpan: 2 }, /* @__PURE__ */ e.createElement(M, null))), l && /* @__PURE__ */ e.createElement(e.Fragment, null, /* @__PURE__ */ e.createElement("tr", null, /* @__PURE__ */ e.createElement("th", null, /* @__PURE__ */ e.createElement(n, { i18nKey: "PurchaseDialog.key.options" }, "Price option")), /* @__PURE__ */ e.createElement("td", null, /* @__PURE__ */ e.createElement(_, null, l.paymentConstraints.map((E, I) => {
      const { regionData: y, currencySymbol: L, finalPrice: U } = E.paymentOption, { currencyCode: T } = y;
      return /* @__PURE__ */ e.createElement(
        q,
        {
          key: `${T}-${I}`,
          onClick: () => this.setState({
            selectedRegionData: y
          }),
          checked: JSON.stringify(s) === JSON.stringify(y),
          label: `${L} ${U}`
        }
      );
    })))), d && /* @__PURE__ */ e.createElement(e.Fragment, null, d.maxAllowedPurchase > 1 && /* @__PURE__ */ e.createElement("tr", null, /* @__PURE__ */ e.createElement("th", null, /* @__PURE__ */ e.createElement(n, { i18nKey: "PurchaseDialog.key.quantity" }, "Quantity")), /* @__PURE__ */ e.createElement("td", null, /* @__PURE__ */ e.createElement("div", { className: "quantitySelector" }, /* @__PURE__ */ e.createElement(
      "button",
      {
        onClick: () => {
          this.setState({
            quantity: t - 1
          });
        },
        disabled: t <= 1,
        className: "quantityControl",
        type: "button"
      },
      "-"
    ), /* @__PURE__ */ e.createElement("span", { className: "value" }, t), /* @__PURE__ */ e.createElement(
      "button",
      {
        onClick: () => {
          this.setState({
            quantity: t + 1
          });
        },
        disabled: t >= d.maxAllowedPurchase || t >= j || !!i.flexible,
        className: "quantityControl",
        type: "button"
      },
      "+"
    )))), u && u.finalPrice > 0 && /* @__PURE__ */ e.createElement("tr", null, /* @__PURE__ */ e.createElement("th", null, /* @__PURE__ */ e.createElement(n, { i18nKey: "PurchaseDialog.key.payable" }, "Amount to pay")), /* @__PURE__ */ e.createElement("td", null, `${u.currencySymbol} ${(t * u.finalPrice).toFixed(
      u.decimals
    )}`)), d.maxAllowedPurchase < 0 && /* @__PURE__ */ e.createElement("tr", null, /* @__PURE__ */ e.createElement("td", { colSpan: 2 }, /* @__PURE__ */ e.createElement("div", { className: "itemUnavailableNotice" }, /* @__PURE__ */ e.createElement(n, { i18nKey: "PurchaseDialog.notice.itemUnavailable" }, "Item Unavailable")))))), m && m.length > 0 && /* @__PURE__ */ e.createElement("tr", null, /* @__PURE__ */ e.createElement("td", { colSpan: 2 }, /* @__PURE__ */ e.createElement(n, { i18nKey: "PurchaseDialog.notice.pendingOrders.message" }, "You already have another pending order that contains this item. Please", /* @__PURE__ */ e.createElement(
      V,
      {
        to: N.OrderDetail.makePath(
          {
            orderNo: m[0].orderNo
          },
          {
            referrer: this.appState.state.appHistory.location.pathname
          }
        )
      },
      "complete"
    ), " ", "that order before making another purchase."))), !!a && /* @__PURE__ */ e.createElement("tr", null, /* @__PURE__ */ e.createElement("td", { colSpan: 2 }, /* @__PURE__ */ e.createElement("div", null, /* @__PURE__ */ e.createElement(b, { type: a.type, onDismiss: () => this.setState({ alert: null }) }, a.element))))))), /* @__PURE__ */ e.createElement(x, { className: "dialogFooter" }, /* @__PURE__ */ e.createElement("button", { disabled: h, onClick: () => c && c(), className: "btn btn-ghost" }, /* @__PURE__ */ e.createElement(n, { i18nKey: "PurchaseDialog.buttons.cancel" }, "CANCEL")), /* @__PURE__ */ e.createElement(
      w,
      {
        loadingIndicator: "SPINNER",
        disabled: m && m.length > 0 || h || !l || l.entitlementConstraint.maxAllowedPurchase < t || r && r.walletConstraint.maxPurchase < t || this.isUserBanned(),
        onClick: () => this.purchase(),
        className: "btn btn-primary",
        isLoading: h || C
      },
      !h && r && /* @__PURE__ */ e.createElement(e.Fragment, null, r.paymentOption.finalPrice > 0 && /* @__PURE__ */ e.createElement(e.Fragment, null, r.paymentOption.regionData.currencyType === S.enum.REAL && /* @__PURE__ */ e.createElement(n, { i18nKey: "PurchaseDialog.buttons.continuePayment" }, "CONTINUE TO PAYMENT"), r.paymentOption.regionData.currencyType === S.enum.VIRTUAL && /* @__PURE__ */ e.createElement(n, { i18nKey: "PurchaseDialog.buttons.payNow" }, "PAY NOW")), r.paymentOption.finalPrice === 0 && /* @__PURE__ */ e.createElement(n, { i18nKey: "PurchaseDialog.buttons.claim" }, "CLAIM"))
    )));
  }
}
const be = $(
  (p) => /* @__PURE__ */ e.createElement(z, { ...p }),
  (p, a) => p.item !== a.item
);
export {
  be as PurchaseDialog
};
