'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 _reactRouter = require('react-router');

var _history = require('history');

var _reactRouterRedux = require('react-router-redux');

var _scriptjs = require('scriptjs');

var _scriptjs2 = _interopRequireDefault(_scriptjs);

var _warning = require('warning');

var _warning2 = _interopRequireDefault(_warning);

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

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

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

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

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

var _reducers2 = _interopRequireDefault(_reducers);

var _App = require('./components/App');

var _App2 = _interopRequireDefault(_App);

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

var _createPluginContext2 = _interopRequireDefault(_createPluginContext);

var _shared = require('./plugins/optionLists/shared');

var _shared2 = _interopRequireDefault(_shared);

var _default = require('./plugins/listTypes/default');

var _default2 = _interopRequireDefault(_default);

var _all = require('./plugins/recordTypes/all');

var _all2 = _interopRequireDefault(_all);

var _authority = require('./plugins/recordTypes/authority');

var _authority2 = _interopRequireDefault(_authority);

var _citation = require('./plugins/recordTypes/citation');

var _citation2 = _interopRequireDefault(_citation);

var _collectionobject = require('./plugins/recordTypes/collectionobject');

var _collectionobject2 = _interopRequireDefault(_collectionobject);

var _concept = require('./plugins/recordTypes/concept');

var _concept2 = _interopRequireDefault(_concept);

var _group = require('./plugins/recordTypes/group');

var _group2 = _interopRequireDefault(_group);

var _intake = require('./plugins/recordTypes/intake');

var _intake2 = _interopRequireDefault(_intake);

var _loanin = require('./plugins/recordTypes/loanin');

var _loanin2 = _interopRequireDefault(_loanin);

var _media = require('./plugins/recordTypes/media');

var _media2 = _interopRequireDefault(_media);

var _object = require('./plugins/recordTypes/object');

var _object2 = _interopRequireDefault(_object);

var _organization = require('./plugins/recordTypes/organization');

var _organization2 = _interopRequireDefault(_organization);

var _person = require('./plugins/recordTypes/person');

var _person2 = _interopRequireDefault(_person);

var _place = require('./plugins/recordTypes/place');

var _place2 = _interopRequireDefault(_place);

var _procedure = require('./plugins/recordTypes/procedure');

var _procedure2 = _interopRequireDefault(_procedure);

var _refs = require('./plugins/subresources/refs');

var _refs2 = _interopRequireDefault(_refs);

var _terms = require('./plugins/subresources/terms');

var _terms2 = _interopRequireDefault(_terms);

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

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

var loadPolyfills = function loadPolyfills(locale, callback) {
  if (window.Intl) {
    window.setTimeout(callback, 0);
  } else {
    var url = 'https://cdn.polyfill.io/v2/polyfill.min.js?features=Intl.~locale.' + locale;

    (0, _scriptjs2.default)(url, callback);
  }
}; /* global document, window */

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

var defaultConfig = (0, _configHelpers.mergeConfig)({
  basename: '',
  className: '',
  container: 'main',
  cspaceUrl: '',
  index: undefined,
  locale: 'en',
  messages: undefined,
  prettyUrls: false
}, {
  plugins: [(0, _shared2.default)(), (0, _default2.default)(), (0, _all2.default)(), (0, _authority2.default)(), (0, _citation2.default)(), (0, _collectionobject2.default)(), (0, _concept2.default)(), (0, _group2.default)(), (0, _intake2.default)(), (0, _loanin2.default)(), (0, _media2.default)(), (0, _object2.default)(), (0, _person2.default)(), (0, _place2.default)(), (0, _organization2.default)(), (0, _procedure2.default)(), (0, _refs2.default)(), (0, _terms2.default)()]
}, pluginContext);

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

  var basename = config.basename,
      className = config.className,
      container = config.container,
      cspaceUrl = config.cspaceUrl,
      idGenerators = config.idGenerators,
      locale = config.locale,
      optionLists = config.optionLists,
      prettyUrls = config.prettyUrls;


  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;

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

    var baseHistory = prettyUrls ? (0, _reactRouter.useRouterHistory)((0, _history.useBeforeUnload)(_history.createHistory))({ basename: basename }) : (0, _reactRouter.useRouterHistory)((0, _history.useBeforeUnload)(_history.createHashHistory))();

    baseHistory.listenBeforeUnload(function () {
      store.dispatch((0, _prefs.savePrefs)());
    });

    var history = (0, _reactRouterRedux.syncHistoryWithStore)(baseHistory, store);

    store.dispatch((0, _cspace.configureCSpace)({
      url: cspaceUrl
    }));

    store.dispatch((0, _prefs.loadPrefs)());
    store.dispatch((0, _optionList.addOptionLists)(optionLists));
    store.dispatch((0, _idGenerator.addIDGenerators)(idGenerators));

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

    loadPolyfills(locale, function () {
      (0, _reactDom.render)(_react2.default.createElement(_App2.default, props), mountNode);
    });
  }
};