"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendEventPostMessage = sendEventPostMessage;
exports.listenPostMessageActions = listenPostMessageActions;
exports.useInternalPostMessageAPIListener = useInternalPostMessageAPIListener;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const constants_1 = require("../common/constants.js");
const postMessage_1 = require("../common/postMessage.js");
function sendEventPostMessage(action, data) {
    const message = { action, data, source: constants_1.POST_MESSAGE_SOURCE };
    window.parent.postMessage(message, '*');
}
function listenPostMessageActions(action, callback) {
    const onMessage = (e) => {
        if (!(0, postMessage_1.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);
}
function useInternalPostMessageAPIListener(action, callback) {
    React.useEffect(() => {
        return listenPostMessageActions(action, callback);
    }, [action, callback]);
}
//# sourceMappingURL=usePostMessageAPI.js.map
