import { useMutation, useQueryClient } from '@tanstack/react-query';
import { api } from '../../../lib/apiClient';
import { MutationConfig } from '../../../lib/reactQuery';
import { ProductSize } from '../types/api';
import { getProductSizesQueryOptions } from './getProductSizes';
import { CreateProductSizeFormValues } from '../types/form';

export const createProductSize = ({ data }: { data: CreateProductSizeFormValues }): Promise<ProductSize> => {
    return api.post('/inventory/sizes', data);
};

type UseCreateProductSizeOptions = {
    mutationConfig?: MutationConfig<typeof createProductSize>;
};

export const useCreateProductSize = ({ mutationConfig }: UseCreateProductSizeOptions = {}) => {
    const queryClient = useQueryClient();

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

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