import { EventEmitter } from 'node:events';
import process from 'node:process';
// eslint-disable-next-line no-restricted-syntax
export var IpcIncomingEvent;
(function (IpcIncomingEvent) {
    IpcIncomingEvent["RESTART_CHILD_BRIDGE"] = "restartChildBridge";
    IpcIncomingEvent["STOP_CHILD_BRIDGE"] = "stopChildBridge";
    IpcIncomingEvent["START_CHILD_BRIDGE"] = "startChildBridge";
    IpcIncomingEvent["CHILD_BRIDGE_METADATA_REQUEST"] = "childBridgeMetadataRequest";
    IpcIncomingEvent["START_MATTER_MONITORING"] = "startMatterMonitoring";
    IpcIncomingEvent["STOP_MATTER_MONITORING"] = "stopMatterMonitoring";
    IpcIncomingEvent["GET_MATTER_ACCESSORIES"] = "getMatterAccessories";
    IpcIncomingEvent["GET_MATTER_ACCESSORY_INFO"] = "getMatterAccessoryInfo";
    IpcIncomingEvent["MATTER_ACCESSORY_CONTROL"] = "matterAccessoryControl";
})(IpcIncomingEvent || (IpcIncomingEvent = {}));
// eslint-disable-next-line no-restricted-syntax
export var IpcOutgoingEvent;
(function (IpcOutgoingEvent) {
    IpcOutgoingEvent["SERVER_STATUS_UPDATE"] = "serverStatusUpdate";
    IpcOutgoingEvent["CHILD_BRIDGE_METADATA_RESPONSE"] = "childBridgeMetadataResponse";
    IpcOutgoingEvent["CHILD_BRIDGE_STATUS_UPDATE"] = "childBridgeStatusUpdate";
    IpcOutgoingEvent["MATTER_EVENT"] = "matterEvent";
})(IpcOutgoingEvent || (IpcOutgoingEvent = {}));
// eslint-disable-next-line ts/no-unsafe-declaration-merging
export class IpcService extends EventEmitter {
    messageHandler = (message) => {
        if (!message || typeof message !== 'object' || !message.id) {
            return;
        }
        this.emit(message.id, message.data);
    };
    constructor() {
        super();
    }
    /**
     * Start the IPC service listeners.
     * Currently this will only listen for messages from a parent process.
     */
    start() {
        process.on('message', this.messageHandler);
    }
    /**
     * Stop the IPC service listeners.
     */
    stop() {
        process.removeListener('message', this.messageHandler);
        // Also drop any EventEmitter listeners registered via ipcService.on(...)
        // by Server. Without this, those listeners outlive the service and retain
        // the Server through closures across hypothetical reload scenarios.
        this.removeAllListeners();
    }
    /**
     * Send a message to connected IPC clients.
     * Currently, this will only send messages if Homebridge was launched as a child_process.fork()
     * from another Node.js process (such as hb-service).
     */
    sendMessage(id, data) {
        if (process.send) {
            process.send({
                id,
                data,
            });
        }
    }
}
//# sourceMappingURL=ipcService.js.map