"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 dataset';
exports.builder = (args) => {
    return args
        .positional('id', { describe: 'Dataset id', type: 'string' })
        .option('name', { alias: 'n', describe: 'Name for new dataset', type: 'string' })
        .option('description', { alias: 'd', describe: 'Description for new dataset', type: 'string' })
        .option('type', { alias: 't', describe: 'Type of dataset', type: 'string', choices: ['table', 'sql'] })
        .option('source', { alias: 's', description: 'Dataset source', type: 'string' })
        .option('interactive', {
        alias: 'i',
        describe: 'Show interactive prompts',
        type: 'boolean',
        default: 'true'
    })
        .option('autodetect', { describe: 'Autodetect geometry', type: 'boolean' })
        .option('srid', { describe: 'Geometry SRID (ignored when autodetect=true)', type: 'number' })
        .option('extent', {
        description: 'Geometry extent x1,y1,x2,y2 (lng/lat) (ignored when autodetect=true)',
        type: 'array'
    });
};
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 dataset', default: params.name },
                {
                    type: 'input',
                    name: 'description',
                    message: 'Description for new dataset',
                    default: params.description
                },
                {
                    type: 'confirm',
                    name: 'autodetect',
                    message: 'Autodetect geometry?',
                    default: params.autodetect
                },
                {
                    type: 'input',
                    name: 'srid',
                    message: 'Geometry SRID',
                    default: params.srid,
                    validate: val => utils_1.isNumeric(val) || 'Enter a number',
                    when: ({ autodetect }) => autodetect === false
                },
                {
                    type: 'input',
                    name: 'extent',
                    message: 'Map extent (x1 y1 x2 y2) (in SRID units)',
                    default: utils_1.extentToString(params.extent || [-180, -90, 180, 90]),
                    validate: val => utils_1.isValidExtent(val) || 'Enter lng/lat bounding box as x1 y1 x2 y2',
                    when: ({ autodetect }) => autodetect === false
                },
                {
                    type: 'list',
                    name: 'type',
                    message: 'Type of dataset to create',
                    choices: ['table', 'sql'],
                    default: params.type
                },
                {
                    type: 'input',
                    name: 'source',
                    message: 'Table name',
                    default: params.source,
                    when: ({ type }) => type === 'table'
                },
                {
                    type: 'editor',
                    name: 'source',
                    message: 'SQL query',
                    default: params.source,
                    when: ({ type }) => type === 'sql'
                }
            ]);
            params.name = answers.name;
            params.description = answers.description;
            params.autodetect = answers.autodetect;
            params.srid = answers.autodetect ? undefined : answers.srid;
            params.extent = answers.autodetect ? undefined : utils_1.extentFromString(answers.extent);
            params.type = answers.type;
            params.source = answers.source.trim();
        }
        let newDataset = {
            id: params.id,
            name: params.name,
            description: params.description,
            type: params.type,
            source: params.source,
            options: {
                autodetect: params.autodetect,
                srid: params.srid,
                extent: params.extent
            }
        };
        newDataset = yield params.client.datasets.create(newDataset);
        if (params.format === 'table') {
            utils_1.logObject(newDataset, 'table', ['id', 'name', 'description', 'type']);
        }
        else {
            utils_1.logObject(newDataset, 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