"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.renderWithDi = exports.createStubInvokeManuallyTriggeredRule = exports.createGenericComponent = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _react = _interopRequireWildcard(require("react"));
var _reactIntl = require("react-intl");
var _reactMagneticDi = require("react-magnetic-di");
var _testingLibrary = require("@atlassian/testing-library");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2.default)(o), (0, _possibleConstructorReturn2.default)(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2.default)(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; })(); }
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) { (0, _defineProperty2.default)(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; } // eslint-disable-next-line import/no-extraneous-dependencies
/**
 * Creates a stubbed invocation API call for triggering rules. Will return
 * the supplied invocation result for all objects the rule was called
 * for.
 */
var createStubInvokeManuallyTriggeredRule = exports.createStubInvokeManuallyTriggeredRule = function createStubInvokeManuallyTriggeredRule(stubbedInvocationResultForAllObjects, responseDelay) {
  return /*#__PURE__*/function () {
    var _ref = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(_env, _site, _ruleId, objects, _inputs) {
      var response;
      return _regenerator.default.wrap(function (_context) {
        while (1) switch (_context.prev = _context.next) {
          case 0:
            response = objects.reduce(function (acc, current) {
              return _objectSpread(_objectSpread({}, acc), {}, (0, _defineProperty2.default)({}, current, stubbedInvocationResultForAllObjects));
            }, {});
            return _context.abrupt("return", responseDelay ? new Promise(function (resolve) {
              return setTimeout(resolve, responseDelay);
            }) : Promise.resolve(response));
          case 1:
          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.default.createElement(_reactIntl.IntlProvider, {
    locale: "en"
  }, /*#__PURE__*/_react.default.createElement(_reactMagneticDi.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
 */
var renderWithDi = exports.renderWithDi = function renderWithDi(node) {
  var dependencies = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  var options = arguments.length > 2 ? arguments[2] : undefined;
  return (0, _testingLibrary.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 : _react.Fragment;
      return /*#__PURE__*/_react.default.createElement(Provider, {
        use: (0, _toConsumableArray2.default)(dependencies),
        target: options === null || options === void 0 ? void 0 : options.target
      }, /*#__PURE__*/_react.default.createElement(CustomWrapper, null, children));
    }
  }));
};
var createGenericComponent = exports.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() {
      (0, _classCallCheck2.default)(this, _Class);
      return _callSuper(this, _Class, arguments);
    }
    (0, _inherits2.default)(_Class, _Component);
    return (0, _createClass2.default)(_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;
      }
    }]);
  }(_react.Component), (0, _defineProperty2.default)(_Class, "displayName", displayName), _Class;
};