import { Loading as O } from "./accelbyte-widgets136.mjs";
import { Ctx as _ } from "./accelbyte-widgets33.mjs";
import { AlertType as k } from "./accelbyte-widgets128.mjs";
import { ErrorHelper as I } from "./accelbyte-widgets120.mjs";
import { SdkDevice as L } from "@accelbyte/sdk";
import r, { useContext as M, useState as i, useMemo as $, useEffect as b } from "react";
import { AppUrlParamContext as z } from "./accelbyte-widgets574.mjs";
import { PAYMENT_PROVIDER_WITHOUT_PAYMENT_URL as D } from "./accelbyte-widgets494.mjs";
import { isMobile as Y } from "./accelbyte-widgets497.mjs";
import { PaymentAlert as H } from "./accelbyte-widgets493.mjs";
import { PaymentDataContext as W } from "./accelbyte-widgets576.mjs";
import B from "./accelbyte-widgets660.mjs";
import C from "./accelbyte-widgets661.mjs";
import J from "./accelbyte-widgets662.mjs";
import d from "./accelbyte-widgets663.mjs";
import { Platform as U } from "@accelbyte/sdk-platform";
function le({ paymentOrderDetails: l, paymentMethods: s }) {
  const p = _.getAppState().getSdk(), { paymentOrderNo: f, returnUrl: y } = M(z), { paymentData: m, setPaymentData: S } = M(W), [t, x] = i(s[0]), [o, a] = i({
    value: null,
    isLoading: !1,
    error: null
  }), [P, c] = i({
    value: null,
    isLoading: !1,
    error: null
  }), [T, w] = i(null), u = $(
    () => D.includes(t.paymentProvider.toUpperCase()),
    [t]
  ), g = async () => {
    var E, h;
    if (o.isLoading)
      return;
    const { paymentProvider: e } = t;
    a({ value: null, isLoading: !0, error: null });
    let v = `${window.location.origin}${window.location.pathname}`;
    y && (v += `?returnUrl=${encodeURIComponent(y)}`);
    try {
      const n = { version: Y() ? L.TYPE.MOBILE : L.TYPE.DESKTOP }, R = await U.PaymentStationApi(p).createPaymentLink({
        paymentOrderNo: f,
        paymentProvider: e,
        returnUrl: v,
        zipCode: m.zipCode,
        ui: JSON.stringify(n)
      });
      a({ value: R, isLoading: !1, error: null });
    } catch (n) {
      console.error(n), I.isAxiosError(n) && (w(`${(E = n.response) == null ? void 0 : E.data.errorCode}: ${(h = n.response) == null ? void 0 : h.data.errorMessage}`), a({ error: n, value: null, isLoading: !1 }));
    }
  }, A = async () => {
    if (!P.isLoading) {
      c({ value: null, isLoading: !0, error: null });
      try {
        const e = await U.PaymentStationApi(p).getPaymentTax({
          paymentProvider: t.paymentProvider,
          paymentOrderNo: f,
          zipCode: m.zipCode
        });
        c({ value: e, isLoading: !1, error: null }), S({ ...m, tax: e });
      } catch (e) {
        console.error(e), c({ error: e, value: null, isLoading: !1 });
      }
    }
  }, N = (e) => {
    o.isLoading || e.paymentProvider === t.paymentProvider || x(e);
  };
  return b(() => {
    u ? a({
      value: null,
      isLoading: !1,
      error: null
    }) : g(), A();
  }, [t]), /* @__PURE__ */ r.createElement("div", { className: d.paymentMethodSelector }, s.length > 1 && /* @__PURE__ */ r.createElement("div", { className: d.paymentMethodContainer }, /* @__PURE__ */ r.createElement(
    J,
    {
      paymentMethods: s,
      activePaymentMethod: t,
      onPaymentMethodChange: N,
      disabled: o.isLoading
    }
  )), P.value && /* @__PURE__ */ r.createElement(B, { paymentOrderDetails: l }), /* @__PURE__ */ r.createElement("div", { className: d.paymentMethodCheckout }, o.isLoading && /* @__PURE__ */ r.createElement(O, null), !!o.error && !u && /* @__PURE__ */ r.createElement(
    H,
    {
      type: k.warning,
      message: T || "Something went wrong, Please retry or contact our administrator if the problem persist",
      onRetry: g
    }
  ), !o.isLoading && !!o.value && /* @__PURE__ */ r.createElement(
    C,
    {
      paymentUrl: o.value,
      paymentOrderDetails: l,
      activePaymentMethod: t
    }
  ), u && /* @__PURE__ */ r.createElement(C, { activePaymentMethod: t, paymentOrderDetails: l })));
}
export {
  le as default
};
