"use strict";
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 });
exports.ConfigSetCommand = exports.ConfigGetCommand = exports.ConfigListCommand = void 0;
const yok_1 = require("../common/yok");
const color_1 = require("../color");
class ConfigListCommand {
    constructor($projectConfigService, $logger) {
        this.$projectConfigService = $projectConfigService;
        this.$logger = $logger;
        this.allowedParameters = [];
    }
    execute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const config = this.$projectConfigService.readConfig();
                this.$logger.info(this.getValueString(config));
            }
            catch (error) {
                this.$logger.info("Failed to read config. Error is: ", error);
            }
        });
    }
    getValueString(value, depth = 0) {
        const indent = () => "  ".repeat(depth);
        if (typeof value === "object") {
            return (`${depth > 0 ? "\n" : ""}` +
                Object.keys(value)
                    .map((key) => {
                    return (color_1.color.green(`${indent()}${key}: `) +
                        this.getValueString(value[key], depth + 1));
                })
                    .join("\n"));
        }
        else {
            return color_1.color.yellow(value.toString());
        }
    }
}
exports.ConfigListCommand = ConfigListCommand;
class ConfigGetCommand {
    constructor($projectConfigService, $logger, $errors) {
        this.$projectConfigService = $projectConfigService;
        this.$logger = $logger;
        this.$errors = $errors;
        this.allowedParameters = [];
    }
    execute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const [key] = args;
                const current = this.$projectConfigService.getValue(key);
                this.$logger.info(current);
            }
            catch (err) {
            }
        });
    }
    canExecute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!args[0]) {
                this.$errors.failWithHelp("You must specify a key. Eg: ios.id");
            }
            return true;
        });
    }
}
exports.ConfigGetCommand = ConfigGetCommand;
class ConfigSetCommand {
    constructor($projectConfigService, $logger, $errors) {
        this.$projectConfigService = $projectConfigService;
        this.$logger = $logger;
        this.$errors = $errors;
        this.allowedParameters = [];
    }
    execute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            const [key, value] = args;
            const current = this.$projectConfigService.getValue(key);
            if (current && typeof current === "object") {
                this.$errors.fail(`Unable to change object values. Please update individual values instead.\nEg: ns config set android.codeCache true`);
            }
            const convertedValue = this.getConvertedValue(value);
            const existingKey = current !== undefined;
            const keyDisplay = color_1.color.green(key);
            const currentDisplay = color_1.color.yellow(current);
            const updatedDisplay = color_1.color.cyan(convertedValue);
            this.$logger.info(`${existingKey ? "Updating" : "Setting"} ${keyDisplay}${existingKey ? ` from ${currentDisplay} ` : " "}to ${updatedDisplay}`);
            try {
                yield this.$projectConfigService.setValue(key, convertedValue);
                this.$logger.info("Done");
            }
            catch (error) {
                this.$logger.info("Could not update conifg. Error is: ", error);
            }
        });
    }
    canExecute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!args[0]) {
                this.$errors.failWithHelp("You must specify a key. Eg: ios.id");
            }
            if (!args[1]) {
                this.$errors.failWithHelp("You must specify a value.");
            }
            return true;
        });
    }
    getConvertedValue(v) {
        try {
            return JSON.parse(v);
        }
        catch (e) {
            return `${v}`;
        }
    }
}
exports.ConfigSetCommand = ConfigSetCommand;
yok_1.injector.registerCommand("config|*list", ConfigListCommand);
yok_1.injector.registerCommand("config|get", ConfigGetCommand);
yok_1.injector.registerCommand("config|set", ConfigSetCommand);
