"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 <name>';
exports.desc = 'Create a new Flo.w application';
exports.builder = (args) => {
    return args
        .positional('name', { describe: 'Name for new application', type: 'string' })
        .option('description', { alias: 'd', describe: 'Description for new application', type: 'string' })
        .option('contact', { alias: 'c', describe: 'Contact email for new application', type: 'string' })
        .option('developer-key', {
        describe: 'Create default developer key',
        type: 'boolean',
        default: 'true'
    })
        .option('public-key', {
        describe: 'Create default public key',
        type: 'boolean',
        default: 'false'
    })
        .option('authorized-domains', {
        describe: 'Authorized email domains',
        type: 'array',
        default: ['emu-analytics.com']
    })
        .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 answers = yield inquirer.prompt([
                { type: 'input', name: 'name', message: 'Name for new application', default: params.name },
                {
                    type: 'input',
                    name: 'description',
                    message: 'Description for new application',
                    default: params.description
                },
                {
                    type: 'input',
                    name: 'contact',
                    message: 'Contact email for new application',
                    default: params.contact
                },
                {
                    type: 'confirm',
                    name: 'developerKey',
                    message: 'Create default developer key (all permissions)?',
                    default: params.developerKey
                },
                {
                    type: 'confirm',
                    name: 'publicKey',
                    message: 'Create default public key (minimal permissions)?',
                    default: params.publicKey
                },
                {
                    type: 'input',
                    name: 'authorizedDomains',
                    message: 'Authorized email domains (comma-separated)',
                    default: params.authorizedDomains.join(', ')
                }
            ]);
            params.name = answers.name;
            params.description = answers.description;
            params.contact = answers.contact;
            params.developerKey = answers.developerKey;
            params.publicKey = answers.publicKey;
            params.authorizedDomains = answers.authorizedDomains.split(',').map((d) => d.trim());
        }
        let application = {
            name: params.name,
            description: params.description,
            contact: params.contact,
            metadata: {
                auth: {
                    authorisedEmailDomains: params.authorizedDomains
                },
                datasetAccessOptions: {
                    allowFn: true,
                    maxQueryLimit: 10000,
                    defaultQueryLimit: 1000,
                    statementTimeout: 10000
                }
            }
        };
        application = yield params.client.applications.create(application);
        let developerKey;
        let publicKey;
        if (params.developerKey) {
            developerKey = {
                description: `${params.name} developer key`,
                applicationId: application.id,
                type: 'app',
                permissions: { '*': { '*': '*' } }
            };
            developerKey = yield params.client.apiKeys.create(developerKey);
        }
        if (params.publicKey) {
            publicKey = {
                description: `${params.name} public key`,
                applicationId: application.id,
                type: 'app',
                // tslint:disable-next-line:object-literal-key-quotes
                permissions: { '*': { execute: '*' }, datasets: { read: '*' }, tiles: { read: '*' } }
            };
            publicKey = yield params.client.apiKeys.create(publicKey);
            application.metadata.auth.defaultApiKey = publicKey.id;
            yield params.client.applications.update(application.id, application);
        }
        const results = {
            applicationId: application.id,
            developerApiKey: developerKey ? developerKey.id : 'none',
            publicApiKey: publicKey ? publicKey.id : 'none'
        };
        utils_1.log(`Application ${application.name} created`);
        utils_1.logObject(results, 'table');
    }
    catch (e) {
        utils_1.logError(e);
    }
});
//# sourceMappingURL=create.js.map