"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 = 'create';
exports.desc = 'Create a new API key';
exports.builder = (args) => {
    return args
        .option('id', { describe: 'ID for new database', type: 'string' })
        .option('applicationId', { describe: 'Application ID for new database', type: 'string' })
        .option('name', { describe: 'Name for new database', type: 'string' })
        .option('description', { describe: 'Description for new database', type: 'string' })
        .option('driver', { describe: 'Database driver', type: 'string' })
        .option('db-host', { describe: 'Database host', type: 'string' })
        .option('db-port', { describe: 'Database port', type: 'number', default: 5432 })
        .option('db-database', { describe: 'Database name', type: 'string' })
        .option('db-user', { describe: 'Database user', type: 'string' })
        .option('db-password', { describe: 'Database password', type: 'string' })
        .option('default', { describe: 'Default database', type: 'boolean', default: true })
        .option('interactive', {
        alias: 'i',
        describe: 'Show interactive prompts',
        type: 'boolean',
        default: 'true'
    });
};
exports.handler = (params) => __awaiter(void 0, void 0, void 0, function* () {
    try {
        if (params.interactive) {
            const drivers = yield params.client.databases.drivers();
            const answers = yield inquirer.prompt([
                {
                    type: 'input',
                    name: 'id',
                    message: 'ID of new database (leave blank to auto-generate)',
                    default: params.id,
                    validate: validateUuid
                },
                {
                    type: 'input',
                    name: 'name',
                    message: 'Name for new database',
                    default: params.description
                },
                {
                    type: 'input',
                    name: 'description',
                    message: 'Description for new database',
                    default: params.description
                },
                {
                    type: 'list',
                    name: 'driver',
                    message: 'Database driver',
                    choices: drivers,
                    default: params.driver
                },
                {
                    type: 'input',
                    name: 'dbHost',
                    message: 'Database host',
                    default: params.dbHost
                },
                {
                    type: 'number',
                    name: 'dbPort',
                    message: 'Database port',
                    default: params.dbPort
                },
                {
                    type: 'input',
                    name: 'dbDatabase',
                    message: 'Database',
                    default: params.dbDatabase
                },
                {
                    type: 'input',
                    name: 'dbUser',
                    message: 'Database user',
                    default: params.dbUser
                },
                {
                    type: 'password',
                    name: 'dbPassword',
                    message: 'Database password',
                    default: params.dbPassword
                },
                {
                    type: 'confirm',
                    name: 'default',
                    message: 'Is this the default database?',
                    default: params.default
                },
                {
                    type: 'confirm',
                    name: 'overrideApplicationId',
                    message: 'Override Application ID',
                    default: false
                },
                {
                    type: 'input',
                    name: 'applicationId',
                    message: 'Application ID of new key',
                    default: params.applicationId,
                    validate: validateUuid,
                    when: ({ overrideApplicationId }) => overrideApplicationId
                }
            ]);
            params.id = answers.id;
            params.name = answers.name;
            params.description = answers.description;
            params.driver = answers.driver;
            params.dbHost = answers.dbHost;
            params.dbPort = answers.dbPort;
            params.dbDatabase = answers.dbDatabase;
            params.dbUser = answers.dbUser;
            params.dbPassword = answers.dbPassword;
            params.default = answers.default;
            params.applicationId = answers.applicationId;
        }
        let database = {
            name: params.name,
            description: params.description,
            type: params.driver,
            host: params.dbHost,
            port: params.dbPort,
            db: params.dbDatabase,
            user: params.dbUser,
            password: params.dbPassword,
            default: params.default
        };
        if (params.id) {
            database.id = params.id;
        }
        if (params.applicationId) {
            database.applicationId = params.applicationId;
        }
        database = yield params.client.databases.create(database);
        database.password = '*****';
        utils_1.logObject(database, 'table');
    }
    catch (e) {
        utils_1.logError(e);
    }
});
function validateUuid(input) {
    if (!input) {
        return true;
    }
    return !!input.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i);
}
//# sourceMappingURL=create.js.map