"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EdenEmitter = void 0;
require("reflect-metadata");
/**
 * Base class for managers that emit events within a specific namespace.
 *
 * Provides type-safe event emission where:
 * - Event names are constrained to the namespace's events
 * - Payload types are automatically inferred
 * - Namespace is extracted from the @EdenNamespace decorator
 *
 * @example
 * ```typescript
 * interface ProcessNamespaceEvents {
 *   "launched": { instance: AppInstance };
 *   "stopped": { appId: string };
 * }
 *
 * @EdenNamespace("process")
 * class ProcessManager extends EdenEmitter<ProcessNamespaceEvents> {
 *   constructor(ipcBridge: IPCBridge) {
 *     super(ipcBridge);
 *   }
 *
 *   async launchApp() {
 *     this.notify("launched", { instance }); // Fully typed!
 *   }
 * }
 * ```
 */
class EdenEmitter {
    ipcBridge;
    constructor(ipcBridge) {
        this.ipcBridge = ipcBridge;
    }
    /**
     * Emit an event within this manager's namespace.
     *
     * The namespace is automatically extracted from the @EdenNamespace decorator.
     * Event names and payloads are type-checked based on the TEvents generic parameter.
     *
     * @param event - Event name (without namespace prefix)
     * @param data - Event payload data
     */
    notify(event, data) {
        // Extract namespace from the @EdenNamespace decorator
        const namespace = Reflect.getMetadata("eden:namespace", this.constructor);
        if (!namespace) {
            throw new Error(`${this.constructor.name} must be decorated with @EdenNamespace to use notify(). ` +
                `Add @EdenNamespace("your-namespace") to the class.`);
        }
        // Construct full event name: "namespace/event"
        const fullEventName = `${namespace}/${String(event)}`;
        // Broadcast to all subscribed views
        this.ipcBridge.eventSubscribers.notify(fullEventName, data);
    }
    /**
     * Emit an event to a specific subscriber within this manager's namespace.
     *
     * Similar to notify(), but targets a specific view instead of broadcasting.
     *
     * @param viewId - The view ID to send the event to
     * @param event - Event name (without namespace prefix)
     * @param data - Event payload data
     */
    notifySubscriber(viewId, event, data) {
        // Extract namespace from the @EdenNamespace decorator
        const namespace = Reflect.getMetadata("eden:namespace", this.constructor);
        if (!namespace) {
            throw new Error(`${this.constructor.name} must be decorated with @EdenNamespace to use notifySubscriber(). ` +
                `Add @EdenNamespace("your-namespace") to the class.`);
        }
        // Construct full event name: "namespace/event"
        const fullEventName = `${namespace}/${String(event)}`;
        // Send to specific view
        this.ipcBridge.eventSubscribers.notifyView(viewId, fullEventName, data);
    }
}
exports.EdenEmitter = EdenEmitter;
//# sourceMappingURL=EdenEmitter.js.map