"use strict";

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

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

var _server = require("react-dom/server");

var _reactRouter = require("@marionebl/react-router");

var _reactRedux = require("react-redux");

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

var _components = require("@patternplate/components");

var _routes = _interopRequireDefault(require("./routes"));

var _store = _interopRequireDefault(require("./store"));

var _item = _interopRequireDefault(require("./selectors/item"));

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

function _default(location, data) {
  const sheet = new _components.ServerStyleSheet();
  const memoryHistory = (0, _reactRouter.createMemoryHistory)(location);
  const store = (0, _store.default)(memoryHistory, data);
  const history = (0, _reactRouterRedux.syncHistoryWithStore)(memoryHistory, store);
  return new Promise((resolve, reject) => {
    (0, _reactRouter.match)({
      history,
      routes: (0, _routes.default)(store),
      location
    }, (error, redirect, props) => {
      if (error) {
        return reject(error);
      }

      const context = sheet.collectStyles(_react.default.createElement(_reactRedux.Provider, {
        store: store
      }, _react.default.createElement(_reactRouter.RouterContext, props)));
      const state = store.getState();
      const item = (0, _item.default)(state);
      const html = (0, _server.renderToString)(context);
      const css = sheet.getStyleElement();
      resolve({
        html,
        css,
        status: typeof item !== 'undefined' ? 200 : 404
      });
    });
  });
}
//# sourceMappingURL=server.js.map