/*!
 * Copyright (c) 2022-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 styled from "@emotion/styled";
import { memo, useCallback, useEffect, useRef, useState } from "react";
import { ErrorBoundary } from "react-error-boundary";
import { Button } from "../Buttons/Button.js";
import { hexToRgb } from "../hexToRgb.js";
import { CloseIcon } from "../icons.generated/Close.js";
import { MoreIcon } from "../icons.generated/More.js";
import { useOdysseyDesignTokens } from "../OdysseyDesignTokensContext.js";
import { InnerAppContainer } from "./InnerAppContainer.js";
import { SideNav } from "./SideNav/SideNav.js";
import { HamburgerMenuIcon } from "./TopNav/HamburgerMenuIcon.js";
import { UiShellLogo } from "./UiShellLogo.js";
import { useUiShellContext } from "./UiShellProvider.js";
import { emptySideNavItems, SIDE_NAV_WIDTH, TOP_NAV_HEIGHT, UI_SHELL_BASE_Z_INDEX, UI_SHELL_OVERLAY_Z_INDEX } from "./uiShellSharedConstants.js";
import { useMatchAppElementToUiShellAppArea } from "./useMatchAppElementToUiShellAppArea.js";
import { useScrollState } from "./useScrollState.js";
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
const StyledAppContentArea = styled("div")({
  gridArea: "app-content",
  position: "relative",
  height: "100%",
  width: "100%"
});
const StyledAppContainer = styled("div", {
  shouldForwardProp: prop => prop !== "appBackgroundColor" && prop !== "odysseyDesignTokens"
})(({
  appBackgroundColor
}) => ({
  backgroundColor: appBackgroundColor,
  height: "100%",
  overflow: "hidden",
  width: "100%"
}));
const StyledBannersContainer = styled("div")({
  gridArea: "banners",
  zIndex: UI_SHELL_BASE_Z_INDEX
});
const StyledLeftSideContainer = styled("div", {
  shouldForwardProp: prop => prop !== "isOpen"
})(({
  isOpen
}) => ({
  display: isOpen ? "block" : "none",
  height: "100%",
  position: "absolute",
  zIndex: UI_SHELL_OVERLAY_Z_INDEX
}));
const StyledRightSideContainer = styled("div", {
  shouldForwardProp: prop => prop !== "isOpen" && prop !== "odysseyDesignTokens"
})(({
  isOpen,
  odysseyDesignTokens
}) => ({
  backgroundColor: odysseyDesignTokens.HueNeutralWhite,
  display: isOpen ? "block" : "none",
  height: "100%",
  maxWidth: SIDE_NAV_WIDTH,
  overflowY: "auto",
  position: "absolute",
  right: 0,
  width: SIDE_NAV_WIDTH,
  zIndex: UI_SHELL_OVERLAY_Z_INDEX
}));
const StyledMenuLogo = styled("div", {
  shouldForwardProp: prop => prop !== "odysseyDesignTokens"
})(({
  odysseyDesignTokens
}) => ({
  alignItems: "center",
  display: "inline-flex",
  gap: odysseyDesignTokens.Spacing3
}));
const StyledLogoContainer = styled("div", {
  shouldForwardProp: prop => prop !== "odysseyDesignTokens"
})(({
  odysseyDesignTokens
}) => ({
  height: TOP_NAV_HEIGHT,
  paddingBlock: odysseyDesignTokens.Spacing4,
  "svg, img": {
    maxHeight: "100%",
    width: "auto",
    maxWidth: "100%"
  }
}));
const StyledPageOverlay = styled("div", {
  shouldForwardProp: prop => prop !== "odysseyDesignTokens"
})(({
  odysseyDesignTokens
}) => ({
  backgroundColor: hexToRgb(odysseyDesignTokens.HueNeutral900).asFormattedString.replace(/rgb\((.+)\)$/, "rgba($1, 0.26)"),
  gridArea: "app-content",
  height: "100vh",
  left: 0,
  position: "absolute",
  top: 0,
  width: "100vw",
  zIndex: UI_SHELL_BASE_Z_INDEX
}));
const StyledSideNavContainer = styled("div")({
  height: "100%"
});
const StyledUiShellContainer = styled("div", {
  shouldForwardProp: prop => prop !== "odysseyDesignTokens"
})(({
  odysseyDesignTokens
}) => ({
  backgroundColor: odysseyDesignTokens.HueNeutral50,
  display: "grid",
  gridGap: 0,
  gridTemplateAreas: `
    "banners"
    "top-nav"
    "app-content"
  `,
  gridTemplateColumns: "1fr",
  gridTemplateRows: "auto auto 1fr",
  height: "100vh",
  overflow: "hidden",
  width: "100vw"
}));
const StyledTopNav = styled("div", {
  shouldForwardProp: prop => prop !== "odysseyDesignTokens" && prop !== "isContentScrolled" && prop !== "topNavBackgroundColor"
})(({
  odysseyDesignTokens,
  isContentScrolled,
  topNavBackgroundColor
}) => ({
  alignItems: "stretch",
  backgroundColor: odysseyDesignTokens.HueNeutralWhite || topNavBackgroundColor,
  boxShadow: isContentScrolled ? odysseyDesignTokens.DepthMedium : undefined,
  clipPath: "inset(0 0 -100vh 0)",
  display: "flex",
  flexDirection: "column",
  gridArea: "top-nav",
  height: "100%",
  justifyContent: "center",
  position: "relative",
  transition: `box-shadow ${odysseyDesignTokens.TransitionDurationMain} ${odysseyDesignTokens.TransitionTimingMain}`,
  zIndex: UI_SHELL_BASE_Z_INDEX
}));
const StyledTopNavMenu = styled("div", {
  shouldForwardProp: prop => prop !== "odysseyDesignTokens" && prop !== "topNavBackgroundColor"
})(({
  odysseyDesignTokens,
  topNavBackgroundColor
}) => ({
  alignItems: "center",
  display: "flex",
  gap: odysseyDesignTokens.Spacing3,
  height: "100%",
  justifyContent: "space-between",
  maxHeight: TOP_NAV_HEIGHT,
  minHeight: TOP_NAV_HEIGHT,
  paddingBlock: odysseyDesignTokens.Spacing2,
  paddingInline: odysseyDesignTokens.Spacing3,
  transition: `box-shadow ${odysseyDesignTokens.TransitionDurationMain} ${odysseyDesignTokens.TransitionTimingMain}`,
  ...(topNavBackgroundColor === odysseyDesignTokens.HueNeutralWhite && {
    borderBottomColor: odysseyDesignTokens.HueNeutral100,
    borderBottomStyle: odysseyDesignTokens.BorderStyleMain,
    borderBottomWidth: odysseyDesignTokens.BorderWidthMain
  })
}));
const StyledTopNavSearch = styled("div", {
  shouldForwardProp: prop => prop !== "odysseyDesignTokens"
})(({
  odysseyDesignTokens
}) => ({
  paddingBlock: odysseyDesignTokens.Spacing2,
  paddingInline: odysseyDesignTokens.Spacing3
}));
const NarrowUiShellContent = ({
  appElement,
  appElementScrollingMode,
  hasSideNavProps,
  hasStandardAppContentPadding = true,
  initialVisibleSections = ["TopNav", "SideNav", "AppSwitcher"],
  logoProps,
  onError = console.error,
  optionalComponents,
  sideNavProps,
  topNavProps
}) => {
  const odysseyDesignTokens = useOdysseyDesignTokens();
  const uiShellContext = useUiShellContext();
  const {
    isContentScrolled
  } = useScrollState(appElement);
  const sideNavContainerRef = useRef(null);
  const topNavContainerRef = useRef(null);
  const uiShellAppAreaRef = useRef(null);
  const [isLeftSideMenuOpen, setIsLeftSideMenuOpen] = useState(false);
  const [isRightSideMenuOpen, setIsRightSideMenuOpen] = useState(false);
  const closeSideMenus = useCallback(() => {
    setIsLeftSideMenuOpen(false);
    setIsRightSideMenuOpen(false);
  }, []);
  useEffect(() => {
    const unsubscribeFromCloseRightSideMenu = uiShellContext?.subscribeToCloseRightSideMenu(() => {
      closeSideMenus();
    });
    const unsubscribeFromSideNavItemClicked = uiShellContext?.subscribeToCloseSideNavMenu(() => {
      closeSideMenus();
    });
    return () => {
      unsubscribeFromCloseRightSideMenu?.();
      unsubscribeFromSideNavItemClicked?.();
    };
  }, [closeSideMenus, uiShellContext]);
  const toggleLeftSideMenu = useCallback(() => {
    setIsRightSideMenuOpen(false);
    setIsLeftSideMenuOpen(isLeftSideMenuOpen => !isLeftSideMenuOpen);
  }, []);
  const toggleRightSideMenu = useCallback(() => {
    setIsLeftSideMenuOpen(false);
    setIsRightSideMenuOpen(isRightSideMenuOpen => !isRightSideMenuOpen);
  }, []);
  const {
    parentContainerRef
  } = useMatchAppElementToUiShellAppArea({
    appElement,
    appElementScrollingMode,
    paddingMode: hasStandardAppContentPadding ? "compact" : "none",
    uiShellAppAreaRef,
    uiShellResizableRefs: [sideNavContainerRef, topNavContainerRef, uiShellAppAreaRef]
  });
  return _jsxs(_Fragment, {
    children: [(isLeftSideMenuOpen || isRightSideMenuOpen) && _jsx(StyledPageOverlay, {
      odysseyDesignTokens: odysseyDesignTokens,
      onClick: closeSideMenus
    }), _jsxs(StyledUiShellContainer, {
      odysseyDesignTokens: odysseyDesignTokens,
      ref: parentContainerRef,
      children: [_jsx(StyledBannersContainer, {
        children: optionalComponents?.banners
      }), (initialVisibleSections?.includes("TopNav") || topNavProps) && _jsx(ErrorBoundary, {
        fallback: null,
        onError: onError,
        children: _jsxs(StyledTopNav, {
          isContentScrolled: isContentScrolled,
          odysseyDesignTokens: odysseyDesignTokens,
          topNavBackgroundColor: uiShellContext?.topNavBackgroundColor,
          children: [_jsxs(StyledTopNavMenu, {
            odysseyDesignTokens: odysseyDesignTokens,
            topNavBackgroundColor: uiShellContext?.sideNavBackgroundColor,
            children: [_jsxs(StyledMenuLogo, {
              odysseyDesignTokens: odysseyDesignTokens,
              children: [hasSideNavProps && (sideNavProps?.isCollapsible || !sideNavProps?.isCollapsed) && _jsx(Button, {
                onClick: toggleLeftSideMenu,
                startIcon: _jsx(HamburgerMenuIcon, {}),
                testId: "sidenav-menu--icon",
                variant: "floating"
              }), _jsx(StyledLogoContainer, {
                odysseyDesignTokens: odysseyDesignTokens,
                children: _jsx(UiShellLogo, {
                  ...(sideNavProps?.logoProps || logoProps)
                })
              })]
            }), optionalComponents?.rightSideMenu && _jsx(Button, {
              onClick: toggleRightSideMenu,
              startIcon: isRightSideMenuOpen ? _jsx(CloseIcon, {}) : _jsx(MoreIcon, {}),
              testId: "userprofile-menu--icon",
              variant: "floating"
            })]
          }), optionalComponents?.topNavLeftSide && _jsx(StyledTopNavSearch, {
            odysseyDesignTokens: odysseyDesignTokens,
            children: optionalComponents?.topNavLeftSide
          })]
        })
      }), _jsxs(StyledAppContentArea, {
        children: [_jsxs(StyledLeftSideContainer, {
          isOpen: isLeftSideMenuOpen,
          children: [initialVisibleSections?.includes("SideNav") && !hasSideNavProps && _jsx(ErrorBoundary, {
            fallback: null,
            onError: onError,
            children: _jsx(SideNav, {
              isLoading: true,
              sideNavItems: emptySideNavItems
            })
          }), hasSideNavProps && sideNavProps && _jsx(ErrorBoundary, {
            fallback: null,
            onError: onError,
            children: _jsx(StyledSideNavContainer, {
              children: _jsx(SideNav, {
                ...{
                  ...sideNavProps,
                  appName: undefined,
                  logoProps: undefined
                },
                ...(sideNavProps.hasCustomFooter && optionalComponents?.sideNavFooter ? {
                  footerComponent: optionalComponents.sideNavFooter,
                  footerItems: undefined,
                  hasCustomFooter: sideNavProps.hasCustomFooter
                } : {
                  footerItems: sideNavProps.footerItems,
                  hasCustomFooter: false
                }),
                isCollapsed: false,
                isCollapsible: false,
                isObtrusive: true,
                onCollapse: closeSideMenus
              })
            })
          })]
        }), _jsx(StyledRightSideContainer, {
          isOpen: isRightSideMenuOpen,
          odysseyDesignTokens: odysseyDesignTokens,
          children: _jsx(StyledSideNavContainer, {
            children: optionalComponents?.rightSideMenu
          })
        }), _jsx(StyledAppContainer, {
          appBackgroundColor: uiShellContext?.appBackgroundColor,
          ref: uiShellAppAreaRef,
          tabIndex: 0,
          children: _jsx(InnerAppContainer, {
            isContentScrolled: isContentScrolled
          })
        })]
      })]
    })]
  });
};
const MemoizedNarrowUiShellContent = memo(NarrowUiShellContent);
MemoizedNarrowUiShellContent.displayName = "NarrowUiShellContent";
export { MemoizedNarrowUiShellContent as NarrowUiShellContent };
//# sourceMappingURL=NarrowUiShellContent.js.map