import { fromPromise } from "xstate";

import type {
  PrivateAlbumsResponse,
  PrivateAlbumUnlockResponse,
} from "@/data/schemas/private-zone";
import { getPrivateZoneRepository } from "@/data/repositories/private_zone_repository";
import { Result } from "@/utils/result";

import { PRIVATE_ZONE_PAGE_SIZE } from "../../helper/albums";

export const loadPrivateAlbumsActor =
  fromPromise<PrivateAlbumsResponse, { characterId: string }>(async ({
    input,
  }) => {
    const result = await getPrivateZoneRepository().getAlbums({
      characterId: input.characterId,
      limit: PRIVATE_ZONE_PAGE_SIZE,
    });
    if (Result.isErr(result)) throw result.error;
    return result.data;
  });

export const unlockPrivateAlbumActor = fromPromise<
  PrivateAlbumUnlockResponse,
  { albumId: string; expectedCost: number }
>(async ({ input }) => {
  const result = await getPrivateZoneRepository().unlockAlbum(
    input.albumId,
    input.expectedCost,
  );
  if (Result.isErr(result)) throw result.error;
  return result.data;
});
