class MessagingService {
    constructor() {
        this.promiseIdCounter = 0;
        this.promiseMap = new Map();
        this.messagingServices = new Map();
        this.isBrowser = typeof window !== 'undefined';
        this.setupMessageListener();
    }
    generatePromiseId() {
        return `promise_${++this.promiseIdCounter}`;
    }
    sendMessage(action, payload) {
        if (!this.isBrowser)
            return;
        const message = {
            type: 'MULTIPLAYER_SESSION_DEBUGGER_LIB',
            action,
            payload,
        };
        window.postMessage(message, '*');
    }
    sendMessagePromise(action, payload) {
        if (!this.isBrowser) {
            return Promise.reject(new Error('Not in browser environment'));
        }
        const promiseId = this.generatePromiseId();
        const promise = new Promise((resolve, reject) => {
            this.promiseMap.set(promiseId, { resolve, reject });
        });
        const message = {
            type: 'MULTIPLAYER_SESSION_DEBUGGER_LIB',
            action,
            payload,
            promiseId,
        };
        window.postMessage(message, '*');
        return promise;
    }
    on(action, handler) {
        const handlers = this.messagingServices.get(action) || [];
        handlers.push(handler);
        this.messagingServices.set(action, handlers);
    }
    off(action, handler) {
        if (!handler) {
            // Remove all handlers for this action
            this.messagingServices.delete(action);
            return;
        }
        const handlers = this.messagingServices.get(action);
        if (handlers) {
            const index = handlers.indexOf(handler);
            if (index !== -1) {
                handlers.splice(index, 1);
                if (handlers.length === 0) {
                    this.messagingServices.delete(action);
                }
                else {
                    this.messagingServices.set(action, handlers);
                }
            }
        }
    }
    setupMessageListener() {
        if (!this.isBrowser)
            return;
        window.addEventListener('message', (event) => {
            const { type, action, payload, promiseId } = event.data;
            if (type !== 'MULTIPLAYER_SESSION_DEBUGGER_EXTENSION' || !action)
                return;
            // Handle promise response
            if (promiseId && this.promiseMap.has(promiseId)) {
                const { resolve, reject } = this.promiseMap.get(promiseId);
                const { error, response } = payload;
                if (error) {
                    reject(error);
                }
                else {
                    resolve(response);
                }
                this.promiseMap.delete(promiseId);
                return;
            }
            // Handle regular message handlers
            const handlers = this.messagingServices.get(action);
            if (handlers) {
                handlers.forEach(handler => handler(payload));
            }
        });
    }
}
const messagingService = new MessagingService();
export default messagingService;
//# sourceMappingURL=messaging.service.js.map