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

export const deleteProductSize = ({ sizeId }: { sizeId: string | number }) => {
    return api.delete(`/inventory/sizes/${sizeId}`);
};

type UseDeleteProductSizeOptions = {
    mutationConfig?: MutationConfig<typeof deleteProductSize>;
};

export const useDeleteProductSize = ({ mutationConfig }: UseDeleteProductSizeOptions = {}) => {
    const queryClient = useQueryClient();

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

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