"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.UiUI = void 0;
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 _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
/**
 * 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 */
    _defineProperty(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;
_defineProperty(UiUI, "slots", [_harmony().Slot.withType(), _harmony().Slot.withType(), _harmony().Slot.withType()]);
_defineProperty(UiUI, "dependencies", [_graphql().GraphqlAspect, _reactRouter().ReactRouterAspect]);
_defineProperty(UiUI, "runtime", _ui().UIRuntime);
_ui().UIAspect.addRuntime(UiUI);

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