import { Loading as M } from "./accelbyte-widgets136.mjs";
import { AppUrlParamContext as N } from "./accelbyte-widgets574.mjs";
import { Ctx as P } from "./accelbyte-widgets33.mjs";
import { AlertType as o } from "./accelbyte-widgets128.mjs";
import { Platform as h } from "@accelbyte/sdk-platform";
import t, { useContext as L, useState as a, useEffect as O, useLayoutEffect as b } from "react";
import { z as D } from "zod";
import { OrderStatusIsChargingError as v } from "./accelbyte-widgets494.mjs";
import { PaymentAlert as l } from "./accelbyte-widgets493.mjs";
import T from "./accelbyte-widgets630.mjs";
import x from "./accelbyte-widgets631.mjs";
import { PaymentDataContext as B } from "./accelbyte-widgets576.mjs";
const i = D.enum(["BILLING_INFO", "PAYMENT"]), J = () => {
  const c = P.getAppState().getSdk(), { paymentOrderNo: g } = L(N), { paymentData: S, setPaymentData: w } = L(B), [E, p] = a(i.enum.BILLING_INFO), [r, s] = a({
    value: null,
    isLoading: !1,
    error: null
  }), [n, u] = a({
    value: null,
    isLoading: !1,
    error: null
  }), [m, f] = a({
    value: null,
    isLoading: !1,
    error: null
  }), d = async () => {
    if (!r.isLoading) {
      s({ value: null, isLoading: !0, error: null });
      try {
        const e = await h.PaymentStationApi(c).getInfoPayment_ByPaymentOrderNo(g);
        if (e.charging)
          throw new v();
        s({ value: e, isLoading: !1, error: null });
      } catch (e) {
        console.error(e), s({ error: e, value: null, isLoading: !1 });
      }
    }
  }, y = async () => {
    if (!(!r.value || n.isLoading)) {
      u({ value: null, isLoading: !0, error: null });
      try {
        const e = await h.PaymentStationApi(c).getPaymentMethods({ paymentOrderNo: g });
        u({ value: e, isLoading: !1, error: null }), p(i.enum.PAYMENT);
      } catch (e) {
        console.error(e), u({ error: e, value: null, isLoading: !1 });
      }
    }
  }, A = async () => {
    if (!r.isLoading) {
      f({ value: null, isLoading: !0, error: null });
      try {
        const e = await P.getAppState().userAppManager.fetchUserProfile();
        f({ value: e || null, isLoading: !1, error: null });
      } catch (e) {
        console.error(e), f({ error: e, value: null, isLoading: !1 });
      }
    }
  }, I = (e) => {
    w({ ...S, zipCode: e }), p(i.enum.PAYMENT);
  };
  return O(() => {
    d(), A();
  }, []), b(() => {
    r.value && y();
  }, [r.value]), r.error ? r.error instanceof v ? /* @__PURE__ */ t.createElement(l, { type: o.warning, message: "This payment order is being processed" }) : /* @__PURE__ */ t.createElement(
    l,
    {
      type: o.warning,
      message: "Something went wrong. Please retry or contact our administrator if the problem persist",
      onRetry: d
    }
  ) : n.error ? /* @__PURE__ */ t.createElement(
    l,
    {
      type: o.warning,
      message: "Something went wrong. Please retry or contact our administrator if the problem persist",
      onRetry: y
    }
  ) : r.isLoading || n.isLoading || m.isLoading ? /* @__PURE__ */ t.createElement(M, null) : E === i.enum.PAYMENT && r.value && n.value ? n.value.length > 0 ? /* @__PURE__ */ t.createElement(x, { paymentOrderDetails: r.value, paymentMethods: n.value }) : /* @__PURE__ */ t.createElement(
    l,
    {
      type: o.warning,
      message: "No payment method available for this purchase, Please contact our administrator if you see this error."
    }
  ) : /* @__PURE__ */ t.createElement(T, { userProfile: m && m.value, onSubmit: I });
};
export {
  J as default
};
