"use strict";
/**
 * Component Status Error Types
 *
 * This module defines specific error types for the component status system,
 * providing better diagnostics and error handling capabilities.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.CrossThreadCollectionError = exports.ComponentStatusOperationError = exports.AggregationError = exports.ITCError = exports.CrossThreadTimeoutError = exports.ComponentStatusError = void 0;
const commonErrors_js_1 = require("../../utility/errors/commonErrors.js");
/**
 * Base error class for component status system
 */
class ComponentStatusError extends Error {
    statusCode;
    timestamp;
    constructor(message, statusCode = commonErrors_js_1.HTTP_STATUS_CODES.INTERNAL_SERVER_ERROR) {
        super(message);
        this.name = 'ComponentStatusError';
        this.statusCode = statusCode;
        this.timestamp = new Date();
        Error.captureStackTrace(this, this.constructor);
    }
}
exports.ComponentStatusError = ComponentStatusError;
/**
 * Error thrown when cross-thread status collection times out
 */
class CrossThreadTimeoutError extends ComponentStatusError {
    requestId;
    timeoutMs;
    collectedCount;
    constructor(requestId, timeoutMs, collectedCount) {
        super(`Component status collection timeout after ${timeoutMs}ms. ` +
            `Collected ${collectedCount} responses for request ${requestId}.`, commonErrors_js_1.HTTP_STATUS_CODES.GATEWAY_TIMEOUT);
        this.name = 'CrossThreadTimeoutError';
        this.requestId = requestId;
        this.timeoutMs = timeoutMs;
        this.collectedCount = collectedCount;
    }
}
exports.CrossThreadTimeoutError = CrossThreadTimeoutError;
/**
 * Error thrown when ITC (Inter-Thread Communication) fails
 */
class ITCError extends ComponentStatusError {
    operation;
    cause;
    constructor(operation, cause) {
        super(`Inter-thread communication failed during ${operation}: ${cause?.message || 'Unknown error'}`, commonErrors_js_1.HTTP_STATUS_CODES.SERVICE_UNAVAILABLE);
        this.name = 'ITCError';
        this.operation = operation;
        this.cause = cause;
    }
}
exports.ITCError = ITCError;
/**
 * Error thrown when component status aggregation fails
 */
class AggregationError extends ComponentStatusError {
    componentCount;
    cause;
    constructor(componentCount, cause) {
        super(`Failed to aggregate status for ${componentCount} components: ${cause?.message || 'Unknown error'}`, commonErrors_js_1.HTTP_STATUS_CODES.INTERNAL_SERVER_ERROR);
        this.name = 'AggregationError';
        this.componentCount = componentCount;
        this.cause = cause;
    }
}
exports.AggregationError = AggregationError;
/**
 * Error thrown when a component status operation fails
 */
class ComponentStatusOperationError extends ComponentStatusError {
    componentName;
    operation;
    constructor(componentName, operation, message) {
        super(`Component '${componentName}' ${operation} failed: ${message}`, commonErrors_js_1.HTTP_STATUS_CODES.INTERNAL_SERVER_ERROR);
        this.name = 'ComponentStatusOperationError';
        this.componentName = componentName;
        this.operation = operation;
    }
}
exports.ComponentStatusOperationError = ComponentStatusOperationError;
/**
 * Error thrown with detailed cross-thread collection diagnostics
 */
class CrossThreadCollectionError extends ComponentStatusError {
    result;
    constructor(result) {
        const message = result.success
            ? `Partial collection success: ${result.collectedFromThreads} threads responded` +
                (result.timedOutThreads.length > 0 ? `, ${result.timedOutThreads.length} timed out` : '')
            : `Collection failed: ${result.errors.map((e) => e.message).join(', ')}`;
        super(message, commonErrors_js_1.HTTP_STATUS_CODES.OK); // 200 - partial success is still success
        this.name = 'CrossThreadCollectionError';
        this.result = result;
    }
    /**
     * Get detailed diagnostic information
     */
    getDiagnostics() {
        const lines = [
            `Cross-thread collection ${this.result.success ? 'partially succeeded' : 'failed'}`,
            `Threads responded: ${this.result.collectedFromThreads}`,
        ];
        if (this.result.expectedThreads) {
            lines.push(`Expected threads: ${this.result.expectedThreads}`);
        }
        if (this.result.timedOutThreads.length > 0) {
            lines.push(`Timed out threads: ${this.result.timedOutThreads.join(', ')}`);
        }
        if (this.result.errors.length > 0) {
            lines.push('Errors:');
            this.result.errors.forEach((error, index) => {
                lines.push(`  ${index + 1}. ${error.name}: ${error.message}`);
            });
        }
        return lines.join('\n');
    }
}
exports.CrossThreadCollectionError = CrossThreadCollectionError;
//# sourceMappingURL=errors.js.map