"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useLoopBackHubspotEvents = useLoopBackHubspotEvents;
exports.useHandleHubspotEvents = useHandleHubspotEvents;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const hubspot_1 = require("../../utils/hubspot.js");
/**
 * @param {string} formId
 * @returns {void}
 *
 * @description Use this hook on top level frame to loopback hubspot forms events back to origin frame
 */
function useLoopBackHubspotEvents(formId) {
    React.useEffect(() => {
        const topHandler = (0, hubspot_1.loopBackHabspotEvents)(formId);
        window.addEventListener('message', topHandler, { passive: true });
        return () => {
            window.removeEventListener('message', topHandler);
        };
    }, [formId]);
}
/**
 * @param {HubspotEventHandlers} handlers form event handlers, should be stable across renders
 * @param {string} formId
 * @returns {void}
 *
 * @description Use this hook in any frame to handle hubspot forms events
 */
function useHandleHubspotEvents(handlers, formId) {
    React.useEffect(() => {
        const topHandler = (0, hubspot_1.handleHubspotEvents)(handlers, formId);
        window.addEventListener('message', topHandler, { passive: true });
        return () => {
            window.removeEventListener('message', topHandler);
        };
    }, [handlers, formId]);
}
//# sourceMappingURL=hubspot.js.map
