import { Loading as w } from "./accelbyte-widgets136.mjs";
import { PaymentAlert as v } from "./accelbyte-widgets493.mjs";
import { sendMessageToParentWindow as S, PAYMENT_STATION_ACTION as b } from "./accelbyte-widgets496.mjs";
import { Ctx as E } from "./accelbyte-widgets33.mjs";
import { AlertType as P } from "./accelbyte-widgets128.mjs";
import { Elements as I } from "@stripe/react-stripe-js";
import { loadStripe as _ } from "@stripe/stripe-js";
import { Platform as A } from "@accelbyte/sdk-platform";
import n, { useState as c, useEffect as d } from "react";
import { z as T } from "zod";
import N from "./accelbyte-widgets705.mjs";
import L from "./accelbyte-widgets706.mjs";
const f = T.enum([
  "succeeded",
  "canceled",
  "processing",
  "requires_action",
  "requires_confirmation",
  "requires_payment_method"
]), Y = (p) => {
  const h = E.getAppState().getSdk(), [i, l] = c({
    value: null,
    isLoading: !1,
    error: null
  }), [s, g] = c({
    value: null,
    isLoading: !1
  }), [r, m] = c({
    value: null,
    isLoading: !1,
    error: null
  }), y = async () => {
    const {
      paymentOrderDetails: { region: o, sandbox: t },
      paymentUrl: { paymentProvider: a }
    } = p;
    if (!i.isLoading) {
      l({ value: null, isLoading: !0, error: null });
      try {
        const u = await A.PaymentStationApi(h).getPaymentPublicconfig({
          paymentProvider: a,
          region: o || null,
          sandbox: t
        });
        if (!u.publishableKey || typeof u.publishableKey != "string")
          throw new Error("Invalid configuration");
        l({ value: u.publishableKey, isLoading: !1, error: null });
      } catch (e) {
        console.error(e), l({ error: e, isLoading: !1, value: null });
      }
    }
  }, C = async (o, t) => {
    var a;
    if (!(r.isLoading || ((a = r.value) == null ? void 0 : a.status) === f.enum.succeeded || !s.value)) {
      m({ value: null, isLoading: !0, error: null });
      try {
        const e = await o.confirmCardPayment(p.paymentUrl.paymentUrl || "", {
          payment_method: t.id
        });
        if (e.error)
          throw e.error;
        e.paymentIntent && e.paymentIntent.status === f.enum.succeeded && m({
          value: e.paymentIntent,
          error: null,
          isLoading: !1
        });
      } catch (e) {
        m({ error: e, value: null, isLoading: !1 });
      }
    }
  };
  return d(() => {
    r.value && r.value.status === f.enum.succeeded && S({ message: { action: b.enum.complete } });
  }, [r.value]), d(() => {
    y();
  }, []), d(() => {
    const { isLoading: o, value: t } = i;
    (async () => {
      if (t) {
        if (o || !t)
          return;
        g({ value: null, isLoading: !0 });
        const a = await _(t);
        g({ isLoading: !1, value: a });
      }
    })();
  }, [i]), i.error ? /* @__PURE__ */ n.createElement(
    v,
    {
      type: P.warning,
      onRetry: y,
      message: "Something went wrong, Please retry or contact our administrator if the problem persist"
    }
  ) : /* @__PURE__ */ n.createElement("div", { className: L.stripeHandler }, !s.value && /* @__PURE__ */ n.createElement(w, null), !!s.value && /* @__PURE__ */ n.createElement(I, { stripe: s.value }, /* @__PURE__ */ n.createElement("div", { className: L.formContainer }, r.error && /* @__PURE__ */ n.createElement(
    v,
    {
      type: P.warning,
      message: r.error.message ? `${r.error.message} Please contact our support team for further information` : "Something went wrong, Please retry or contact our administrator if the problem persist"
    }
  ), !r.error && /* @__PURE__ */ n.createElement(N, { isLoading: r.isLoading, onSubmit: C }))));
};
export {
  f as StripePaymentIntentStatus,
  Y as default
};
