"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 flow_engine_client_1 = require("flow-engine-client");
const inquirer = require("inquirer");
exports.command = 'create <id>';
exports.desc = 'Create a new map style';
exports.builder = (args) => {
    return args
        .positional('id', { describe: 'Map style id', type: 'string' })
        .option('name', { alias: 'n', describe: 'Name for new style', type: 'string' })
        .option('description', { alias: 'd', describe: 'Description for new style', type: 'string' })
        .option('interactive', {
        alias: 'i',
        describe: 'Show interactive prompts',
        type: 'boolean',
        default: 'true'
    })
        .option('sourceId', {
        alias: 'sid',
        describe: 'ID of source style',
        type: 'string',
        default: 'darkmatter'
    })
        .option('sourceKey', {
        alias: 'skey',
        describe: 'API key to access source style',
        type: 'string',
        default: '6f19c5c7-1a9b-425b-84ab-09ba56e905b2'
    });
};
exports.handler = (params) => __awaiter(void 0, void 0, void 0, function* () {
    try {
        const styles = [].concat(yield getStyles(params.host, params.apiKey), yield getStyles(params.host, params.sourceKey));
        const styleChoices = styles.map(style => ({ name: style.name, value: style, short: style.name }));
        let sourceStyle = styles.find(style => style.id === params.sourceId);
        if (params.interactive) {
            const answers = yield inquirer.prompt([
                { type: 'input', name: 'name', message: 'Name for new style', default: params.name },
                {
                    type: 'input',
                    name: 'description',
                    message: 'Description for new style',
                    default: params.description
                },
                {
                    type: 'list',
                    name: 'sourceStyle',
                    message: 'Select the source style to copy',
                    choices: styleChoices,
                    default: sourceStyle
                }
            ]);
            params.name = answers.name;
            (params.description = answers.description), (params.sourceId = answers.sourceStyle.id);
            sourceStyle = styles.find(style => style.id === params.sourceId);
        }
        // Fix up style metadata
        sourceStyle.style.id = params.id;
        sourceStyle.style.name = params.name;
        sourceStyle.style.metadata.description = params.description;
        let newStyle = {
            id: params.id,
            name: params.name,
            description: params.description,
            type: 'plain',
            style: sourceStyle.style
        };
        newStyle = yield params.client.styles.create(newStyle);
        if (params.format === 'table') {
            utils_1.logObject(newStyle, 'table', ['id', 'name', 'description', 'type']);
        }
        else {
            utils_1.logObject(newStyle, params.format || 'json');
        }
    }
    catch (e) {
        utils_1.logError(e);
    }
});
/**
 * Retrieve style IDs for given apiKey
 *
 * @param apiKey
 */
function getStyles(host, apiKey) {
    return __awaiter(this, void 0, void 0, function* () {
        const tempClient = new flow_engine_client_1.Client(host, apiKey);
        return tempClient.styles.list();
    });
}
//# sourceMappingURL=create.js.map