"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Component = Component;
function _react() {
  const data = _interopRequireWildcard(require("react"));
  _react = function () {
    return data;
  };
  return data;
}
function _lodash() {
  const data = _interopRequireDefault(require("lodash.flatten"));
  _lodash = function () {
    return data;
  };
  return data;
}
function _uiFoundationUiReactRouter() {
  const data = require("@teambit/ui-foundation.ui.react-router.slot-router");
  _uiFoundationUiReactRouter = function () {
    return data;
  };
  return data;
}
function _lodash2() {
  const data = require("lodash");
  _lodash2 = function () {
    return data;
  };
  return data;
}
function _context() {
  const data = require("./context");
  _context = function () {
    return data;
  };
  return data;
}
function _useComponent() {
  const data = require("./use-component");
  _useComponent = function () {
    return data;
  };
  return data;
}
function _useComponentFromLocation() {
  const data = require("./use-component-from-location");
  _useComponentFromLocation = function () {
    return data;
  };
  return data;
}
function _() {
  const data = require("..");
  _ = function () {
    return data;
  };
  return data;
}
function _componentModule() {
  const data = _interopRequireDefault(require("./component.module.scss"));
  _componentModule = function () {
    return data;
  };
  return data;
}
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
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); }
function getComponentIdStr(componentIdStr) {
  if ((0, _lodash2().isFunction)(componentIdStr)) return componentIdStr();
  return componentIdStr;
}

/**
 * main UI component of the Component extension.
 */
function Component({
  routeSlot,
  overriddenRoutes,
  containerSlot,
  host,
  onComponentChange,
  componentIdStr,
  useComponent,
  path,
  useComponentFilters
}) {
  const idFromLocation = (0, _useComponentFromLocation().useIdFromLocation)();
  const componentIdStrWithScopeFromLocation = (0, _useComponentFromLocation().useIdFromLocation)(undefined, true);
  const _componentIdStr = getComponentIdStr(componentIdStr);
  const componentId = _componentIdStr ? _().ComponentID.fromString(_componentIdStr) : undefined;
  const resolvedComponentIdStr = path || idFromLocation;
  const componentFiltersFromProps = useComponentFilters?.() || {};
  const useComponentOptions = {
    logFilters: _objectSpread(_objectSpread({}, componentFiltersFromProps), componentFiltersFromProps.loading ? {} : {
      log: _objectSpread({}, componentFiltersFromProps.log)
    }),
    customUseComponent: useComponent
  };
  const {
    component,
    componentDescriptor,
    error
  } = (0, _useComponent().useComponent)(host, componentId?.toString() || componentIdStrWithScopeFromLocation, useComponentOptions);

  // trigger onComponentChange when component changes
  (0, _react().useEffect)(() => onComponentChange?.(component), [component]);
  // cleanup when unmounting component
  (0, _react().useEffect)(() => () => onComponentChange?.(undefined), []);
  const pageItems = (0, _react().useMemo)(() => (0, _lodash().default)(containerSlot?.values()), [containerSlot]);
  const before = (0, _react().useMemo)(() => pageItems.filter(x => x.type === 'before').map(x => x.content), [pageItems]);
  const after = (0, _react().useMemo)(() => pageItems.filter(x => x.type === 'after').map(x => x.content), [pageItems]);
  if (error) return error?.renderError();
  if (!component) return /*#__PURE__*/_react().default.createElement("div", null);
  return /*#__PURE__*/_react().default.createElement(_context().ComponentDescriptorProvider, {
    componentDescriptor: componentDescriptor
  }, /*#__PURE__*/_react().default.createElement(_context().ComponentProvider, {
    component: component
  }, before, /*#__PURE__*/_react().default.createElement("div", {
    className: _componentModule().default.container
  }, routeSlot && /*#__PURE__*/_react().default.createElement(_uiFoundationUiReactRouter().SlotRouter, {
    parentPath: `${resolvedComponentIdStr}/*`,
    slot: routeSlot,
    routes: overriddenRoutes
  })), after));
}

//# sourceMappingURL=component.js.map