import _Drawer from "@mui/material/Drawer";
/*!
 * 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 styled from "@emotion/styled";
import { memo, useCallback, useEffect, useMemo, useRef, useState } from "react";
import { Button } from "./Buttons/Button.js";
import { FullScreenOverlay } from "./FullScreenOverlay.js";
import { useTranslation } from "./i18n.generated/i18n.js";
import { CloseIcon } from "./icons.generated/index.js";
import { useOdysseyDesignTokens } from "./OdysseyDesignTokensContext.js";
import { Heading5 } from "./Typography.js";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
export const variantValues = ["temporary", "persistent"];
const DrawerHeader = styled("div", {
  shouldForwardProp: prop => prop !== "odysseyDesignTokens" && prop !== "hasDividers"
})`
  grid-area: header;
  position: sticky;
  top: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 0;
  padding: ${({
  odysseyDesignTokens
}) => odysseyDesignTokens.Spacing4}
    ${({
  odysseyDesignTokens
}) => odysseyDesignTokens.Spacing5};
  color: ${({
  odysseyDesignTokens
}) => odysseyDesignTokens.HueNeutral900};
  background-color: ${({
  odysseyDesignTokens
}) => odysseyDesignTokens.HueNeutralWhite};
  border-bottom: ${({
  hasDividers,
  odysseyDesignTokens
}) => hasDividers ? `1px solid ${odysseyDesignTokens.HueNeutral200}` : "none"};
`;
const DrawerContentWrapper = styled("div", {
  shouldForwardProp: prop => prop !== "odysseyDesignTokens"
})({
  display: "grid",
  gridTemplateAreas: `
    "header"
    "content"
    "footer"
  `,
  gridTemplateRows: "auto 1fr auto",
  overflow: "hidden"
});
const DrawerContent = styled("div", {
  shouldForwardProp: prop => prop !== "odysseyDesignTokens" && prop !== "hasDividers"
})`
  grid-area: content;
  overflow-y: auto;
  padding: ${({
  hasDividers,
  odysseyDesignTokens
}) => hasDividers ? `${odysseyDesignTokens.Spacing5}` : `0 ${odysseyDesignTokens.Spacing5}`};
`;
const DrawerFooter = styled("div", {
  shouldForwardProp: prop => prop !== "odysseyDesignTokens" && prop !== "hasDividers"
})`
  grid-area: footer;
  position: sticky;
  bottom: 0;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  align-content: center;
  padding: ${({
  odysseyDesignTokens
}) => odysseyDesignTokens.Spacing4};
  border-top: ${({
  hasDividers,
  odysseyDesignTokens
}) => hasDividers ? `1px solid ${odysseyDesignTokens.HueNeutral200}` : "none"};
  background-color: ${({
  odysseyDesignTokens
}) => odysseyDesignTokens.HueNeutralWhite};
`;
const Drawer = ({
  ariaLabel,
  children,
  hasDividers: hasDividersProp = false,
  isOpen,
  onClose,
  primaryCallToActionComponent,
  secondaryCallToActionComponent,
  showDividers = false,
  tertiaryCallToActionComponent,
  testId,
  title,
  translate,
  variant = "temporary"
}) => {
  const [isContentScrollable, setIsContentScrollable] = useState(false);
  const drawerContentRef = useRef(null);
  const odysseyDesignTokens = useOdysseyDesignTokens();
  const hasDividers = hasDividersProp || showDividers;
  const {
    i18n
  } = useTranslation();
  const anchorDirection = i18n.dir() === "rtl" ? "left" : "right";
  useEffect(() => {
    let frameId;
    const handleContentScroll = () => {
      const drawerContentElement = drawerContentRef.current;
      if (drawerContentElement) {
        cancelAnimationFrame(frameId);
        setIsContentScrollable(drawerContentElement.scrollHeight > drawerContentElement.clientHeight);
      }
      frameId = requestAnimationFrame(handleContentScroll);
    };
    if (isOpen) {
      frameId = requestAnimationFrame(handleContentScroll);
    }
    return () => {
      cancelAnimationFrame(frameId);
    };
  }, [isOpen]);
  const dividersVisible = useMemo(() => {
    return hasDividers || isContentScrollable;
  }, [hasDividers, isContentScrollable]);
  const hasFooter = useMemo(() => primaryCallToActionComponent || secondaryCallToActionComponent || tertiaryCallToActionComponent, [primaryCallToActionComponent, secondaryCallToActionComponent, tertiaryCallToActionComponent]);
  const handleCloseButtonClick = useCallback(event => onClose(event, "closeButtonClick"), [onClose]);
  const muiDrawerComponent = useMemo(() => _jsx(_Drawer, {
    anchor: anchorDirection,
    "data-se": testId,
    onClose: onClose,
    open: isOpen,
    sx: {
      ...(variant === "persistent" && {
        "& .MuiDrawer-paper": {
          transition: "none"
        }
      })
    },
    variant: variant,
    children: _jsxs(DrawerContentWrapper, {
      odysseyDesignTokens: odysseyDesignTokens,
      children: [_jsxs(DrawerHeader, {
        hasDividers: dividersVisible,
        odysseyDesignTokens: odysseyDesignTokens,
        translate: translate,
        children: [_jsx(Heading5, {
          children: title
        }), _jsx(Button, {
          ariaLabel: ariaLabel,
          onClick: handleCloseButtonClick,
          size: "small",
          startIcon: _jsx(CloseIcon, {}),
          variant: "floating"
        })]
      }), _jsx(DrawerContent, {
        ...(isContentScrollable && {
          tabIndex: 0
        }),
        hasDividers: dividersVisible,
        odysseyDesignTokens: odysseyDesignTokens,
        ref: drawerContentRef,
        children: children
      }), hasFooter && _jsxs(DrawerFooter, {
        hasDividers: dividersVisible,
        odysseyDesignTokens: odysseyDesignTokens,
        children: [tertiaryCallToActionComponent, secondaryCallToActionComponent, primaryCallToActionComponent]
      })]
    })
  }), [anchorDirection, ariaLabel, children, dividersVisible, handleCloseButtonClick, hasFooter, isContentScrollable, isOpen, odysseyDesignTokens, onClose, primaryCallToActionComponent, secondaryCallToActionComponent, tertiaryCallToActionComponent, testId, title, translate, variant]);
  return variant === "temporary" ? _jsx(FullScreenOverlay, {
    children: muiDrawerComponent
  }) : muiDrawerComponent;
};
const MemoizedDrawer = memo(Drawer);
MemoizedDrawer.displayName = "Drawer";
export { MemoizedDrawer as Drawer };
//# sourceMappingURL=Drawer.js.map