import { map } from 'rxjs/operators';
import { gql, mutateGraphQL } from '../../backend/graphql';
import { eventLogger } from '../../tracking/eventLogger';
import { createAggregateError } from '../../util/errors';
/**
 * Sends a GraphQL mutation to update a user's profile
 */
export function updateUser(user, args) {
    return mutateGraphQL(gql `
            mutation updateUser($user: ID!, $username: String, $displayName: String, $avatarURL: String) {
                updateUser(user: $user, username: $username, displayName: $displayName, avatarURL: $avatarURL) {
                    alwaysNil
                }
            }
        `, Object.assign({}, args, { user })).pipe(map(({ data, errors }) => {
        if (!data || !data.updateUser) {
            eventLogger.log('UpdateUserFailed');
            throw createAggregateError(errors);
        }
        eventLogger.log('UserProfileUpdated');
    }));
}
export function updatePassword(args) {
    return mutateGraphQL(gql `
            mutation updatePassword($oldPassword: String!, $newPassword: String!) {
                updatePassword(oldPassword: $oldPassword, newPassword: $newPassword) {
                    alwaysNil
                }
            }
        `, args).pipe(map(({ data, errors }) => {
        if (!data || !data.updatePassword) {
            eventLogger.log('UpdatePasswordFailed');
            throw createAggregateError(errors);
        }
        eventLogger.log('PasswordUpdated');
    }));
}
/**
 * Set the verification state for a user email address.
 *
 * @param user the user's GraphQL ID
 * @param email the email address to edit
 * @param verified the new verification state for the user email
 */
export function setUserEmailVerified(user, email, verified) {
    return mutateGraphQL(gql `
            mutation SetUserEmailVerified($user: ID!, $email: String!, $verified: Boolean!) {
                setUserEmailVerified(user: $user, email: $email, verified: $verified) {
                    alwaysNil
                }
            }
        `, { user, email, verified }).pipe(map(({ data, errors }) => {
        if (!data || (errors && errors.length > 0)) {
            throw createAggregateError(errors);
        }
    }));
}
/**
 * Log a user action (used to allow site admins on a Sourcegraph instance
 * to see a count of unique users on a daily, weekly, and monthly basis).
 *
 * Not used at all for public/sourcegraph.com usage.
 */
export function logUserEvent(event) {
    if (window.context.sourcegraphDotComMode) {
        return;
    }
    mutateGraphQL(gql `
            mutation logUserEvent($event: UserEvent!, $userCookieID: String!) {
                logUserEvent(event: $event, userCookieID: $userCookieID) {
                    alwaysNil
                }
            }
        `, { event, userCookieID: eventLogger.getAnonUserID() })
        .pipe(map(({ data, errors }) => {
        if (!data || (errors && errors.length > 0)) {
            throw createAggregateError(errors);
        }
        return;
    }))
        .subscribe();
}
//# sourceMappingURL=backend.js.map