import { useEffect, useState } from "react";
import { connectToParent, connectToChild, ErrorCode } from "penpal";
import connectToChildV4 from "penpal-v4/lib/connectToChild";
import { inIframe } from "../../utils";
import { getLogger } from "../../logger";
const {
  trace
} = getLogger("useFrame");
const TIMEOUT = 30000;
export const useParentFrame = function (_ref) {
  let {
    dispatch
  } = _ref;
  const [parentFrameConnection, setParentFrameConnection] = useState();
  const [status, setStatus] = useState("DISCONNECTED");
  useEffect(() => {
    if (inIframe() && status === "DISCONNECTED") {
      const parentV5 = connectToParent({
        methods: {
          dispatch: dispatch
        },
        timeout: TIMEOUT // this will ensure connection to promise reject, when connection versions not match. otherwise it will be stucked in promise pending
      }).promise;
      setStatus("CONNECTING");
      parentV5.then(parentConnection => {
        trace("connectToParent success: ", parentConnection);
        setParentFrameConnection(parentConnection);
        setStatus("CONNECTED");
      }).catch(err => {
        trace("connectToParent failed: ", err);
        if (err.code === ErrorCode.ConnectionTimeout) {
          setStatus("TIMEOUT");
        } else {
          setStatus("DISCONNECTED");
        }
      });
    }
  }, [status, dispatch]);
  return [status === "CONNECTED", parentFrameConnection];
};
export const useChildFrame = function (props) {
  const [childFrameConnection, setChildFrameConnection] = useState();
  const [status, setStatus] = useState("DISCONNECTED");
  useEffect(() => {
    if (props.iframe.current && status === "DISCONNECTED") {
      const childV5 = connectToChild({
        methods: {
          dispatch: props.dispatch,
          ...props.methods
        },
        iframe: props.iframe.current,
        timeout: TIMEOUT // this will ensure connection to promise reject, when connection versions not match. otherwise it will be stucked in promise pending
      }).promise;
      const childV4 = connectToChildV4({
        methods: {
          dispatch: props.dispatch,
          ...props.methods
        },
        iframe: props.iframe.current,
        timeout: TIMEOUT // this will ensure connection to promise reject, when connection versions not match. otherwise it will be stucked in promise pending
      }).promise;
      setStatus("CONNECTING");
      Promise.any([childV5, childV4]).then(childConnection => {
        trace("connectToChild success: ", childConnection);
        setChildFrameConnection(childConnection);
        setStatus("CONNECTED");
      }).catch(err => {
        trace("connectToChild failed: ", err);
        if (err.errors.map(e => e.code).some(c => c === ErrorCode.ConnectionTimeout)) {
          setStatus("TIMEOUT");
        } else {
          setStatus("DISCONNECTED");
        }
      });
    }
  }, [status, props]);
  return [status === "CONNECTED", status === "TIMEOUT", childFrameConnection];
};
//# sourceMappingURL=useFrame.js.map