'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 _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 }; }

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } /* global document, window */

var pluginContext = (0, _createPluginContext2.default)();

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

module.exports = function (uiConfig) {
  var config = (0, _configHelpers.normalizeConfig)((0, _configHelpers.mergeConfig)(defaultConfig, uiConfig, pluginContext));

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


  var 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)([].concat(_toConsumableArray(_en2.default)));

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

    var _intlProvider$getChil = intlProvider.getChildContext(),
        intl = _intlProvider$getChil.intl;

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


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

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

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

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

    var props = {
      config: config,
      store: store
    };

    _cspaceLayout.Modal.setAppElement(mountNode);

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