import { obfuscateField, updateHeight, updateTemplates } from "./frame.actions";
import React, { useEffect } from "react";
import { useParentFrame } from "./useFrame";
import { renderDocument, selectTemplate, print } from "./host.actions";
import { isActionOf } from "typesafe-actions";
/**
 * Component connecting to the host of the frame.
 * Once the connection has been established, `onConnected` will be called and will provide, as first parameter, a function to send actions to the host.
 * This component must be provided a `dispatch `function that will listen for actions coming from the host.
 */
export var HostConnector = function (_a) {
    var dispatch = _a.dispatch, children = _a.children, onConnected = _a.onConnected;
    var _b = useParentFrame({
        dispatch: dispatch,
        methods: {
            renderDocument: function (document, rawDocument) {
                dispatch(renderDocument({ document: document, rawDocument: rawDocument }));
            },
            selectTemplateTab: function (tabIndex) {
                dispatch(selectTemplate(tabIndex));
            },
            print: function () {
                dispatch(print());
            }
        }
    }), connected = _b[0], toHost = _b[1];
    useEffect(function () {
        if (connected) {
            onConnected(function (action) {
                if (toHost.dispatch) {
                    toHost.dispatch(action);
                }
                else if (isActionOf(updateHeight, action) && toHost.updateHeight) {
                    toHost.updateHeight(action.payload);
                }
                else if (isActionOf(obfuscateField, action) && toHost.handleObfuscation) {
                    toHost.handleObfuscation(action.payload);
                }
                else if (isActionOf(updateTemplates, action) && toHost.updateTemplates) {
                    toHost.updateTemplates(action.payload);
                }
            });
        }
    }, [connected, toHost, onConnected]);
    return React.createElement(React.Fragment, null, children);
};
//# sourceMappingURL=HostConnector.js.map