"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ErrorManager = exports.ErrorTypes = void 0;
const ui_1 = require("./ui");
const error_codes_1 = require("./error-codes");
const platform_commands_1 = require("./platform-commands");
/**
 * Error types with structured messages (Legacy - kept for compatibility)
 */
exports.ErrorTypes = {
    NO_CLAUDE_CLI: 'NO_CLAUDE_CLI',
    MISSING_SETTINGS: 'MISSING_SETTINGS',
    INVALID_CONFIG: 'INVALID_CONFIG',
    UNKNOWN_PROFILE: 'UNKNOWN_PROFILE',
    PERMISSION_DENIED: 'PERMISSION_DENIED',
    GENERIC: 'GENERIC',
};
/**
 * Enhanced error manager with context-aware messages
 */
class ErrorManager {
    /**
     * Show error code and documentation URL
     */
    static showErrorCode(errorCode) {
        console.error((0, ui_1.dim)(`Error: ${errorCode}`));
        console.error((0, ui_1.dim)((0, error_codes_1.getErrorDocUrl)(errorCode)));
        console.error('');
    }
    /**
     * Show Claude CLI not found error
     */
    static async showClaudeNotFound() {
        await (0, ui_1.initUI)();
        console.error('');
        console.error((0, ui_1.errorBox)('Claude CLI not found\n\n' +
            'CCS requires Claude CLI to be installed\n' +
            'and available in PATH.', 'ERROR'));
        console.error('');
        console.error((0, ui_1.header)('SOLUTIONS'));
        console.error('');
        console.error('  1. Install Claude CLI');
        console.error(`     ${(0, ui_1.color)('https://docs.claude.com/install', 'path')}`);
        console.error('');
        // Windows-specific guidance for native installer users
        if (process.platform === 'win32') {
            console.error('  2. If you used the Windows installer, run:');
            console.error(`     ${(0, ui_1.color)('claude install', 'command')}`);
            console.error((0, ui_1.dim)('     This adds Claude to your PATH'));
            console.error('');
            console.error('  3. Verify installation');
            console.error(`     ${(0, ui_1.color)('Get-Command claude', 'command')}`);
            console.error('');
            console.error('  4. Custom path (if installed elsewhere)');
            console.error(`     ${(0, ui_1.color)('$env:CCS_CLAUDE_PATH="C:\\path\\to\\claude.exe"', 'command')}`);
        }
        else {
            console.error('  2. Verify installation');
            console.error(`     ${(0, ui_1.color)('command -v claude', 'command')}`);
            console.error('');
            console.error('  3. Custom path (if installed elsewhere)');
            console.error(`     ${(0, ui_1.color)('export CCS_CLAUDE_PATH="/path/to/claude"', 'command')}`);
        }
        console.error('');
        this.showErrorCode(error_codes_1.ERROR_CODES.CLAUDE_NOT_FOUND);
    }
    /**
     * Show settings file not found error
     */
    static async showSettingsNotFound(settingsPath) {
        await (0, ui_1.initUI)();
        const isClaudeSettings = settingsPath.includes('.claude') && settingsPath.endsWith('settings.json');
        console.error('');
        console.error((0, ui_1.errorBox)('Settings file not found\n\n' + `File: ${settingsPath}`, 'ERROR'));
        console.error('');
        if (isClaudeSettings) {
            console.error('This file is auto-created when you login to Claude CLI.');
            console.error('');
            console.error((0, ui_1.header)('SOLUTIONS'));
            console.error(`  ${(0, ui_1.color)(`echo '{}' > ${settingsPath}`, 'command')}`);
            console.error(`  ${(0, ui_1.color)('claude /login', 'command')}`);
            console.error('');
            console.error((0, ui_1.dim)('Why: Newer Claude CLI versions require explicit login.'));
        }
        else {
            console.error((0, ui_1.header)('SOLUTIONS'));
            console.error(`  ${(0, ui_1.color)('npm install -g @kaitranntt/ccs --force', 'command')}`);
            console.error('');
            console.error((0, ui_1.dim)('This will recreate missing profile settings.'));
        }
        console.error('');
        this.showErrorCode(error_codes_1.ERROR_CODES.CONFIG_INVALID_PROFILE);
    }
    /**
     * Show invalid configuration error
     */
    static async showInvalidConfig(configPath, errorDetail) {
        await (0, ui_1.initUI)();
        console.error('');
        console.error((0, ui_1.errorBox)('Configuration invalid\n\n' + `File: ${configPath}\n` + `Issue: ${errorDetail}`, 'ERROR'));
        console.error('');
        console.error((0, ui_1.header)('SOLUTIONS'));
        console.error('');
        console.error(`  ${(0, ui_1.dim)('# Backup corrupted file')}`);
        console.error(`  ${(0, ui_1.color)(`mv ${configPath} ${configPath}.backup`, 'command')}`);
        console.error('');
        console.error(`  ${(0, ui_1.dim)('# Reinstall CCS')}`);
        console.error(`  ${(0, ui_1.color)('npm install -g @kaitranntt/ccs --force', 'command')}`);
        console.error('');
        console.error((0, ui_1.dim)('Your profile settings will be preserved.'));
        console.error('');
        this.showErrorCode(error_codes_1.ERROR_CODES.CONFIG_INVALID_JSON);
    }
    /**
     * Show profile not found error
     */
    static async showProfileNotFound(profileName, availableProfiles, suggestions = []) {
        await (0, ui_1.initUI)();
        console.error('');
        console.error((0, ui_1.errorBox)(`Profile '${profileName}' not found`, 'ERROR'));
        console.error('');
        if (suggestions && suggestions.length > 0) {
            console.error((0, ui_1.header)('DID YOU MEAN'));
            suggestions.forEach((s) => console.error(`  ${(0, ui_1.color)(s, 'command')}`));
            console.error('');
        }
        console.error((0, ui_1.header)('AVAILABLE PROFILES'));
        availableProfiles.forEach((line) => console.error(`  ${(0, ui_1.color)(line, 'info')}`));
        console.error('');
        console.error((0, ui_1.header)('SOLUTIONS'));
        console.error('');
        console.error('  Use an existing profile:');
        console.error(`    ${(0, ui_1.color)('ccs <profile> "your prompt"', 'command')}`);
        console.error('');
        console.error('  Create a new account profile:');
        console.error(`    ${(0, ui_1.color)('ccs auth create <name>', 'command')}`);
        console.error('');
        console.error((0, ui_1.info)(`Tip: Use ${(0, ui_1.color)('ccs config', 'command')} for web-based configuration`));
        console.error('');
        this.showErrorCode(error_codes_1.ERROR_CODES.PROFILE_NOT_FOUND);
    }
    /**
     * Show permission denied error
     */
    static async showPermissionDenied(filePath) {
        await (0, ui_1.initUI)();
        console.error('');
        console.error((0, ui_1.errorBox)('Permission denied\n\n' + `Cannot write to: ${filePath}`, 'ERROR'));
        console.error('');
        console.error((0, ui_1.header)('SOLUTIONS'));
        console.error('');
        console.error(`  ${(0, ui_1.dim)('# Fix ownership')}`);
        console.error(`  ${(0, ui_1.color)('sudo chown -R $USER ~/.ccs ~/.claude', 'command')}`);
        console.error('');
        console.error(`  ${(0, ui_1.dim)('# Fix permissions')}`);
        console.error(`  ${(0, ui_1.color)('chmod 755 ~/.ccs ~/.claude', 'command')}`);
        console.error('');
        console.error(`  ${(0, ui_1.dim)('# Retry installation')}`);
        console.error(`  ${(0, ui_1.color)('npm install -g @kaitranntt/ccs --force', 'command')}`);
        console.error('');
        this.showErrorCode(error_codes_1.ERROR_CODES.FS_CANNOT_WRITE_FILE);
    }
    /**
     * Show CLIProxy OAuth timeout error
     */
    static async showOAuthTimeout(provider) {
        await (0, ui_1.initUI)();
        console.error('');
        console.error((0, ui_1.errorBox)('OAuth Timeout\n\n' + 'Authentication did not complete within 2 minutes.', 'ERROR'));
        console.error('');
        console.error((0, ui_1.header)('TROUBLESHOOTING'));
        console.error('  1. Check if browser opened (popup blocker?)');
        console.error('  2. Complete login in browser, then return here');
        console.error('  3. Try different browser');
        console.error('  4. Disable browser extensions temporarily');
        console.error('');
        console.error((0, ui_1.header)('FOR HEADLESS/SSH ENVIRONMENTS'));
        console.error(`  ${(0, ui_1.color)(`ccs ${provider} --auth --headless`, 'command')}`);
        console.error('');
        console.error((0, ui_1.dim)('This displays manual authentication steps.'));
        console.error('');
        console.error((0, ui_1.info)(`Tip: Use ${(0, ui_1.color)('ccs config', 'command')} for web-based configuration`));
        console.error('');
    }
    /**
     * Show CLIProxy port conflict error
     */
    static async showPortConflict(port) {
        await (0, ui_1.initUI)();
        const isWindows = process.platform === 'win32';
        console.error('');
        console.error((0, ui_1.errorBox)('Port Conflict\n\n' + `CLIProxy port ${port} is already in use.`, 'ERROR'));
        console.error('');
        console.error((0, ui_1.header)('SOLUTIONS'));
        console.error('');
        console.error('  1. Find process using port:');
        console.error(`     ${(0, ui_1.color)((0, platform_commands_1.getPortCheckCommand)(port), 'command')}`);
        console.error('');
        console.error('  2. Kill the process:');
        if (isWindows) {
            console.error(`     ${(0, ui_1.color)(`taskkill /F /PID <PID>`, 'command')}  (replace <PID> with actual ID)`);
        }
        else {
            console.error(`     ${(0, ui_1.color)((0, platform_commands_1.getKillPidCommand)(12345).replace('12345', '<PID>'), 'command')}`);
        }
        console.error('');
        console.error('  3. Auto-fix: Run:');
        console.error(`     ${(0, ui_1.color)('ccs doctor --fix', 'command')}`);
        console.error('');
    }
    /**
     * Show CLIProxy binary download failure error
     */
    static async showBinaryDownloadFailed(url, error) {
        await (0, ui_1.initUI)();
        console.error('');
        console.error((0, ui_1.errorBox)('Binary Download Failed\n\n' + `Error: ${error}`, 'ERROR'));
        console.error('');
        console.error((0, ui_1.header)('TROUBLESHOOTING'));
        console.error('  1. Check internet connection');
        console.error('  2. Check firewall/proxy settings');
        console.error('  3. Try again in a few minutes');
        console.error('');
        console.error((0, ui_1.header)('MANUAL DOWNLOAD'));
        console.error(`  URL: ${(0, ui_1.color)(url, 'path')}`);
        console.error(`  Save to: ${(0, ui_1.color)('~/.ccs/bin/cliproxyapi', 'path')}`);
        console.error(`  ${(0, ui_1.color)('chmod +x ~/.ccs/bin/cliproxyapi', 'command')}`);
        console.error('');
    }
    /**
     * Show CLIProxy authentication required error
     */
    static async showAuthRequired(provider) {
        await (0, ui_1.initUI)();
        const displayName = provider.charAt(0).toUpperCase() + provider.slice(1);
        console.error('');
        console.error((0, ui_1.errorBox)(`${displayName} authentication required`, 'ERROR'));
        console.error('');
        console.error((0, ui_1.header)('TO AUTHENTICATE'));
        console.error(`  ${(0, ui_1.color)(`ccs ${provider} --auth`, 'command')}`);
        console.error('');
        console.error((0, ui_1.dim)('This will open a browser for OAuth login.'));
        console.error((0, ui_1.dim)('After login, you can use the profile normally.'));
        console.error('');
        console.error((0, ui_1.info)(`Tip: Use ${(0, ui_1.color)('ccs config', 'command')} for web-based configuration`));
        console.error('');
    }
}
exports.ErrorManager = ErrorManager;
//# sourceMappingURL=error-manager.js.map