'use strict';

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactDom = require('react-dom');

var _redux = require('redux');

var _reduxThunk = require('redux-thunk');

var _reduxThunk2 = _interopRequireDefault(_reduxThunk);

var _reactIntl = require('react-intl');

var _en = require('react-intl/locale-data/en');

var _en2 = _interopRequireDefault(_en);

var _warning = require('warning');

var _warning2 = _interopRequireDefault(_warning);

var _cspaceLayout = require('cspace-layout');

var _collectionspace = require('../images/collectionspace.svg');

var _collectionspace2 = _interopRequireDefault(_collectionspace);

var _cspace = require('./actions/cspace');

var _idGenerator = require('./actions/idGenerator');

var _optionList = require('./actions/optionList');

var _prefs = require('./actions/prefs');

var _searchOperators = require('./constants/searchOperators');

var _plugins = require('./plugins');

var _plugins2 = _interopRequireDefault(_plugins);

var _reducers = require('./reducers');

var _reducers2 = _interopRequireDefault(_reducers);

var _AppContainer = require('./containers/AppContainer');

var _AppContainer2 = _interopRequireDefault(_AppContainer);

var _createPluginContext = require('./helpers/createPluginContext');

var _createPluginContext2 = _interopRequireDefault(_createPluginContext);

var _configHelpers = require('./helpers/configHelpers');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

const pluginContext = (0, _createPluginContext2.default)(); /* global document, window */

const defaultConfig = (0, _configHelpers.mergeConfig)({
  autocompleteFindDelay: 500,
  autocompleteMinLength: 3,
  basename: '',
  className: '',
  container: '#cspace',
  defaultAdvancedSearchBooleanOp: _searchOperators.OP_OR,
  index: '/search',
  locale: 'en-US',
  logo: _collectionspace2.default,
  mediaSnapshotSort: 'title',
  messages: undefined,
  prettyUrls: false,
  serverUrl: '',
  showTermListStateIcon: false,
  tenantId: '1',
  termDeprecationEnabled: false
}, {
  plugins: _plugins2.default.map(plugin => plugin())
}, pluginContext);

module.exports = uiConfig => {
  const resolvedUiConfig = (0, _configHelpers.evaluatePlugin)(uiConfig, pluginContext);
  const config = (0, _configHelpers.mergeConfig)(defaultConfig, resolvedUiConfig, pluginContext);

  const container = config.container,
        idGenerators = config.idGenerators,
        optionLists = config.optionLists;


  const mountNode = document.querySelector(container);

  process.env.NODE_ENV !== 'production' ? (0, _warning2.default)(mountNode, `No container element was found using the selector '${container}'. The CollectionSpace UI will not be rendered.`) : void 0;

  if (mountNode) {
    process.env.NODE_ENV !== 'production' ? (0, _warning2.default)(mountNode !== document.body, `The container element for the CollectionSpace UI found using the selector '${container}' is the document body. This may cause problems, and is not supported.`) : void 0;

    (0, _reactIntl.addLocaleData)([..._en2.default]);

    const intlProvider = new _reactIntl.IntlProvider({
      locale: config.locale,
      messages: config.messages
    });

    var _intlProvider$getChil = intlProvider.getChildContext();

    const intl = _intlProvider$getChil.intl;

    // eslint-disable-next-line no-underscore-dangle

    const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || _redux.compose;

    const store = (0, _redux.createStore)(_reducers2.default, composeEnhancers((0, _redux.applyMiddleware)(_reduxThunk2.default.withExtraArgument(intl))));

    window.addEventListener('beforeunload', () => {
      store.dispatch((0, _prefs.savePrefs)());
    });

    store.dispatch((0, _cspace.configureCSpace)(config));
    store.dispatch((0, _optionList.addOptionLists)(optionLists));
    store.dispatch((0, _idGenerator.addIDGenerators)(idGenerators));

    const props = {
      config,
      store
    };

    _cspaceLayout.Modal.setAppElement(mountNode);

    (0, _reactDom.render)(_react2.default.createElement(_AppContainer2.default, props), mountNode);
  }
};