"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const command_1 = require("@oclif/command");
const command_2 = __importDefault(require("../extends/command"));
const account_1 = __importDefault(require("../implements/account"));
const update_1 = require("../utils/update");
const env_1 = require("../utils/env");
class Login extends command_2.default {
    async run(params) {
        const { flags } = await this.parse(Login);
        const account = new account_1.default();
        account.eventTracker('login_start', { tags: Object.assign({}, flags) });
        account_1.default.checkTokenExist(this.id);
        const isLogin = account.isLogin(flags.environment);
        if (isLogin) {
            console.log('Already login, current user is: ' + account.getUserInfo());
            console.log('If you want to login with the other account, you can run ' +
                this.success(`opdev logout`) +
                '.');
            if (env_1.environment.length > 1) {
                console.log('If you want to switch to the other env, you can run ' +
                    this.success(`opdev switch -e [env]`) +
                    '.');
            }
        }
        else {
            await account.login({
                environment: flags.environment,
            });
            if (params === null || params === void 0 ? void 0 : params.updateConfig) {
                const loading = this.createLoading({});
                loading.start();
                await (0, update_1.writeUpdateData)(undefined, false);
                await (0, update_1.writeFGData)();
                loading.stop();
            }
            console.info(this.success('Success') + ' logged in');
        }
    }
}
exports.default = Login;
Login.description = 'Login with feishu account';
Login.examples = [
    ['$ {{cli}} login -e feishu', 'login with feishu account'],
];
Login.flags = {
    environment: command_1.flags.enum({
        char: 'e',
        description: 'environment',
        options: env_1.environment,
    }),
    help: command_1.flags.help({
        char: 'h',
    }),
};
