"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(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
/**
 * 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