"use strict";
/**
 * Copyright © 2018 Emu Analytics
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
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 });
exports.handler = exports.builder = exports.desc = exports.command = void 0;
const utils_1 = require("../../utils");
const inquirer = __importStar(require("inquirer"));
exports.command = 'create';
exports.desc = 'Create a new API key';
const builder = (args) => {
    return args
        .option('id', { describe: 'ID for new API key', type: 'string' })
        .option('applicationId', { describe: 'Application ID for new API key', type: 'string' })
        .option('description', { describe: 'Description for new API key', type: 'string' })
        .option('type', { alias: 't', describe: 'Type of key', type: 'string', choices: ['app', 'master'] })
        .option('permissions', { describe: 'Permissions for new API key', type: 'string' })
        .option('interactive', {
        alias: 'i',
        describe: 'Show interactive prompts',
        type: 'boolean',
        default: 'true'
    });
};
exports.builder = builder;
const handler = (params) => __awaiter(void 0, void 0, void 0, function* () {
    try {
        if (params.interactive) {
            const answers = yield inquirer.prompt([
                {
                    type: 'input',
                    name: 'id',
                    message: 'ID of new key (leave blank to auto-generate)',
                    default: params.id,
                    validate: validateUuid
                },
                {
                    type: 'input',
                    name: 'description',
                    message: 'Name for new API key',
                    default: params.description
                },
                {
                    type: 'list',
                    name: 'type',
                    message: 'Type of API key to create',
                    choices: ['app', 'master'],
                    default: params.type
                },
                {
                    type: 'input',
                    name: 'permissions',
                    message: 'Permissions for new API key (JSON)',
                    default: params.permissions,
                    validate: (input) => {
                        try {
                            JSON.parse(input);
                            return true;
                        }
                        catch (e) {
                            return false;
                        }
                    }
                },
                {
                    type: 'confirm',
                    name: 'overrideApplicationId',
                    message: 'Override Application ID',
                    default: false,
                    when: ({ type }) => type === 'app'
                },
                {
                    type: 'input',
                    name: 'applicationId',
                    message: 'Application ID of new key',
                    default: params.applicationId,
                    validate: validateUuid,
                    when: ({ overrideApplicationId }) => overrideApplicationId
                }
            ]);
            params.id = answers.id;
            params.description = answers.description;
            params.type = answers.type;
            params.permissions = answers.permissions;
            params.applicationId = answers.applicationId;
        }
        let apiKey = {
            description: params.description,
            type: params.type,
            permissions: JSON.parse(params.permissions)
        };
        if (params.id) {
            apiKey.id = params.id;
        }
        if (params.applicationId) {
            apiKey.applicationId = params.applicationId;
        }
        apiKey = yield params.client.apiKeys.create(apiKey);
        (0, utils_1.logObject)(apiKey, 'table');
    }
    catch (e) {
        (0, utils_1.logError)(e);
    }
});
exports.handler = handler;
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