/**
 * This approach decouples the communication logic from PaymentMethodForm component,
 * making it cleaner, more maintainable, and easier to test
 */
export class FrameCommunicationService {
    constructor(iframe, iframeOrigin) {
        this.iframe = iframe;
        this.iframeOrigin = iframeOrigin;
    }
    postMessage(eventType, payload) {
        const message = Object.assign({ eventType }, payload);
        this.iframe.contentWindow.postMessage(message, this.iframeOrigin);
    }
    addMessageListener(callback) {
        window.addEventListener('message', callback);
    }
    removeMessageListener(callback) {
        window.removeEventListener('message', callback);
    }
    postMessageWithResponseListener(eventType, payload) {
        return new Promise((resolve) => {
            const responseListener = (event) => {
                if (event.data.eventType !== eventType)
                    return;
                window.removeEventListener('message', responseListener);
                resolve(event.data.data);
            };
            window.addEventListener('message', responseListener);
            this.postMessage(eventType, payload);
        });
    }
}
