"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/es.promise.js");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.UiUI = void 0;
function _defineProperty2() {
  const data = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
  _defineProperty2 = function () {
    return data;
  };
  return data;
}
function _graphql() {
  const data = require("@teambit/graphql");
  _graphql = function () {
    return data;
  };
  return data;
}
function _harmony() {
  const data = require("@teambit/harmony");
  _harmony = function () {
    return data;
  };
  return data;
}
function _reactRouter() {
  const data = require("@teambit/react-router");
  _reactRouter = function () {
    return data;
  };
  return data;
}
function _reactRendering() {
  const data = require("@teambit/react.rendering.ssr");
  _reactRendering = function () {
    return data;
  };
  return data;
}
function _uiFoundationUiHooks() {
  const data = require("@teambit/ui-foundation.ui.hooks.use-user-agent");
  _uiFoundationUiHooks = function () {
    return data;
  };
  return data;
}
function _react() {
  const data = _interopRequireDefault(require("react"));
  _react = function () {
    return data;
  };
  return data;
}
function _ui() {
  const data = require("./ui.aspect");
  _ui = function () {
    return data;
  };
  return data;
}
function _clientContext() {
  const data = require("./ui/client-context");
  _clientContext = function () {
    return data;
  };
  return data;
}
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2().default)(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
/**
 * extension
 */
class UiUI {
  constructor(
  /**
   * react-router extension.
   */
  router,
  /**
   * ui root registry.
   */
  uiRootSlot, /** slot for overlay ui elements */
  hudSlot, /** hooks into the ssr render process */
  renderPluginsSlot) {
    this.router = router;
    this.uiRootSlot = uiRootSlot;
    this.hudSlot = hudSlot;
    this.renderPluginsSlot = renderPluginsSlot;
    /** adds elements to the Heads Up Display */
    (0, _defineProperty2().default)(this, "registerHudItem", element => {
      this.hudSlot.register(element);
    });
  }

  /** render and rehydrate client-side */
  async render(rootExtension) {
    const rootFactory = this.getRoot(rootExtension);
    if (!rootFactory) throw new Error(`root: ${rootExtension} was not found`);
    const uiRoot = rootFactory();
    const routes = this.router.renderRoutes(uiRoot.routes);
    const hudItems = this.hudSlot.values();
    const lifecyclePlugins = this.getLifecyclePlugins();
    const reactSsr = new (_reactRendering().BrowserRenderer)(lifecyclePlugins);
    await reactSsr.render( /*#__PURE__*/_react().default.createElement(_clientContext().ClientContext, null, hudItems, routes));
  }

  /** render dehydrated server-side */
  async renderSsr(rootExtension, ssrContent) {
    const rootFactory = this.getRoot(rootExtension);
    if (!rootFactory) throw new Error(`root: ${rootExtension} was not found`);
    const uiRoot = rootFactory();
    const routes = this.router.renderRoutes(uiRoot.routes);
    const hudItems = this.hudSlot.values();
    const lifecyclePlugins = this.getLifecyclePlugins();
    const reactSsr = new (_reactRendering().ServerRenderer)(lifecyclePlugins);
    const fullHtml = await reactSsr.render( /*#__PURE__*/_react().default.createElement(_clientContext().ClientContext, null, /*#__PURE__*/_react().default.createElement(_uiFoundationUiHooks().SSRBrowserProvider, {
      value: ssrContent.browser
    }, hudItems, routes)), ssrContent);
    return fullHtml;
  }
  /**
   * adds global context at the ui root
   * @deprecated replace with `.registerRenderHooks({ reactContext })`.
   */
  registerContext(context) {
    this.renderPluginsSlot.register({
      reactContext: context
    });
  }
  registerRoot(uiRoot) {
    return this.uiRootSlot.register(uiRoot);
  }
  registerRenderHooks(plugin) {
    return this.renderPluginsSlot.register(plugin);
  }
  getLifecyclePlugins() {
    const lifecyclePlugins = this.renderPluginsSlot.toArray().map(([key, plugin]) => {
      if (plugin.key) return plugin;

      // for backward compatibility
      return _objectSpread(_objectSpread({}, plugin), {}, {
        key
      });
    });

    // react-router should register its plugin, when we can reverse it's dependency to depend on Ui
    lifecyclePlugins.unshift(this.router.renderPlugin);
    return lifecyclePlugins;
  }
  getRoot(rootExtension) {
    return this.uiRootSlot.get(rootExtension);
  }
  static async provider([GraphqlUi, router], config, [uiRootSlot, hudSlot, renderLifecycleSlot]) {
    const uiUi = new UiUI(router, uiRootSlot, hudSlot, renderLifecycleSlot);
    if (GraphqlUi) uiUi.registerRenderHooks(GraphqlUi.renderPlugins);
    return uiUi;
  }
}
exports.UiUI = UiUI;
(0, _defineProperty2().default)(UiUI, "slots", [_harmony().Slot.withType(), _harmony().Slot.withType(), _harmony().Slot.withType()]);
(0, _defineProperty2().default)(UiUI, "dependencies", [_graphql().GraphqlAspect, _reactRouter().ReactRouterAspect]);
(0, _defineProperty2().default)(UiUI, "runtime", _ui().UIRuntime);
_ui().UIAspect.addRuntime(UiUI);

//# sourceMappingURL=ui.ui.runtime.js.map