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

const useActionPostMutation = (options?: { onSetted: () => void }) => {
  return useMutation({
    mutationKey: ["action-mutaions"],
    mutationFn: async (data: IPost) => {
      if (data.listedProducts?.length) {
        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,
      });
    },
    onSettled() {
      options?.onSetted?.();
    },
  });
};

export default useActionPostMutation;
