import { CommonError as c } from "./accelbyte-widgets124.mjs";
import u from "./accelbyte-widgets210.mjs";
import { PaymentStatus as n } from "./accelbyte-widgets185.mjs";
import p from "classnames";
import o from "react";
import { z as y } from "zod";
var s;
const i = y.enum(["paymentSuccess", "paymentGenericError"]), w = u(
  (s = class extends o.Component {
    constructor(e) {
      super(e), this.paymentSessionCode = String(Math.round(Math.random() * 1e5)), this.extractPaymentResult = (r) => {
        try {
          if (this.isStringJson(r)) {
            const t = JSON.parse(r);
            if (t.action === "complete" || t.paymentStatus)
              return {
                orderNo: "",
                paymentStatus: n.enum.DONE
              };
            if (!!t.type && t.type === "paymentStationEvent") {
              const l = !!t.event.name && t.event.name;
              let m = null;
              switch (l) {
                case i.enum.paymentSuccess:
                  m = { orderNo: t.event.orderNo, paymentStatus: n.enum.DONE };
                  break;
                case i.enum.paymentGenericError:
                  m = { orderNo: t.event.orderNo, paymentStatus: n.enum.FAILED };
                  break;
              }
              return m;
            }
          }
          return null;
        } catch (t) {
          return console.error(t), {
            orderNo: "",
            paymentStatus: n.enum.FAILED
          };
        }
      }, this.randomizeNumber = () => String(Math.floor(Math.random() * 100)), this.reloadIframe = () => {
        this.setState({ iframeKey: this.randomizeNumber() });
      }, this.listenToPaymentCallback = (r) => {
        const { onPaymentProcessed: t } = this.props;
        try {
          const a = this.extractPaymentResult(r.data);
          if (a)
            switch (a.paymentStatus) {
              case n.enum.DONE:
                t(a.paymentStatus);
                break;
              case n.enum.FAILED:
                t(a.paymentStatus), this.reloadIframe();
                break;
            }
        } catch (a) {
          console.error(a);
        }
      }, this.state = {
        iframeKey: "0"
      };
    }
    getPaymentStationUrl() {
      try {
        const { returnUrl: e, order: r, paymentStationUrl: t } = this.props, a = new URL(t || r && r.paymentStationUrl || "");
        return e && a.searchParams.append("returnUrl", e), a.toString();
      } catch {
        return null;
      }
    }
    componentDidMount() {
      window.addEventListener("message", this.listenToPaymentCallback);
    }
    componentWillUnmount() {
      window.removeEventListener("message", this.listenToPaymentCallback);
    }
    isStringJson(e) {
      try {
        JSON.parse(e);
      } catch {
        return !1;
      }
      return !0;
    }
    render() {
      const e = this.getPaymentStationUrl();
      return e ? /* @__PURE__ */ o.createElement(
        "iframe",
        {
          key: this.state.iframeKey,
          className: p("paymentStation", this.props.className),
          title: "Pay station",
          src: e,
          sandbox: s.IFRAME_SANDBOX_OPTIONS
        }
      ) : /* @__PURE__ */ o.createElement("div", { className: "errorContainer" }, /* @__PURE__ */ o.createElement(c, { error: new Error() }));
    }
  }, s.IFRAME_SANDBOX_OPTIONS = [
    "allow-forms",
    "allow-modals",
    "allow-orientation-lock",
    "allow-pointer-lock",
    "allow-presentation",
    "allow-same-origin",
    "allow-scripts",
    "allow-popups"
  ].join(" "), s),
  (e, r) => e.order !== r.order
);
export {
  w as LegacyPaymentStation
};
