/**
 * Streaming Error Classes
 * Defines error types for SSE streaming with retryability flags
 */
/**
 * Network error - typically connection issues or fetch failures
 * Retryable: Yes
 */
class StreamingNetworkError extends Error {
    constructor(message, details) {
        super(message);
        this.details = details;
        this.type = 'network';
        this.retryable = true;
        this.name = 'StreamingNetworkError';
    }
}
/**
 * Timeout error - request exceeded specified timeout duration
 * Retryable: Yes
 */
class StreamingTimeoutError extends Error {
    constructor(timeout) {
        super(`Request timeout after ${timeout}ms`);
        this.type = 'timeout';
        this.retryable = true;
        this.name = 'StreamingTimeoutError';
    }
}
/**
 * Parse error - invalid JSON or malformed SSE data
 * Retryable: No (indicates server-side issue or format mismatch)
 */
class StreamingParseError extends Error {
    constructor(message, chunk) {
        super(message);
        this.chunk = chunk;
        this.type = 'parse';
        this.retryable = false;
        this.name = 'StreamingParseError';
    }
}
/**
 * Abort error - user manually cancelled the stream
 * Retryable: No (user initiated)
 */
class StreamingAbortError extends Error {
    constructor() {
        super('Stream aborted by user');
        this.type = 'abort';
        this.retryable = false;
        this.name = 'StreamingAbortError';
    }
}
/**
 * Server error - HTTP error response from server (4xx, 5xx)
 * Retryable: No for 4xx, could be Yes for 5xx but defaulting to No
 */
class StreamingServerError extends Error {
    constructor(status, statusText, details) {
        super(`Server error: ${status} ${statusText}`);
        this.status = status;
        this.details = details;
        this.type = 'server';
        this.retryable = false;
        this.name = 'StreamingServerError';
    }
}
/**
 * Normalize any error into a StreamingError
 * Used to convert unknown errors (from fetch, network, etc.) into typed streaming errors
 *
 * @param error - Any error object
 * @returns Normalized StreamingError
 */
const normalizeError = (error) => {
    // Handle AbortController abortion
    if (error instanceof DOMException && error.name === 'AbortError') {
        return new StreamingAbortError();
    }
    // Already a streaming error - return as-is
    if (error instanceof StreamingNetworkError ||
        error instanceof StreamingTimeoutError ||
        error instanceof StreamingParseError ||
        error instanceof StreamingServerError ||
        error instanceof StreamingAbortError) {
        return error;
    }
    // Unknown error - treat as network error (retryable)
    return new StreamingNetworkError(error instanceof Error ? error.message : 'Unknown streaming error', error);
};

export { StreamingAbortError, StreamingNetworkError, StreamingParseError, StreamingServerError, StreamingTimeoutError, normalizeError };
