import React from 'react';
import PropTypes from 'prop-types';
import { render as rtlRender } from '@testing-library/react';
import { IntlProvider } from 'react-intl';
import { Router, Route } from 'react-router-dom';
import history from './history';
import { Provider } from 'react-redux';
import { configureStore } from '@capillarytech/vulcan-react-sdk/utils';
import { initialReducer } from '../initialReducer';

function Wrapper({ children }) {
  const store = configureStore({}, initialReducer, history);
  return (
    <Provider store={store}>
      <IntlProvider locale="en">{children}</IntlProvider>
    </Provider>
  );
}
Wrapper.propTypes = {
  children: PropTypes.array,
};

function render(ui, renderOptions = {}) {
  return rtlRender(ui, { wrapper: Wrapper, ...renderOptions });
}


function renderWithRouter(
  Component,
  props,
  { routePath = '/', historyPush = '/' } = {},
  renderOptions,
) {
  history.push(historyPush);

  return {
    history,
    ...rtlRender(
       <Router history={history}>
        <Route
        path={routePath}
          render={(routeProps) => <Component {...props} {...routeProps} />}
        />
      </Router>,
      { wrapper: Wrapper, ...renderOptions },
    ),
  };
}

// re-export everything
export * from '@testing-library/react';

// override render method
export { render, renderWithRouter };
