"use strict";
/**
 * Kiro Import Helper
 *
 * Imports Kiro token from Kiro IDE when OAuth callback redirects to IDE instead of CLI.
 * Spawns cli-proxy-api-plus --kiro-import to import token from Kiro IDE's storage.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.importKiroToken = exports.tryKiroImport = void 0;
const child_process_1 = require("child_process");
const ui_1 = require("../../utils/ui");
const binary_manager_1 = require("../binary-manager");
const config_generator_1 = require("../config/config-generator");
const token_manager_1 = require("./token-manager");
/**
 * Try to import Kiro token from Kiro IDE
 * Uses cli-proxy-api-plus --kiro-import flag
 */
async function tryKiroImport(tokenDir, verbose = false) {
    const log = (msg) => {
        if (verbose)
            console.error(`[kiro-import] ${msg}`);
    };
    try {
        log('Ensuring CLIProxy binary is available...');
        const binaryPath = await (0, binary_manager_1.ensureCLIProxyBinary)(verbose, { skipAutoUpdate: true });
        const configPath = (0, config_generator_1.generateConfig)('kiro');
        log(`Binary: ${binaryPath}`);
        log(`Config: ${configPath}`);
        log(`Token dir: ${tokenDir}`);
        return new Promise((resolve) => {
            const args = ['--config', configPath, '--kiro-import'];
            log(`Running: ${binaryPath} ${args.join(' ')}`);
            const proc = (0, child_process_1.spawn)(binaryPath, args, {
                stdio: ['ignore', 'pipe', 'pipe'],
                env: { ...process.env, CLI_PROXY_AUTH_DIR: tokenDir },
            });
            let stdout = '';
            let stderr = '';
            let resolved = false;
            const safeResolve = (result) => {
                if (resolved)
                    return;
                resolved = true;
                clearTimeout(timeoutId);
                resolve(result);
            };
            proc.stdout?.on('data', (data) => {
                const output = data.toString();
                stdout += output;
                log(`stdout: ${output.trim()}`);
            });
            proc.stderr?.on('data', (data) => {
                const output = data.toString();
                stderr += output;
                log(`stderr: ${output.trim()}`);
            });
            proc.on('exit', (code) => {
                log(`Exit code: ${code}`);
                if (code === 0) {
                    // Parse output for provider info
                    const providerMatch = stdout.match(/Provider:\s*(\w+)/i);
                    const emailMatch = stdout.match(/email[:\s]+([^\s,)]+)/i);
                    const successMatch = stdout.includes('Kiro token import successful') ||
                        stdout.includes('Imported Kiro token') ||
                        stdout.includes('Authentication saved');
                    if (successMatch) {
                        safeResolve({
                            success: true,
                            provider: providerMatch?.[1],
                            email: emailMatch?.[1],
                        });
                    }
                    else {
                        safeResolve({
                            success: false,
                            error: 'Import completed but token not confirmed',
                        });
                    }
                }
                else {
                    const errorLine = stderr.trim().split('\n')[0] || stdout.trim().split('\n')[0];
                    safeResolve({
                        success: false,
                        error: errorLine || `Exit code ${code}`,
                    });
                }
            });
            proc.on('error', (error) => {
                log(`Process error: ${error.message}`);
                safeResolve({
                    success: false,
                    error: error.message,
                });
            });
            // Timeout after 30 seconds
            const timeoutId = setTimeout(() => {
                if (!resolved && !proc.killed) {
                    proc.kill();
                    safeResolve({
                        success: false,
                        error: 'Import timed out after 30 seconds',
                    });
                }
            }, 30000);
        });
    }
    catch (error) {
        log(`Error: ${error.message}`);
        return {
            success: false,
            error: error.message,
        };
    }
}
exports.tryKiroImport = tryKiroImport;
/**
 * Import Kiro token with user-facing output
 * Shows progress and result to user
 */
async function importKiroToken(verbose = false) {
    const tokenDir = (0, token_manager_1.getProviderTokenDir)('kiro');
    console.log('');
    console.log((0, ui_1.info)('Importing token from Kiro IDE...'));
    const result = await tryKiroImport(tokenDir, verbose);
    if (result.success) {
        const providerInfo = result.provider ? ` (Provider: ${result.provider})` : '';
        console.log((0, ui_1.ok)(`Imported Kiro token from IDE${providerInfo}`));
        return true;
    }
    console.log((0, ui_1.fail)(`Import failed: ${result.error}`));
    console.log('');
    console.log('Make sure you are logged into Kiro IDE first:');
    console.log('  1. Open Kiro IDE');
    console.log('  2. Sign in with your AWS/Google account');
    console.log('  3. Run: ccs kiro --import');
    return false;
}
exports.importKiroToken = importKiroToken;
//# sourceMappingURL=kiro-import.js.map