"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const core_1 = require("@oclif/core");
const config_1 = tslib_1.__importDefault(require("@trello-cli/config"));
const cache_1 = tslib_1.__importDefault(require("@trello-cli/cache"));
const path = tslib_1.__importStar(require("path"));
const trello_js_1 = require("trello.js");
const ink_1 = require("ink");
const App_1 = require("../tui/App");
const config_2 = require("../tui/config");
class Interactive extends core_1.Command {
    async run() {
        const { flags } = await this.parse(Interactive);
        const homeDir = process.env[process.platform === "win32" ? "USERPROFILE" : "HOME"];
        const configDir = path.join(homeDir, ".trello-cli");
        const profile = process.env.TRELLO_CLI_PROFILE || "default";
        const trelloConfig = new config_1.default(configDir, profile);
        let token;
        let appKey;
        try {
            token = await trelloConfig.getToken();
            appKey = await trelloConfig.getApiKey();
        }
        catch (e) {
            const message = e.message || e;
            if (e.code === "ERR_NO_APP_KEY") {
                this.warn(`Visit ${e.data.url} to get an API key then run 'trello auth:api-key YOUR_API_KEY'`);
            }
            else if (e.code === "ERR_NO_TOKEN") {
                this.warn(e.message);
                this.logToStderr(`\nVisit ${e.data.url} to generate a token\n\nNext, run 'trello auth:token YOUR_TOKEN'`);
            }
            else {
                this.warn(message);
            }
            this.exit(1);
            return;
        }
        const client = new trello_js_1.TrelloClient({
            key: appKey,
            token: token,
        });
        const cache = new cache_1.default(path.join(configDir, profile), appKey, token);
        // Ensure cache is bootstrapped
        await cache.bootstrap();
        // Load TUI config
        const tuiConfig = (0, config_2.loadTuiConfig)(configDir);
        // Apply mouse flag override
        if (flags.mouse !== undefined) {
            tuiConfig.mouse = flags.mouse;
        }
        const { waitUntilExit } = (0, ink_1.render)((0, jsx_runtime_1.jsx)(App_1.App, { client: client, cache: cache, config: tuiConfig }));
        await waitUntilExit();
    }
}
Interactive.description = "Launch interactive terminal UI for Trello";
Interactive.examples = [
    "$ trello interactive",
    "$ trello interactive --mouse",
    "$ trello interactive --no-mouse",
];
Interactive.flags = {
    mouse: core_1.Flags.boolean({
        description: "Enable mouse support (default: from tui.json config)",
        allowNo: true,
    }),
};
exports.default = Interactive;
