import { productApi } from "@/api/product-api.service";
import { delay } from "@/features/app";
import { type IPost } from "@/lib/utils";
import { useQuery } from "@tanstack/react-query";

const usePost = (post?: IPost) => {
  const { isLoading, refetch, ...query } = useQuery({
    queryKey: ["product", post?.id],
    queryFn: async () => {
      if (!post) return null;

      await delay(300); // Giả lập delay để thấy loading

      // const data = await productApi.apiRequest("getProductLocalServer", post);

      // if (data) return data;

      const res = await productApi.apiRequest("get", post);
      return res;
    },
  });

  return { isLoading, refetch, ...query };
};

export default usePost;
