import { useEffect, useCallback, useState } from "react";
export const sendToChildIframe = (htmlIframeElement, message) => {
    const iframeElement = typeof htmlIframeElement === "string"
        ? document.getElementById(htmlIframeElement)
        : htmlIframeElement;
    if (iframeElement.contentWindow) {
        iframeElement.contentWindow.postMessage(message, "*");
    }
};
export const useListenToPostMessage = (messageType, callback, defaultState) => {
    const [lastMessage, setLastMessage] = useState(defaultState);
    const handleMessage = useCallback(message => {
        const data = message.data;
        if (data.type === messageType) {
            setLastMessage(data.payload);
            if (callback) {
                callback(data.payload);
            }
        }
    }, [callback, messageType]);
    const resetMesssage = useCallback(() => {
        setLastMessage(defaultState);
    }, [defaultState]);
    useEffect(() => {
        window.addEventListener("message", handleMessage);
        return () => {
            window.removeEventListener("message", handleMessage);
        };
    }, [handleMessage, messageType]);
    return [lastMessage, resetMesssage];
};
//# sourceMappingURL=post-message.js.map