"use strict";
/**
 * Copyright © 2018 Emu Analytics
 */
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("../../utils");
const inquirer = require("inquirer");
exports.command = ['update <email>'];
exports.desc = 'Update a user';
exports.builder = (args) => {
    return args
        .option('firstName', { description: 'First name', type: 'string' })
        .option('lastName', { description: 'Last name', type: 'string' })
        .option('roles', { alias: 'r', description: 'Roles (comma-separated)', type: 'array' })
        .option('emailVerified', { description: 'Email verified', type: 'boolean' })
        .option('enabled', { alias: 'e', description: 'Login enabled', type: 'boolean' })
        .option('interactive', {
        alias: 'i',
        describe: 'Show interactive prompts',
        type: 'boolean',
        default: 'true'
    })
        .positional('email', { describe: 'Email (or unique prefix)', type: 'string' });
};
function handler(params) {
    return __awaiter(this, void 0, void 0, function* () {
        const client = params.client;
        const email = params.email;
        try {
            const user = yield utils_1.resolveResource(email, client.auth.users, 'email');
            if (params.interactive) {
                const answers = yield inquirer.prompt([
                    {
                        type: 'input',
                        name: 'firstName',
                        message: 'First name',
                        default: params.firstName || user.firstName
                    },
                    { type: 'input', name: 'lastName', message: 'Last name', default: params.lastName || user.lastName },
                    {
                        type: 'input',
                        name: 'roles',
                        message: 'Roles (comma separated)',
                        default: (params.roles || user.roles).join(', ')
                    },
                    {
                        type: 'confirm',
                        name: 'emailVerified',
                        message: 'Verified?',
                        default: params.hasOwnProperty('emailVerified') ? params.emailVerified : user.emailVerified
                    },
                    {
                        type: 'confirm',
                        name: 'enabled',
                        message: 'Enabled?',
                        default: params.hasOwnProperty('enabled') ? params.enabled : user.enabled
                    }
                ]);
                params.firstName = answers.firstName;
                params.lastName = answers.lastName;
                params.roles = answers.roles.split(',').map((r) => r.trim());
                params.emailVerified = answers.emailVerified;
                params.enabled = answers.enabled;
            }
            const updatedUser = yield client.auth.users.update(user.id, {
                firstName: params.firstName,
                lastName: params.lastName,
                roles: params.roles,
                emailVerified: params.emailVerified,
                enabled: params.enabled
            });
            if (params.format === 'table') {
                utils_1.logObject(updatedUser, 'table');
            }
            else {
                utils_1.logObject(updatedUser, 'json');
            }
        }
        catch (e) {
            utils_1.logError(e);
        }
    });
}
exports.handler = handler;
//# sourceMappingURL=update.js.map