import { useCallback as u } from "react";
import { z as y } from "zod";
import { F as o, e as P, u as S, f as I, W as m } from "./FetchConfigsUtils-476a602b.mjs";
import { Platform as p } from "@accelbyte/sdk-platform";
y.enum(["locationChange", "heightChange"]);
class h {
  static sendMessageToParentWindow({ message: s, channel: r, origin: i = "*" }) {
    const l = JSON.stringify({ ...s, channel: r });
    if (!this.isInIframe()) {
      console.log("Message to parent window ignored: ", { ...s, channel: r });
      return;
    }
    window.parent.postMessage(l, i);
  }
  static isInIframe() {
    try {
      return window.self !== window.top;
    } catch {
      return !0;
    }
  }
}
const g = y.enum(["complete", "cancel", "requestData"]);
y.enum(["paymentSuccess", "paymentGenericError"]);
const A = {
  paymentAccounts: null,
  paymentAccountsFetchStatus: o.IDLE,
  paymentAccountsError: null,
  deletePaymentAccountFetchStatus: o.IDLE,
  deletePaymentAccountError: null,
  paymentData: {},
  paymentStationMessage: null
}, w = P(A);
function C() {
  const [c] = S(), [s, r] = I(w), i = u(
    async (t) => {
      let a = null, n = null;
      try {
        return r((e) => ({
          ...e,
          paymentAccountsFetchStatus: o.FETCHING,
          paymentAccountsError: null
        })), a = await p.PaymentAccountApi(c).getPaymentAccounts_ByUserId(t), { data: a };
      } catch (e) {
        return n = m.getErrorType(e), { error: n };
      } finally {
        r((e) => ({
          ...e,
          paymentAccounts: a,
          paymentAccountsError: n,
          paymentAccountsFetchStatus: o.IDLE
        }));
      }
    },
    [c]
  ), l = u(
    async (t, a) => {
      let n = null;
      try {
        return r((e) => ({
          ...e,
          deletePaymentAccountFetchStatus: o.DELETING,
          deletePaymentAccountError: null
        })), await p.PaymentAccountApi(c).deletePaymentAccount_ByUserId_ByType_ById(t, a.type, a.id), { data: !0 };
      } catch (e) {
        return n = m.getErrorType(e), { error: n };
      } finally {
        r((e) => ({
          ...e,
          deletePaymentAccountError: n,
          deletePaymentAccountFetchStatus: o.IDLE
        }));
      }
    },
    [c]
  ), d = u(({ message: t, origin: a = "*" }) => {
    let n = "";
    if (typeof t == "object") {
      if (!g.options.includes(t == null ? void 0 : t.action))
        return;
      n = JSON.stringify(t);
    } else
      typeof t == "string" && (n = t);
    if (!h.isInIframe()) {
      r((e) => ({ ...e, paymentStationMessage: n }));
      return;
    }
    window.parent.postMessage(n, a);
  }, []), f = u((t) => {
    r((a) => ({ ...a, paymentData: t }));
  }, []), E = u(() => {
    r(A);
  }, []);
  return {
    state: s,
    mutations: {
      getPaymentAccounts: i,
      deletePaymentAccount: l,
      setPaymentData: f,
      setPaymentStationMessage: d,
      resetState: E
    }
  };
}
export {
  h as C,
  g as P,
  C as u
};
