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

export const deleteProductCategory = ({ categoryId }: { categoryId: string | number }) => {
    return api.delete(`/inventory/categories/${categoryId}`);
};

type UseDeleteProductCategoryOptions = {
    mutationConfig?: MutationConfig<typeof deleteProductCategory>;
};

export const useDeleteProductCategory = ({ mutationConfig }: UseDeleteProductCategoryOptions = {}) => {
    const queryClient = useQueryClient();

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

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