import _slicedToArray from "@babel/runtime-corejs3/helpers/slicedToArray";
import React, { useEffect } from "react";
import { useParentFrame } from "./useFrame";
import { jsx as ___EmotionJSX } from "@emotion/react";

/**
 * 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 HostConnector(_ref) {
  var dispatch = _ref.dispatch,
      children = _ref.children,
      onConnected = _ref.onConnected;

  var _useParentFrame = useParentFrame({
    dispatch: dispatch
  }),
      _useParentFrame2 = _slicedToArray(_useParentFrame, 2),
      connected = _useParentFrame2[0],
      toHost = _useParentFrame2[1];

  useEffect(function () {
    if (connected) {
      onConnected(function (action) {
        if (toHost.dispatch) {
          toHost.dispatch(action);
        }
      });
    }
  }, [connected, toHost, onConnected]);
  return ___EmotionJSX(React.Fragment, null, children);
};
//# sourceMappingURL=HostConnector.js.map