"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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

var React = _interopRequireWildcard(require("react"));

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

var _styledComponents = require("styled-components");

var _reactRouter = require("react-router");

var _reactHelmet = require("react-helmet");

var _intl = require("@reactizer/intl");

var _Root = _interopRequireDefault(require("../../app/Root"));

var _config = require("../config");

var _data = require("../data");

var _Html = _interopRequireDefault(require("./Html"));

function markup({
  url,
  context,
  themeId,
  localeId
}) {
  const theme = _data.themes[themeId];
  const locale = _data.locales[localeId];
  const sheet = new _styledComponents.ServerStyleSheet();
  const root = ReactDOM.renderToString(_config.extractor.collectChunks(sheet.collectStyles(React.createElement(_styledComponents.ThemeProvider, {
    theme: theme
  }, React.createElement(_intl.IntlProvider, {
    locale: locale,
    onChange: () => Promise.resolve(locale)
  }, React.createElement(_reactRouter.StaticRouter, {
    context: context,
    location: url,
    basename: process.env.BASENAME
  }, React.createElement(_Root.default, null))))))); // Redirect

  if (context.url) {
    return Promise.resolve(null);
  }

  const helmet = _reactHelmet.Helmet.renderStatic();

  return Promise.resolve(ReactDOM.renderToStaticMarkup(React.createElement(_Html.default, {
    root: root,
    helmet: helmet,
    styles: sheet.getStyleElement(),
    preloadable: _config.extractor.getLinkElements(),
    loadable: _config.extractor.getScriptElements(),
    themeId: themeId,
    localeId: localeId
  })));
}

var _default = markup;
exports.default = _default;