"use client";

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

import { getSplashLatestMessagePreview } from "./splash_latest_message_preview";

export async function fetchSplashLatestMessagePreview(): Promise<
  ResultT<string | null>
> {
  const result = await getChatRepository().getHistory(1, 0);
  if (Result.isErr(result)) return Result.err(result.error);

  const latestMessage = result.data.messages[0] ?? null;
  return Result.ok(getSplashLatestMessagePreview(latestMessage));
}
