import _defineProperty from "@babel/runtime/helpers/defineProperty";
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 const createStubInvokeManuallyTriggeredRule = (stubbedInvocationResultForAllObjects, responseDelay) => async (_env, _site, _ruleId, objects, _inputs) => {
  const response = objects.reduce((acc, current) => ({
    ...acc,
    [current]: stubbedInvocationResultForAllObjects
  }), {});
  return responseDelay ? new Promise(resolve => setTimeout(resolve, responseDelay)) : Promise.resolve(response);
};
const Provider = ({
  children,
  use,
  target
}) => /*#__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 const renderWithDi = (node, dependencies = [], options) => render(node, {
  ...options,
  wrapper({
    children
  }) {
    var _options$wrapper;
    const 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: [...dependencies],
      target: options === null || options === void 0 ? void 0 : options.target
    }, /*#__PURE__*/React.createElement(CustomWrapper, null, children));
  }
});
export const createGenericComponent = (displayName, renderChildren = true, childrenParams) => {
  var _Class;
  return _Class = class extends Component {
    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;
    }
  }, _defineProperty(_Class, "displayName", displayName), _Class;
};