"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 });
exports.handler = exports.builder = exports.desc = exports.command = void 0;
const utils_1 = require("../utils");
exports.command = ['appconfig'];
exports.desc = 'Get/set app config';
const builder = (args) => {
    return args
        .option('get', {
        type: 'string',
        describe: 'One or more key/values to set in form <key>=<value>. Supports dotted notation for keys'
    })
        .option('set', {
        type: 'array',
        describe: 'Set one or more key/values (in form <key>=<value>.) Use <key>= to delete a key. Supports dotted notation for keys'
    });
};
exports.builder = builder;
function handler({ client, get, set }) {
    return __awaiter(this, void 0, void 0, function* () {
        try {
            if (set) {
                // Set appconfig
                const keyValues = (0, utils_1.getMetadataKeyValues)(set);
                for (const key of Object.keys(keyValues)) {
                    const value = keyValues[key];
                    if (value === '') {
                        yield client.appconfig.delete(key);
                    }
                    else {
                        yield client.appconfig.set(key, value);
                    }
                }
                (0, utils_1.logDetail)('Done');
            }
            else {
                // Get appconfig
                const appConfig = yield client.appconfig.get(get);
                (0, utils_1.logObject)(appConfig, 'json');
            }
        }
        catch (e) {
            (0, utils_1.logError)(e);
        }
    });
}
exports.handler = handler;
//# sourceMappingURL=appconfig.js.map