import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/inherits";
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
import _regeneratorRuntime from "@babel/runtime/regenerator";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import React, { Component, Fragment } from 'react';

// eslint-disable-next-line import/no-extraneous-dependencies
import { IntlProvider } from 'react-intl';
import { DiProvider } from 'react-magnetic-di';
import { render } from '@atlassian/testing-library';
/**
 * Creates a stubbed invocation API call for triggering rules. Will return
 * the supplied invocation result for all objects the rule was called
 * for.
 */
export var createStubInvokeManuallyTriggeredRule = function createStubInvokeManuallyTriggeredRule(stubbedInvocationResultForAllObjects, responseDelay) {
  return /*#__PURE__*/function () {
    var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(_env, _site, _ruleId, objects, _inputs) {
      var response;
      return _regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) switch (_context.prev = _context.next) {
          case 0:
            response = objects.reduce(function (acc, current) {
              return _objectSpread(_objectSpread({}, acc), {}, _defineProperty({}, current, stubbedInvocationResultForAllObjects));
            }, {});
            return _context.abrupt("return", responseDelay ? new Promise(function (resolve) {
              return setTimeout(resolve, responseDelay);
            }) : Promise.resolve(response));
          case 2:
          case "end":
            return _context.stop();
        }
      }, _callee);
    }));
    return function (_x, _x2, _x3, _x4, _x5) {
      return _ref.apply(this, arguments);
    };
  }();
};
var Provider = function Provider(_ref2) {
  var children = _ref2.children,
    use = _ref2.use,
    target = _ref2.target;
  return /*#__PURE__*/React.createElement(IntlProvider, {
    locale: "en"
  }, /*#__PURE__*/React.createElement(DiProvider, {
    use: use,
    target: target
  }, children));
};

/**
 * Render into a container which is appended to `document.body`.
 *
 * Includes `IntlProvider` wrapper
 *
 * @param node The React element to render
 * @param dependencies List of React Magnetic DI dependencies to inject
 * @param options React Testing Library options
 */
export var renderWithDi = function renderWithDi(node) {
  var dependencies = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  var options = arguments.length > 2 ? arguments[2] : undefined;
  return render(node, _objectSpread(_objectSpread({}, options), {}, {
    wrapper: function wrapper(_ref3) {
      var _options$wrapper;
      var children = _ref3.children;
      var CustomWrapper = (_options$wrapper = options === null || options === void 0 ? void 0 : options.wrapper) !== null && _options$wrapper !== void 0 ? _options$wrapper : Fragment;
      return /*#__PURE__*/React.createElement(Provider, {
        use: _toConsumableArray(dependencies),
        target: options === null || options === void 0 ? void 0 : options.target
      }, /*#__PURE__*/React.createElement(CustomWrapper, null, children));
    }
  }));
};
export var createGenericComponent = function createGenericComponent(displayName) {
  var _Class;
  var renderChildren = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  var childrenParams = arguments.length > 2 ? arguments[2] : undefined;
  return _Class = /*#__PURE__*/function (_Component) {
    function _Class() {
      _classCallCheck(this, _Class);
      return _callSuper(this, _Class, arguments);
    }
    _inherits(_Class, _Component);
    return _createClass(_Class, [{
      key: "render",
      value: function render() {
        if (renderChildren && typeof this.props.children === 'function') {
          if (childrenParams) {
            return this.props.children(childrenParams);
          }
          return this.props.children();
        }
        if (renderChildren && this.props.children) {
          return this.props.children;
        }
        return null;
      }
    }]);
  }(Component), _defineProperty(_Class, "displayName", displayName), _Class;
};