"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());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.handler = exports.builder = exports.desc = exports.command = void 0;
const moment_1 = __importDefault(require("moment"));
const utils_1 = require("../../utils");
exports.command = ['list [id]', 'ls', '$0'];
exports.desc = 'List pipelines';
const builder = (args) => {
    return args
        .option('limit', { alias: 'n', type: 'number', describe: 'Limit to n results' })
        .option('offset', { alias: ['o'], type: 'number', describe: 'Start list at offset n' })
        .option('sort', { alias: ['s'], type: 'string', describe: 'Sort by attribute' })
        .positional('id', { describe: 'Pipeline id (or unique prefix)', type: 'string' })
        .conflicts('id', ['limit', 'offset', 'sort']);
};
exports.builder = builder;
function handler({ client, id, format, offset, limit, sort: originalSort }) {
    return __awaiter(this, void 0, void 0, function* () {
        try {
            const sort = originalSort || 'id';
            if (id) {
                const resource = yield (0, utils_1.resolveResource)(id, client.pipelines, 'id', undefined, sort);
                const result = extractVersion(resource);
                if (format === 'table') {
                    (0, utils_1.logObject)(mapHeartbeatToColor(result), 'table');
                }
                else {
                    (0, utils_1.logObject)(result, format);
                }
                return;
            }
            const pipelines = yield client.pipelines.list({ offset, limit, sort });
            const results = pipelines.map(extractVersion);
            if (format === 'table') {
                (0, utils_1.logObject)(results.map(mapHeartbeatToColor), 'table', [
                    'id',
                    'environment',
                    'version',
                    'description',
                    'startedAt',
                    'heartbeatAt'
                ]);
            }
            else {
                (0, utils_1.logObject)(results, format || 'json');
            }
        }
        catch (e) {
            (0, utils_1.logError)(e);
        }
    });
}
exports.handler = handler;
function mapHeartbeatToColor(pipeline) {
    const now = (0, moment_1.default)();
    const running = pipeline.heartbeatAt && now.diff((0, moment_1.default)(pipeline.heartbeatAt), 'seconds') < 30;
    return Object.assign(Object.assign({}, pipeline), { heartbeatAt: { color: running ? '06a453' : 'e11529', value: pipeline.heartbeatAt } });
}
function extractVersion(pipeline) {
    const versionTag = pipeline.metadata.version && pipeline.metadata.version.tag;
    return Object.assign(Object.assign({}, pipeline), { version: versionTag ? versionTag.replace(/^release-/, '') : versionTag });
}
//# sourceMappingURL=list.js.map