import { useMutation } from '@apollo/client';
import { useToast } from '@theorchard/suite-components';
import { flattenError } from '@theorchard/suite-frontend';
import RenameArtistMutation from './RenameArtist.gql';
import type {
    RenameLabelParticipant,
    RenameLabelParticipantVariables,
} from './__generated__/RenameArtist';

export const useRenameArtistMutation = (
    variables: RenameLabelParticipantVariables
) => {
    const toast = useToast();
    const [renameLabelParticipant, renameLabelParticipantResult] = useMutation<
        RenameLabelParticipant,
        RenameLabelParticipantVariables
    >(RenameArtistMutation, {
        variables,
        onCompleted: () =>
            toast($t('tools.artistNameUpdateSidecar.artistNameSuccess'), {
                variant: 'success',
            }),
        onError: () =>
            toast($t('tools.artistNameUpdateSidecar.artistNameFailed'), {
                variant: 'error',
            }),
    });

    const { data, loading, error } = renameLabelParticipantResult;

    return {
        renameLabelParticipant,
        data: data?.renameLabelParticipant,
        loading,
        error: error && flattenError(error),
    };
};
