import { AppUrlParamContext as S } from "./accelbyte-widgets574.mjs";
import { sendMessageToParentWindow as d, PAYMENT_STATION_ACTION as r } from "./accelbyte-widgets496.mjs";
import { Ctx as O } from "./accelbyte-widgets33.mjs";
import L from "./accelbyte-widgets700.mjs";
import T from "./accelbyte-widgets701.mjs";
import _ from "./accelbyte-widgets702.mjs";
import { Platform as k } from "@accelbyte/sdk-platform";
import e, { useContext as A, useState as g, useEffect as y } from "react";
import a from "./accelbyte-widgets703.mjs";
const v = 450, E = 600, I = [
  "resizable",
  "scrollbars",
  "status",
  `width=${v}`,
  `height=${E}`,
  `top=${(window.screen.height - E) / 4}`,
  `left=${(window.screen.width - v) / 2}`
].join(","), x = ({ onClick: s, onClose: c }) => /* @__PURE__ */ e.createElement("div", { className: a.overlay }, /* @__PURE__ */ e.createElement("div", { className: a.closeButton }, /* @__PURE__ */ e.createElement("button", { onClick: c }, /* @__PURE__ */ e.createElement("img", { src: _, alt: "close" }))), /* @__PURE__ */ e.createElement("div", { className: a.content }, /* @__PURE__ */ e.createElement("div", { className: a.logo }, /* @__PURE__ */ e.createElement("img", { src: T, alt: "paypal-logo" })), /* @__PURE__ */ e.createElement("div", { className: a.description }, "Don't see the secure PayPal browser? We'll help you re-launch the window to complete your purchase."), /* @__PURE__ */ e.createElement("button", { onClick: s }, "Click to Continue"))), Y = ({ paymentUrl: s }) => {
  const c = O.getAppState().getSdk(), { paymentOrderNo: P, returnUrl: h } = A(S), [t, m] = g(null), [l, u] = g({
    value: null,
    isLoading: !1,
    error: null
  }), C = async () => {
    if (!l.isLoading) {
      u({ value: null, isLoading: !0, error: null });
      try {
        const n = await k.PaymentStationApi(c).getStatusPayment_ByPaymentOrderNo(P);
        u({ value: n, isLoading: !1, error: null });
      } catch (n) {
        console.error(n), u({ error: n, value: null, isLoading: !1 });
      }
    }
  }, N = () => {
    if (l.isLoading || !s.paymentUrl)
      return;
    const n = window.open(s.paymentUrl, "paypal_child_window", I);
    if (n) {
      m(n);
      const i = setInterval(() => {
        (!n || n.closed) && (C(), m(null), clearInterval(i));
      }, 1e3);
    }
  }, W = () => {
    t && t.focus();
  }, b = () => {
    t && (t.close(), m(null));
  };
  return y(() => {
    const n = (p) => {
      var f, w;
      try {
        const o = JSON.parse(p.data);
        ((f = o == null ? void 0 : o.message) == null ? void 0 : f.action) === r.enum.complete && t ? (d({ message: { action: r.enum.complete } }), t.close()) : ((w = o == null ? void 0 : o.message) == null ? void 0 : w.action) === r.enum.cancel && t && (d({ message: { action: r.enum.cancel } }), t.close());
      } catch (o) {
        console.error(o);
      }
    }, i = (p) => {
      t && t.close();
    };
    return t ? (window.addEventListener("beforeunload", i), window.addEventListener("message", n), function() {
      t && t.close(), window.removeEventListener("beforeunload", i), window.removeEventListener("message", n);
    }) : function() {
    };
  }, [t, h]), y(() => {
    l.value && l.value.success && d({ message: { action: r.enum.complete } });
  }, [l]), /* @__PURE__ */ e.createElement("div", { className: a.wrapper }, /* @__PURE__ */ e.createElement("div", { className: a.paypalContainer }, /* @__PURE__ */ e.createElement("p", null, "Continuing will take you to your PayPal account.", /* @__PURE__ */ e.createElement("br", null), "Please make sure pop-ups aren't blocked."), /* @__PURE__ */ e.createElement("button", { onClick: () => N(), disabled: !!t || l.isLoading }, /* @__PURE__ */ e.createElement("span", null, "Pay with"), /* @__PURE__ */ e.createElement("img", { src: L, alt: "paypal logo" }))), t && /* @__PURE__ */ e.createElement(x, { onClick: W, onClose: b }));
};
export {
  Y as default
};
