import { useMutation, useQuery } from "@tanstack/react-query";
import { useAuthToken } from "../hooks/use-root-data";
import {
  ShippingInfoCreate,
  ShippingInfoDetail,
} from "../../src/api/types";
import { createShippingManagementApi } from "~/utils/api-client-factory";

export function useCreateShippingInfo() {
  const token = useAuthToken();

  return useMutation({
    mutationFn: async (data: ShippingInfoCreate) => {
      const shippingApi = createShippingManagementApi(token);
      return await shippingApi.apiOrderV1ShippingInfoCreate({ data });
    },
  });
}

export function useShippingInfoDetails(shippingInfoId: string) {
  const token = useAuthToken();
  return useQuery<ShippingInfoDetail>({
    queryKey: ["shippingInfo", shippingInfoId],
    queryFn: async () => {
      const shippingApi = createShippingManagementApi(token);
      return await shippingApi.apiOrderV1ShippingInfoRead({ shippingInfoId });
    },
    enabled: !!shippingInfoId && !!token,
  });
}
