/**
 * Factory SSE Service
 *
 * Manages Server-Sent Events connections for real-time factory state updates.
 * Broadcasts combined data from Crewly teams AND Claude Code processes.
 *
 * Instead of each client polling, this service:
 * - Maintains a single internal poll loop (3s interval)
 * - Broadcasts updates only when data changes (hash-based diff)
 * - Sends heartbeats every 30s to keep connections alive
 *
 * @module services/factory/factory-sse
 */
import { Response } from 'express';
import { FactoryService, FactoryStateResponse } from '../factory.service.js';
/**
 * SSE event types sent to clients
 */
export type SSEEventType = 'connected' | 'instances' | 'heartbeat' | 'error';
/**
 * SSE event payload structure
 */
export interface SSEEvent<T = unknown> {
    /** Event type */
    type: SSEEventType;
    /** Event payload */
    data: T;
    /** Timestamp of the event */
    timestamp: string;
}
/**
 * Factory SSE Service configuration
 */
export interface FactorySSEConfig {
    /** Polling interval in milliseconds (default: 3000) */
    pollInterval?: number;
    /** Heartbeat interval in milliseconds (default: 30000) */
    heartbeatInterval?: number;
}
/**
 * Factory SSE Service for managing real-time client connections
 */
export declare class FactorySSEService {
    /** Connected SSE clients */
    private clients;
    /** Factory service for fetching combined factory state */
    private factoryService;
    /** Current data hash for change detection */
    private lastDataHash;
    /** Last cached response data */
    private lastData;
    /** Poll interval timer */
    private pollTimer;
    /** Heartbeat interval timer */
    private heartbeatTimer;
    /** Polling interval in milliseconds */
    private pollInterval;
    /** Heartbeat interval in milliseconds */
    private heartbeatInterval;
    /** Whether the service is currently polling */
    private isPolling;
    /**
     * Creates a new FactorySSEService instance
     *
     * @param factoryService - Factory service for data fetching (injectable for testing)
     * @param config - Optional configuration overrides
     */
    constructor(factoryService?: FactoryService, config?: FactorySSEConfig);
    /**
     * Adds a new SSE client connection
     *
     * @param clientId - Unique identifier for the client
     * @param res - Express Response object configured for SSE
     */
    addClient(clientId: string, res: Response): void;
    /**
     * Removes a client connection
     *
     * @param clientId - Client identifier to remove
     */
    removeClient(clientId: string): void;
    /**
     * Gets the current number of connected clients
     *
     * @returns Number of connected clients
     */
    getClientCount(): number;
    /**
     * Checks if a specific client is connected
     *
     * @param clientId - Client identifier to check
     * @returns True if client is connected
     */
    hasClient(clientId: string): boolean;
    /**
     * Starts the internal polling loop
     */
    private startPolling;
    /**
     * Stops the internal polling loop
     */
    private stopPolling;
    /**
     * Performs a single poll and broadcasts if data changed
     */
    private poll;
    /**
     * Computes a hash of the data for change detection
     * Only includes significant fields to avoid false positives
     *
     * @param data - Factory state response (teams + Claude instances)
     * @returns Hash string for comparison
     */
    private computeDataHash;
    /**
     * Broadcasts factory state data to all connected clients
     *
     * @param data - Factory state response (teams + Claude instances)
     */
    private broadcastInstances;
    /**
     * Broadcasts a heartbeat to all connected clients
     */
    private broadcastHeartbeat;
    /**
     * Broadcasts an error to all connected clients
     *
     * @param message - Error message
     * @param code - Error code
     */
    private broadcastError;
    /**
     * Sends an SSE event to a specific client
     *
     * @param client - Client to send to
     * @param type - Event type
     * @param data - Event data payload
     */
    private sendEvent;
    /**
     * Force triggers a poll and broadcast (for testing)
     */
    forcePoll(): Promise<void>;
    /**
     * Cleans up all resources (for graceful shutdown)
     */
    shutdown(): void;
    /**
     * Returns whether the service is currently polling
     *
     * @returns True if polling is active
     */
    isActive(): boolean;
    /**
     * Gets the last cached data (for fallback/debugging)
     *
     * @returns Last cached factory state response or null
     */
    getLastData(): FactoryStateResponse | null;
}
/** Singleton instance for use across the application */
export declare const factorySSEService: FactorySSEService;
//# sourceMappingURL=factory-sse.service.d.ts.map