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

export const deleteProductCollection = ({ collectionId }: { collectionId: string | number }) => {
    return api.delete(`/inventory/collections/${collectionId}`);
};

type UseDeleteProductCollectionOptions = {
    mutationConfig?: MutationConfig<typeof deleteProductCollection>;
};

export const useDeleteProductCollection = ({ mutationConfig }: UseDeleteProductCollectionOptions = {}) => {
    const queryClient = useQueryClient();

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

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