"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.requestActionPostMessage = requestActionPostMessage;
exports.listenPostMessageEvents = listenPostMessageEvents;
exports.usePostMessageAPIListener = usePostMessageAPIListener;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
function requestActionPostMessage(action, data, destinationElement) {
    const message = { action, data };
    destinationElement.postMessage(message);
}
function listenPostMessageEvents(action, callback) {
    const onMessage = (e) => {
        const message = e.data;
        if ('action' in message && message.action === action) {
            return callback(message.data);
        }
        return undefined;
    };
    window.addEventListener('message', onMessage);
    return () => {
        window.removeEventListener('message', onMessage);
    };
}
function usePostMessageAPIListener(action, callback, deps = []) {
    React.useEffect(() => {
        return listenPostMessageEvents(action, callback);
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [...deps]);
}
//# sourceMappingURL=postMessage.js.map
