"use client";

import { useCallback } from "react";
import useAxios from "@/hooks/useAxios";
import { IMAGE_BASE_URL } from "@/components/main/BaseUrl";

export function useViewOnceMedia() {
  const { request } = useAxios();

  const openViewOnce = useCallback(
    async (messageId: string) => {
      const res = await request<{
        file: string;
        fileType: string;
        messageId: string;
      }>("POST", "/chat/view-once/open", { messageId });

      const path = res?.file || "";
      const url = path.startsWith("blob:")
        ? path
        : path.startsWith("http")
          ? path
          : IMAGE_BASE_URL + path;

      return { url, fileType: res?.fileType as string };
    },
    [request]
  );

  const completeViewOnce = useCallback(
    async (messageId: string) => {
      await request<{ deletedIds: string[] }>(
        "POST",
        "/chat/view-once/complete",
        { messageId }
      );
    },
    [request]
  );

  return { openViewOnce, completeViewOnce };
}
