import "@adyen/adyen-web/dist/adyen.css";
import AdyenCheckout from "@adyen/adyen-web/modern";
import { Loading } from "./accelbyte-widgets136.mjs";
import { AppUrlParamContext } from "./accelbyte-widgets574.mjs";
import { PaymentAlert } from "./accelbyte-widgets493.mjs";
import { sendMessageToParentWindow, PAYMENT_STATION_ACTION } from "./accelbyte-widgets496.mjs";
import { Ctx } from "./accelbyte-widgets33.mjs";
import { AlertType } from "./accelbyte-widgets128.mjs";
import { Platform } from "@accelbyte/sdk-platform";
import React__default, { useContext, useState, useEffect } from "react";
import { z } from "zod";
import styles from "./accelbyte-widgets687.mjs";
import "./accelbyte-widgets688.mjs";
const ADYEN_CHECKOUT_ELEMENT_ID = "adyenCheckout", AdyenPaymentStatus = z.enum(["Authorised", "Refused"]), Environment = z.enum(["test", "live"]), COMMON_ERROR_MSG = "Something went wrong, Please retry or contact our administrator if the problem persist", AdyenHandler = (props) => {
  const webSdk = Ctx.getAppState().getSdk(), { paymentOrderNo } = useContext(AppUrlParamContext), [paymentProviderPublicConfig, setPaymentProviderPublicConfig] = useState({
    value: null,
    isLoading: !1,
    error: null
  }), [isPaymentRefused, setIsPaymentRefused] = useState(!1), [paymentError, setPaymentError] = useState(""), [isLoadingComponent, setIsLoadingComponent] = useState(!1), loadPaymentProviderPublicConfig = async () => {
    const {
      paymentOrderDetails: { region: e, sandbox: o },
      paymentUrl: { paymentProvider: t }
    } = props;
    if (!paymentProviderPublicConfig.isLoading) {
      setPaymentProviderPublicConfig({ value: null, isLoading: !0, error: null });
      try {
        const n = await Platform.PaymentStationApi(webSdk).getPaymentPublicconfig({
          paymentProvider: t,
          region: e || null,
          sandbox: o
        });
        setPaymentProviderPublicConfig({ value: n, isLoading: !1, error: null });
      } catch (n) {
        console.error(n), setPaymentProviderPublicConfig({ error: n, isLoading: !1, value: null });
      }
    }
  };
  return useEffect(() => {
    async function initData() {
      const {
        paymentOrderDetails: { sandbox },
        paymentUrl: { sessionId, sessionData }
      } = props;
      if (paymentProviderPublicConfig.value) {
        let settingsObj = {};
        try {
          settingsObj = eval(`(${paymentProviderPublicConfig.value.dropInSettings})`);
        } catch (e) {
          console.error("Invalid settings object");
        }
        const clientKey = paymentProviderPublicConfig.value.clientKey, configuration = {
          environment: sandbox ? Environment.enum.test : Environment.enum.live,
          clientKey: typeof clientKey == "string" ? clientKey : void 0,
          session: {
            id: sessionId,
            sessionData
          },
          beforeSubmit: async (e, o, t) => {
            const n = await Platform.PaymentStationApi(webSdk).getInfoPayment_ByPaymentOrderNo(paymentOrderNo);
            if (n && n.charging) {
              t.reject(), setPaymentError("Please do not pay for the order repeatedly.");
              return;
            }
            t.resolve(e);
          },
          onPaymentCompleted: (e, o) => {
            const t = e.resultCode;
            t === AdyenPaymentStatus.enum.Authorised ? sendMessageToParentWindow({ message: { action: PAYMENT_STATION_ACTION.enum.complete } }) : t === AdyenPaymentStatus.enum.Refused && setPaymentError("The payment was refused, please try the payment again, you can choose a different payment method or card.");
          },
          onError: (e, o) => {
            console.error(e.name, e.message, e.stack, o);
          },
          paymentMethodsConfiguration: {
            card: {
              hasHolderName: !0,
              holderNameRequired: !0,
              billingAddressRequired: !0
            }
          },
          ...settingsObj
        };
        setIsLoadingComponent(!0);
        const checkout = await AdyenCheckout(configuration);
        checkout.create("dropin", {
          onReady: () => {
            setIsLoadingComponent(!1);
          }
        }).mount(`#${ADYEN_CHECKOUT_ELEMENT_ID}`);
      }
    }
    initData();
  }, [paymentProviderPublicConfig.value]), useEffect(() => {
    loadPaymentProviderPublicConfig();
  }, []), isPaymentRefused ? /* @__PURE__ */ React__default.createElement(
    PaymentAlert,
    {
      type: AlertType.warning,
      onRetry: () => {
        setIsPaymentRefused(!1), loadPaymentProviderPublicConfig();
      },
      message: COMMON_ERROR_MSG
    }
  ) : paymentError ? /* @__PURE__ */ React__default.createElement(
    PaymentAlert,
    {
      type: AlertType.warning,
      onRetry: () => {
        setPaymentError(""), loadPaymentProviderPublicConfig();
      },
      message: paymentError
    }
  ) : paymentProviderPublicConfig.error ? /* @__PURE__ */ React__default.createElement(
    PaymentAlert,
    {
      type: AlertType.warning,
      message: paymentProviderPublicConfig.error.message ? `${paymentProviderPublicConfig.error.message} Please contact our support team for further information` : COMMON_ERROR_MSG,
      onRetry: loadPaymentProviderPublicConfig
    }
  ) : /* @__PURE__ */ React__default.createElement(React__default.Fragment, null, (paymentProviderPublicConfig.isLoading || isLoadingComponent) && /* @__PURE__ */ React__default.createElement(Loading, null), /* @__PURE__ */ React__default.createElement("div", { className: styles.adyenIntent, id: ADYEN_CHECKOUT_ELEMENT_ID }));
};
export {
  AdyenHandler as default
};
