/**
 * Request tracking utility for managing DAP request timeouts
 */
export class RequestTracker {
    pendingRequests = new Map();
    defaultTimeoutMs;
    constructor(defaultTimeoutMs = 30000) {
        this.defaultTimeoutMs = defaultTimeoutMs;
    }
    /**
     * Track a new request with timeout
     */
    track(requestId, command, timeoutMs) {
        // Clear any existing request with same ID
        this.complete(requestId);
        const timeout = timeoutMs ?? this.defaultTimeoutMs;
        const timer = setTimeout(() => {
            const request = this.pendingRequests.get(requestId);
            if (request) {
                this.pendingRequests.delete(requestId);
                // Timeout callback would be handled by the worker
            }
        }, timeout);
        this.pendingRequests.set(requestId, {
            requestId,
            command,
            timer,
            timestamp: Date.now()
        });
    }
    /**
     * Mark a request as completed and clear its timeout
     */
    complete(requestId) {
        const request = this.pendingRequests.get(requestId);
        if (request) {
            clearTimeout(request.timer);
            this.pendingRequests.delete(requestId);
        }
    }
    /**
     * Clear all pending requests
     */
    clear() {
        for (const request of this.pendingRequests.values()) {
            clearTimeout(request.timer);
        }
        this.pendingRequests.clear();
    }
    /**
     * Get all pending requests
     */
    getPending() {
        return new Map(this.pendingRequests);
    }
    /**
     * Get count of pending requests
     */
    getPendingCount() {
        return this.pendingRequests.size;
    }
    /**
     * Check if a specific request is pending
     */
    isPending(requestId) {
        return this.pendingRequests.has(requestId);
    }
    /**
     * Get elapsed time for a request
     */
    getElapsedTime(requestId) {
        const request = this.pendingRequests.get(requestId);
        if (!request) {
            return null;
        }
        return Date.now() - request.timestamp;
    }
}
/**
 * Enhanced request tracker with timeout callbacks
 */
export class CallbackRequestTracker extends RequestTracker {
    onTimeout;
    constructor(onTimeout, defaultTimeoutMs = 30000) {
        super(defaultTimeoutMs);
        this.onTimeout = onTimeout;
    }
    track(requestId, command, timeoutMs) {
        // Clear any existing request with same ID
        this.complete(requestId);
        const timeout = timeoutMs ?? this.defaultTimeoutMs;
        const timer = setTimeout(() => {
            const request = this.pendingRequests.get(requestId);
            if (request) {
                this.pendingRequests.delete(requestId);
                // Call the timeout handler
                this.onTimeout(requestId, command);
            }
        }, timeout);
        this.pendingRequests.set(requestId, {
            requestId,
            command,
            timer,
            timestamp: Date.now()
        });
    }
}
//# sourceMappingURL=dap-proxy-request-tracker.js.map