/**
 * LspErrorHandler - Fail-fast error handling with grep fallback
 *
 * @see spec.md US-007: Error Handling
 * @satisfies AC-US7-01, AC-US7-02
 */
/**
 * Fail-fast error handler for LSP operations
 */
export class LspErrorHandler {
    constructor(grepFallback) {
        this.grepFallback = grepFallback;
    }
    /**
     * Handle an LSP error with fail-fast behavior
     *
     * @param error - The error that occurred
     * @param options - Handling options
     * @returns Promise resolving to handler result
     */
    async handle(error, options = {}) {
        const errorType = this.classifyError(error);
        const errorMessage = this.formatError(error, errorType);
        const suggestion = this.getSuggestion(errorType);
        let usedFallback = false;
        let fallbackResults;
        // Use grep fallback if enabled and crash/timeout
        if (options.enableFallback &&
            this.grepFallback &&
            options.searchPattern &&
            options.searchPath &&
            (errorType === 'crash' || errorType === 'timeout')) {
            usedFallback = true;
            fallbackResults = await this.grepFallback.search(options.searchPattern, options.searchPath);
        }
        return {
            retried: false, // Fail fast - never retry
            errorMessage,
            errorType,
            suggestion,
            usedFallback,
            fallbackResults,
        };
    }
    classifyError(error) {
        const message = error.message.toLowerCase();
        if (message.includes('timeout') ||
            message.includes('etimedout') ||
            message.includes('timed out')) {
            return 'timeout';
        }
        if (message.includes('crash') ||
            message.includes('exited') ||
            message.includes('process')) {
            return 'crash';
        }
        if (message.includes('econnrefused') ||
            message.includes('connection') ||
            message.includes('refused')) {
            return 'connection';
        }
        return 'unknown';
    }
    formatError(error, type) {
        switch (type) {
            case 'timeout':
                return `LSP operation timed out: ${error.message}`;
            case 'crash':
                return `LSP server crashed: ${error.message}`;
            case 'connection':
                return `Failed to connect to LSP server: ${error.message}`;
            default:
                return `LSP error: ${error.message}`;
        }
    }
    getSuggestion(type) {
        switch (type) {
            case 'timeout':
                return 'Try increasing the timeout in .specweave/config.json (lsp.timeout)';
            case 'crash':
                return 'Run `specweave lsp doctor` to diagnose server issues';
            case 'connection':
                return 'Ensure the LSP server is installed and in your PATH';
            default:
                return undefined;
        }
    }
}
//# sourceMappingURL=error-handler.js.map