/**
 * Manages lifecycle event emission and tracking for intent execution
 */
export class LifecycleManager {
    events = [];
    listeners = new Map();
    /**
     * Register a listener for a specific event type
     */
    on(eventType, handler) {
        if (!this.listeners.has(eventType)) {
            this.listeners.set(eventType, new Set());
        }
        this.listeners.get(eventType).add(handler);
    }
    /**
     * Emit a lifecycle event
     */
    emit(type, intentId, phase, data) {
        const event = {
            type,
            timestamp: new Date().toISOString(),
            intentId,
            phase,
            data,
        };
        this.events.push(event);
        // Notify listeners
        const handlers = this.listeners.get(type);
        if (handlers) {
            handlers.forEach((handler) => handler(event));
        }
    }
    /**
     * Get all recorded events
     */
    getEvents() {
        return [...this.events];
    }
    /**
     * Clear all events and listeners
     */
    reset() {
        this.events = [];
        this.listeners.clear();
    }
    /**
     * Get events for a specific intent
     */
    getIntentEvents(intentId) {
        return this.events.filter((e) => e.intentId === intentId);
    }
}
/**
 * Helper to emit common lifecycle events
 */
export const createLifecycleHelpers = (manager) => ({
    emitIntentReceived: (intentId, request) => manager.emit("intent-received", intentId, "init", { request }),
    emitIntentRouted: (intentId, plan) => manager.emit("intent-routed", intentId, "plan", { plan }),
    emitPreconditionsChecked: (intentId, passed, failures) => manager.emit("preconditions-checked", intentId, "plan", {
        passed,
        failures,
    }),
    emitSimulationStarted: (intentId, plan) => manager.emit("simulation-started", intentId, "plan", { plan }),
    emitSimulationPassed: (intentId) => manager.emit("simulation-passed", intentId, "plan", {}),
    emitExecutionStarted: (intentId, plan) => manager.emit("execution-started", intentId, "execute", { plan }),
    emitTaskAttempt: (intentId, record) => manager.emit("task-attempt", intentId, "execute", { record }),
    emitHealingApplied: (intentId, decision, error) => manager.emit("healing-applied", intentId, "heal", { decision, error }),
    emitExecutionComplete: (intentId, success) => manager.emit("execution-complete", intentId, "complete", { success }),
    emitIntentSuccess: (intentId, result) => manager.emit("intent-success", intentId, "complete", { result }),
    emitIntentFailed: (intentId, error) => manager.emit("intent-failed", intentId, "complete", { error }),
    emitIntentBlocked: (intentId, reason) => manager.emit("intent-blocked", intentId, "complete", { reason }),
});
//# sourceMappingURL=lifecycle.js.map