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

export const createProductColour = ({ data }: { data: CreateProductColourFormValues }): Promise<ProductColour> => {
    return api.post('/inventory/colours', data);
};

type UseCreateProductColourOptions = {
    mutationConfig?: MutationConfig<typeof createProductColour>;
};

export const useCreateProductColour = ({ mutationConfig }: UseCreateProductColourOptions = {}) => {
    const queryClient = useQueryClient();

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

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