/**
 * Query management for Claude SDK
 *
 * Handles tracking and lifecycle management of active Claude queries.
 * Supports concurrent query execution with interrupt capabilities.
 *
 * QueryRegistry is a singleton that encapsulates the global activeQueries Map.
 */
/**
 * Registry for tracking active Claude queries.
 * Singleton — use QueryRegistry.getInstance().
 */
export class QueryRegistry {
    static instance = null;
    activeQueries = new Map();
    constructor() { }
    static getInstance() {
        if (!QueryRegistry.instance) {
            QueryRegistry.instance = new QueryRegistry();
        }
        return QueryRegistry.instance;
    }
    /** Reset singleton for testing */
    static resetInstance() {
        QueryRegistry.instance = null;
    }
    /** Check if there is an active Claude process */
    hasActiveProcess() {
        return this.activeQueries.size > 0;
    }
    /** Check if a specific query is active */
    isQueryActive(queryId) {
        return this.activeQueries.has(queryId);
    }
    /** Get count of active queries */
    getActiveQueryCount() {
        return this.activeQueries.size;
    }
    /** Register an active query */
    registerQuery(queryId, queryInstance) {
        this.activeQueries.set(queryId, queryInstance);
    }
    /** Unregister an active query */
    unregisterQuery(queryId) {
        this.activeQueries.delete(queryId);
    }
    /**
     * Interrupt a specific Claude query by ID.
     * @returns true if the query was interrupted, false if not found
     */
    interruptQuery(queryId) {
        const queryInstance = this.activeQueries.get(queryId);
        if (queryInstance) {
            queryInstance.interrupt();
            this.activeQueries.delete(queryId);
            return true;
        }
        return false;
    }
    /**
     * Interrupt all active Claude queries.
     * Catches EPIPE errors from the SDK writing to a dying child process.
     * @returns number of queries that were interrupted
     */
    interruptAllQueries() {
        const count = this.activeQueries.size;
        for (const [id, queryInstance] of this.activeQueries) {
            // interrupt() is async and writes to child process stdin.
            // If the child process has already exited, the write causes EPIPE.
            // Catch the rejection to prevent unhandled promise rejection.
            void Promise.resolve(queryInstance.interrupt()).catch(() => {
                // Expected: EPIPE when child process is already dead
            });
            this.activeQueries.delete(id);
        }
        return count;
    }
    /**
     * Interrupt the most recently started Claude query.
     * @returns true if a query was interrupted, false if no query was running
     */
    interruptCurrentProcess() {
        if (this.activeQueries.size === 0) {
            return false;
        }
        this.interruptAllQueries();
        return true;
    }
}
/** Generate a unique query ID */
export function generateQueryId() {
    return `q-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;
}
export function hasActiveProcess() {
    return QueryRegistry.getInstance().hasActiveProcess();
}
export function isQueryActive(queryId) {
    return QueryRegistry.getInstance().isQueryActive(queryId);
}
export function getActiveQueryCount() {
    return QueryRegistry.getInstance().getActiveQueryCount();
}
export function registerQuery(queryId, queryInstance) {
    QueryRegistry.getInstance().registerQuery(queryId, queryInstance);
}
export function unregisterQuery(queryId) {
    QueryRegistry.getInstance().unregisterQuery(queryId);
}
export function interruptQuery(queryId) {
    return QueryRegistry.getInstance().interruptQuery(queryId);
}
export function interruptAllQueries() {
    return QueryRegistry.getInstance().interruptAllQueries();
}
export function interruptCurrentProcess() {
    return QueryRegistry.getInstance().interruptCurrentProcess();
}
//# sourceMappingURL=query-manager.js.map