"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _react = _interopRequireDefault(require("react"));

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

var _redux = require("redux");

var _reduxThunk = _interopRequireDefault(require("redux-thunk"));

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

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

var _warning = _interopRequireDefault(require("warning"));

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

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

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 = _interopRequireDefault(require("./plugins"));

var _reducers = _interopRequireDefault(require("./reducers"));

var _AppContainer = _interopRequireDefault(require("./containers/AppContainer"));

var _createConfigContext = _interopRequireDefault(require("./helpers/createConfigContext"));

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

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

/* global document, window */
const configContext = (0, _createConfigContext.default)();
const defaultConfig = (0, _configHelpers.mergeConfig)({
  allowDeleteHierarchyLeaves: false,
  autocompleteFindDelay: 500,
  autocompleteMinLength: 3,
  basename: '',
  className: '',
  container: '#cspace',
  defaultAdvancedSearchBooleanOp: _searchOperators.OP_OR,
  defaultDropdownFilter: 'substring',
  defaultSearchPageSize: 20,
  defaultSearchPanelSize: 5,
  defaultUserPrefs: {
    panels: {
      collectionobject: {
        mediaSnapshotPanel: {
          collapsed: false
        }
      }
    }
  },
  index: '/search',
  locale: 'en-US',
  logo: _collectionspace.default,
  mediaSnapshotSort: 'title',
  messages: undefined,
  prettyUrls: false,
  relationMemberPerm: 'U',
  serverUrl: '',
  showTermListStateIcon: false,
  structDateOptionListNames: ['dateQualifiers'],
  structDateVocabNames: ['dateera', 'datecertainty', 'datequalifier'],
  tenantId: '1',
  termDeprecationEnabled: false
}, {
  plugins: _plugins.default.map(plugin => plugin())
}, configContext);

var _default = uiConfig => {
  const resolvedUiConfig = (0, _configHelpers.evaluatePlugin)(uiConfig, configContext);
  const config = (0, _configHelpers.finalizeRecordTypes)((0, _configHelpers.mergeConfig)(defaultConfig, resolvedUiConfig, configContext));
  const {
    container,
    idGenerators,
    optionLists
  } = config;
  const mountNode = document.querySelector(container);
  process.env.NODE_ENV !== "production" ? (0, _warning.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, _warning.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)([..._en.default]);
    const intlProvider = new _reactIntl.IntlProvider({
      locale: config.locale,
      messages: config.messages
    });
    const {
      intl
    } = intlProvider.getChildContext(); // eslint-disable-next-line no-underscore-dangle

    const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || _redux.compose;
    const store = (0, _redux.createStore)(_reducers.default, composeEnhancers((0, _redux.applyMiddleware)(_reduxThunk.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));
    store.dispatch((0, _cspace.readSystemInfo)(config));
    const props = {
      config,
      store
    };

    _cspaceLayout.Modal.setAppElement(mountNode);

    (0, _reactDom.render)( /*#__PURE__*/_react.default.createElement(_AppContainer.default, props), mountNode);
  }
};

exports.default = _default;