/**
 * TaskRegistry — provides O(1) task lookup by ID and O(k) lookup by kind,
 * running status, and parent task.
 *
 * All mutation methods return `this` for chaining.
 */
export class TaskRegistry {
    /** Primary task index keyed by task ID. */
    _tasks = new Map();
    /**
     * Registers a task in the registry. If a task with the same ID already
     * exists it is replaced (treated as an update).
     *
     * @param task - The RuntimeTask to register.
     */
    register(task) {
        this._tasks.set(task.id, task);
        return this;
    }
    /**
     * Removes a task from the registry by ID.
     *
     * @param id - The task ID to remove.
     * @returns `true` if the task existed and was removed; `false` otherwise.
     */
    deregister(id) {
        return this._tasks.delete(id);
    }
    /**
     * Retrieves a task by its unique ID.
     *
     * @param id - The task ID to look up.
     * @returns The RuntimeTask if found, or `undefined`.
     */
    get(id) {
        return this._tasks.get(id);
    }
    /**
     * Returns all tasks of a given kind.
     *
     * @param kind - The TaskKind to filter by.
     * @returns Array of matching RuntimeTask records (may be empty).
     */
    getByKind(kind) {
        const results = [];
        for (const task of this._tasks.values()) {
            if (task.kind === kind) {
                results.push(task);
            }
        }
        return results;
    }
    /**
     * Returns all currently running tasks (status === 'running').
     *
     * @returns Array of RuntimeTask records with status 'running'.
     */
    getRunning() {
        const results = [];
        for (const task of this._tasks.values()) {
            if (task.status === 'running') {
                results.push(task);
            }
        }
        return results;
    }
    /**
     * Returns all tasks that are direct children of the given parent task ID.
     *
     * @param parentId - The parent task ID.
     * @returns Array of child RuntimeTask records (may be empty).
     */
    getChildren(parentId) {
        const results = [];
        for (const task of this._tasks.values()) {
            if (task.parentTaskId === parentId) {
                results.push(task);
            }
        }
        return results;
    }
    /**
     * Returns all tasks matching the given status.
     *
     * @param status - The lifecycle status to filter by.
     * @returns Array of RuntimeTask records with the given status.
     */
    getByStatus(status) {
        const results = [];
        for (const task of this._tasks.values()) {
            if (task.status === status) {
                results.push(task);
            }
        }
        return results;
    }
    /**
     * Returns the total number of tasks currently tracked in the registry.
     */
    get size() {
        return this._tasks.size;
    }
    /**
     * Returns a snapshot of all tasks as a new Map.
     * Mutations to the returned Map do not affect the registry.
     */
    snapshot() {
        return new Map(this._tasks);
    }
}
