import * as React from 'react';
import { POST_MESSAGE_SOURCE } from "./constants.js";
export function isValidPostMessage(data) {
    return (typeof data === 'object' &&
        data !== null &&
        data.source === POST_MESSAGE_SOURCE);
}
export function requestActionPostMessage(action, data, destinationElement) {
    const message = { action, data, source: POST_MESSAGE_SOURCE };
    destinationElement.postMessage(message, '*');
}
export function listenPostMessageEvents(action, callback) {
    const onMessage = (e) => {
        if (!isValidPostMessage(e.data)) {
            return undefined;
        }
        const message = e.data;
        if (message.action === action) {
            return callback(message.data);
        }
        return undefined;
    };
    window.addEventListener('message', onMessage);
    return () => window.removeEventListener('message', onMessage);
}
export function usePostMessageAPIListener(action, callback, deps = []) {
    React.useEffect(() => {
        return listenPostMessageEvents(action, callback);
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [...deps]);
}
//# sourceMappingURL=postMessage.js.map
