import { fromPromise } from "xstate";

import type {
  GiftProductsResponse,
  TipMessageResponse,
} from "@/data/schemas/payment";
import { getPaymentRepository } from "@/data/repositories/payment_repository";
import { Result } from "@/utils/result";

export const loadGiftProductsActor = fromPromise<
  GiftProductsResponse,
  { characterId: string }
>(async ({ input }) => {
  if (!input.characterId) throw new Error("Missing gift recipient character.");
  const result = await getPaymentRepository().getGiftProducts(
    input.characterId,
  );
  if (Result.isErr(result)) throw result.error;
  return result.data;
});

export const loadTipMessageActor = fromPromise<
  TipMessageResponse,
  { orderId: string }
>(async ({ input }) => {
  if (!input.orderId) throw new Error("Missing paid Tip order id.");
  const result = await getPaymentRepository().getTipMessage(input.orderId);
  if (Result.isErr(result)) throw result.error;
  return result.data;
});
