import { useMutation, useQueryClient } from '@tanstack/react-query';
import { api } from '../../../lib/apiClient';
import { MutationConfig } from '../../../lib/reactQuery';
import { getProductColoursQueryOptions } from './getProductColours';

export const deleteProductColour = ({ colourId }: { colourId: string | number }) => {
    return api.delete(`/inventory/colours/${colourId}`);
};

type UseDeleteProductColourOptions = {
    mutationConfig?: MutationConfig<typeof deleteProductColour>;
};

export const useDeleteProductColour = ({ mutationConfig }: UseDeleteProductColourOptions = {}) => {
    const queryClient = useQueryClient();

    const { onSuccess, ...restConfig } = mutationConfig || {};

    return useMutation({
        mutationFn: deleteProductColour,
        onSuccess: (...args) => {
            queryClient.invalidateQueries({
                queryKey: getProductColoursQueryOptions().queryKey,
            });
            onSuccess?.(...args);
        },
        ...restConfig,
    });
};
