"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const commander_1 = require("commander");
const Config_1 = require("../lib/Config");
const Gateway_1 = require("../remote-services/Gateway");
const cli = new commander_1.Command('data');
cli.command('init')
    .option('-t,--access-token <access-token>', 'Gateway管理员口令牌')
    .action(async ({ accessToken }) => {
    const clientId = Config_1.default.gapper.clientId;
    const clientSecret = Config_1.default.gapper.clientSecret;
    if (!clientId || !clientSecret) {
        console.log('您似乎没有正确的配置clientId和clientSecret!');
        return;
    }
    const gateway = new Gateway_1.default(accessToken);
    if (accessToken) {
        // 传入了管理员口令, 可以尝试进行应用的注册
        const body = JSON.stringify({
            client_secret: clientSecret,
            name: Config_1.default.name,
            short_name: Config_1.default.shortName,
            description: Config_1.default.description,
            icon: Config_1.default.icon,
            url: '',
            active: true,
            type: 'service',
        });
        const app = await gateway.put(`v1/app/${clientId}`, body);
        if (!app) {
            console.error('无法更新应用!');
        }
    }
    else {
        const data = await gateway.post('v1/auth/app-token', {
            client_id: clientId,
            client_secret: clientSecret,
        });
        gateway.setAccessToken(data.access_token);
    }
});
exports.default = cli;
