/*!
 * Copyright (c) 2024-present, Okta, Inc. and/or its affiliates. All rights reserved.
 * The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
 *
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and limitations under the License.
 */

import { ErrorBoundary } from "react-error-boundary";
import { bufferLatest } from "../tools/bufferLatest.js";
import { createMessageBus } from "../tools/createMessageBus.js";
import { renderReactInWebComponent } from "../web-component/renderReactInWebComponent.js";
import { UiShell } from "./UiShell.js";
import { uiShellDataAttribute } from "./useHasUiShell.js";
import { jsx as _jsx } from "react/jsx-runtime";
export const optionalComponentSlotNames = {
  banners: "banners",
  rightSideMenu: "right-side-menu",
  sideNavFooter: "side-nav-footer",
  topNavLeftSide: "top-nav-left-side",
  topNavRightSide: "top-nav-right-side"
};
export const renderUiShell = ({
  appBackgroundColor,
  appBackgroundContrastMode,
  appElement: providedAppElement,
  appElementScrollingMode,
  breakpointConfig,
  hasStandardAppContentPadding,
  initialVisibleSections,
  onError = console.error,
  onRender,
  parentElement,
  sideNavBackgroundColor,
  sideNavBackgroundContrastColor,
  topNavBackgroundColor,
  nonce = window.cspNonce
}) => {
  const appElement = providedAppElement || document.createElement("div");
  parentElement.setAttribute(uiShellDataAttribute, "");
  const {
    publish: publishPropChanges,
    subscribe: subscribeToPropChanges
  } = createMessageBus();
  const {
    publish: publishSubscriptionCreated,
    subscribe: subscribeToReactAppSubscribed
  } = createMessageBus();
  const publishAfterReactAppReadyForProps = bufferLatest({
    publish: publishPropChanges,
    subscribe: subscribeToReactAppSubscribed
  });
  const {
    publish: closeRightSideMenu,
    subscribe: subscribeToCloseRightSideMenu
  } = createMessageBus();
  const {
    publish: closeSideNavMenu,
    subscribe: subscribeToCloseSideNavMenu
  } = createMessageBus();
  const slottedElements = Object.fromEntries(Object.entries(optionalComponentSlotNames).map(([optionalComponentKey, slotName]) => {
    const element = document.createElement("div");
    element.setAttribute("slot", slotName);
    return [optionalComponentKey, element];
  }));
  const webComponentChildren = Object.values(slottedElements);
  const uiShellElement = renderReactInWebComponent({
    getReactComponent: reactRootElements => _jsx(ErrorBoundary, {
      fallback: _jsx("div", {
        "data-error": true
      }),
      onError: onError,
      children: _jsx(UiShell, {
        appBackgroundColor: appBackgroundColor,
        appBackgroundContrastMode: appBackgroundContrastMode,
        appElement: appElement,
        appElementScrollingMode: appElementScrollingMode,
        breakpointConfig: breakpointConfig,
        closeSideNavMenu: closeSideNavMenu,
        hasStandardAppContentPadding: hasStandardAppContentPadding,
        initialVisibleSections: initialVisibleSections,
        onError: onError,
        onSubscriptionCreated: publishSubscriptionCreated,
        optionalComponents: Object.fromEntries(Object.entries(optionalComponentSlotNames).map(([optionalComponentKey, slotName]) => [optionalComponentKey, _jsx("slot", {
          name: slotName
        })])),
        sideNavBackgroundColor: sideNavBackgroundColor,
        sideNavBackgroundContrastColor: sideNavBackgroundContrastColor,
        subscribeToCloseRightSideMenu: subscribeToCloseRightSideMenu,
        subscribeToCloseSideNavMenu: subscribeToCloseSideNavMenu,
        subscribeToPropChanges: subscribeToPropChanges,
        topNavBackgroundColor: topNavBackgroundColor,
        uiShellAppElement: reactRootElements.appRootElement,
        uiShellStylesElement: reactRootElements.stylesRootElement
      })
    }),
    webComponentChildren,
    webComponentParentElement: parentElement,
    nonce
  });
  const unsubscribeFromUnifiedUiShellRendered = subscribeToReactAppSubscribed(() => {
    unsubscribeFromUnifiedUiShellRendered();
    onRender?.({
      appElement,
      closeRightSideMenu,
      closeSideNavMenu,
      setComponentProps: publishAfterReactAppReadyForProps,
      slottedElements,
      uiShellElement
    });
  });
  parentElement.appendChild(appElement);
  return {
    appElement,
    closeRightSideMenu,
    closeSideNavMenu,
    setComponentProps: publishAfterReactAppReadyForProps,
    slottedElements,
    uiShellElement
  };
};
//# sourceMappingURL=renderUiShell.js.map