import _Object$keys from "@babel/runtime-corejs3/core-js-stable/object/keys";
import _Object$getOwnPropertySymbols from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols";
import _filterInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/filter";
import _Object$getOwnPropertyDescriptor from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor";
import _Object$getOwnPropertyDescriptors from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors";
import _defineProperty from "@babel/runtime-corejs3/helpers/defineProperty";
import _slicedToArray from "@babel/runtime-corejs3/helpers/slicedToArray";
import _Promise from "@babel/runtime-corejs3/core-js-stable/promise";
import _mapInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/map";

function ownKeys(object, enumerableOnly) { var keys = _Object$keys(object); if (_Object$getOwnPropertySymbols) { var symbols = _Object$getOwnPropertySymbols(object); if (enumerableOnly) { symbols = _filterInstanceProperty(symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (_Object$getOwnPropertyDescriptors) { Object.defineProperties(target, _Object$getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } } return target; }

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";

var _getLogger = getLogger("useFrame"),
    trace = _getLogger.trace;

var TIMEOUT = 30000;
export var useParentFrame = function useParentFrame(_ref) {
  var dispatch = _ref.dispatch;

  var _useState = useState(),
      _useState2 = _slicedToArray(_useState, 2),
      parentFrameConnection = _useState2[0],
      setParentFrameConnection = _useState2[1];

  var _useState3 = useState("DISCONNECTED"),
      _useState4 = _slicedToArray(_useState3, 2),
      status = _useState4[0],
      setStatus = _useState4[1];

  useEffect(function () {
    if (inIframe() && status === "DISCONNECTED") {
      var 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(function (parentConnection) {
        trace("connectToParent success: ", parentConnection);
        setParentFrameConnection(parentConnection);
        setStatus("CONNECTED");
      }).catch(function (err) {
        trace("connectToParent failed: ", err);

        if (err.code === ErrorCode.ConnectionTimeout) {
          setStatus("TIMEOUT");
        } else {
          setStatus("DISCONNECTED");
        }
      });
    }
  }, [status, dispatch]);
  return [status === "CONNECTED", parentFrameConnection];
};
export var useChildFrame = function useChildFrame(props) {
  var _useState5 = useState(),
      _useState6 = _slicedToArray(_useState5, 2),
      childFrameConnection = _useState6[0],
      setChildFrameConnection = _useState6[1];

  var _useState7 = useState("DISCONNECTED"),
      _useState8 = _slicedToArray(_useState7, 2),
      status = _useState8[0],
      setStatus = _useState8[1];

  useEffect(function () {
    if (props.iframe.current && status === "DISCONNECTED") {
      var childV5 = connectToChild({
        methods: _objectSpread({
          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;
      var childV4 = connectToChildV4({
        methods: _objectSpread({
          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(function (childConnection) {
        trace("connectToChild success: ", childConnection);
        setChildFrameConnection(childConnection);
        setStatus("CONNECTED");
      }).catch(function (err) {
        var _context;

        trace("connectToChild failed: ", err);

        if (_mapInstanceProperty(_context = err.errors).call(_context, function (e) {
          return e.code;
        }).some(function (c) {
          return c === ErrorCode.ConnectionTimeout;
        })) {
          setStatus("TIMEOUT");
        } else {
          setStatus("DISCONNECTED");
        }
      });
    }
  }, [status, props]);
  return [status === "CONNECTED", status === "TIMEOUT", childFrameConnection];
};
//# sourceMappingURL=useFrame.js.map