import { productApi } from "@/api/product-api.service";
import { mapToIPost, type IPost } from "@/lib/utils";
import { useMutation } from "@tanstack/react-query";

const useActionPostMutation = () => {
  return useMutation({
    mutationKey: ["action-mutaions"],
    mutationFn: async (data: IPost) => {
      if (data.status) {
        return productApi.apiRequest("unlist", data);
      }

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

      // if (localServerPost && (localServerPost as any)?.mapped) {
      //   return productApi.apiRequest("publist", localServerPost);
      // }

      const res = await productApi.apiRequest("get", data);

      if (!res || !(res as any)?.data) return;

      return productApi.apiRequest("publist", {
        ...data,
        images: mapToIPost({ ...(res as any)?.data }).images,
      });
    },
  });
};

export default useActionPostMutation;
