"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const cli_ux_1 = require("cli-ux");
const core_1 = require("@oclif/core");
const utils = require("@codiac.io/cli-tools");
const command_base_tenantless_1 = require("../../command-base-tenantless");
class ImagesList extends command_base_tenantless_1.CommandBaseTenantless {
    run() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.bootstrap();
            const { args, flags } = yield this.parse(ImagesList);
            if (this._projCfg.images) {
                // this._ui.userMessage(JSON.stringify(this._projCfg.images));
                if (flags.output == "json") {
                    this._ui.userMessage(JSON.stringify(this._projCfg.images, undefined, 2));
                }
                else {
                    // if (flags.output != "csv") {
                    //   this._ui.userMessage(`ImageExports in ${utils.inOrange(this._projCfg.name)}:`);
                    // }
                    cli_ux_1.cli.table(utils.toArray(this._projCfg.images), {
                        "Image": { get: row => this._styler.hilite(row.name) },
                        // "Builds": { get: row => (row.readonly == undefined) ? "true" : !row.readonly },
                        "Definition-File": { get: row => (row.definitionFile) ? `${row.definitionFile} ${row.overwrite}`.trim() : (row.readonly) ? "(built elsewhere)" : (row.overwrite) ? `(gen ${row.overwrite})` : "(in memory auto)" },
                        "Default-Port": { get: row => row.port || "" },
                        "version": { get: row => (row.readonly) ? row.version || "(latest)" : "n/a" },
                        "Registry": { get: row => this.getDefaultRegistryUrl(row), extended: true },
                        "More-Info": { get: row => this.getDefaultRegistryInfoUrl(row) || "", extended: true }
                    }, { extended: (flags.output == "extended"), csv: (flags.output == "csv") });
                }
            }
            else {
                this._ui.userMessage(`No images are configured as an exports in this project.`);
            }
        });
    }
    getDefaultRegistry(image) {
        let target;
        if (image.targets && image.targets.length > 0) {
            let def = image.targets.find(t => t.environment == "default");
            target = def || image.targets[0];
        }
        return target;
    }
    getDefaultRegistryUrl(image) {
        let output;
        let target = this.getDefaultRegistry(image);
        if (target) {
            let prefix = (image.targets.length > 1) ? `(${target.environment}) ` : "";
            output = `${prefix}${target.registryUrl}`;
        }
        return output;
    }
    getDefaultRegistryInfoUrl(image) {
        let output;
        let target = this.getDefaultRegistry(image);
        if (target) {
            output = target.infoUrl;
        }
        return output;
    }
}
exports.default = ImagesList;
ImagesList.description = 'Renders the list of container images that are configured as project exports.';
ImagesList.aliases = ["image:list", "img:list"];
ImagesList.flags = {
    help: core_1.Flags.help({ char: 'h' }),
    output: core_1.Flags.string({ char: 'o', options: ['table', 'extended', 'json', 'csv'], default: 'table', description: "Format for the response data." })
};
ImagesList.args = {};
//# sourceMappingURL=list.js.map