"use client";

import type { ChatMediaKind } from "@/data/dto/chat";
import { getChatRepository } from "@/data/repositories/chat_repository";
import { Result, type Result as ResultT } from "@/utils";

export interface ResolveCachedChatMediaBlobInput {
  messageId: string;
  kind: ChatMediaKind;
}

export interface CacheRemoteChatMediaBlobInput
  extends ResolveCachedChatMediaBlobInput {
  remoteUrl: string;
}

export async function resolveCachedChatMediaBlob(
  input: ResolveCachedChatMediaBlobInput,
): Promise<ResultT<Blob | null>> {
  const result = await getChatRepository().getCachedMedia(input);
  if (Result.isErr(result)) return result;
  return Result.ok(result.data?.blob ?? null);
}

export async function cacheRemoteChatMediaBlob(
  input: CacheRemoteChatMediaBlobInput,
): Promise<ResultT<Blob>> {
  const result = await getChatRepository().cacheRemoteMedia(input);
  if (Result.isErr(result)) return result;
  return Result.ok(result.data.blob);
}
