"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.variantValues = exports.Drawer = void 0;
var _Drawer2 = _interopRequireDefault(require("@mui/material/Drawer"));
var _styled = _interopRequireDefault(require("@emotion/styled"));
var _react = require("react");
var _Button = require("./Buttons/Button.cjs");
var _FullScreenOverlay = require("./FullScreenOverlay.cjs");
var _i18n = require("./i18n.generated/i18n.cjs");
var _index = require("./icons.generated/index.cjs");
var _OdysseyDesignTokensContext = require("./OdysseyDesignTokensContext.cjs");
var _Typography = require("./Typography.cjs");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/*!
 * 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.
 */

const variantValues = exports.variantValues = ["temporary", "persistent"];
const DrawerHeader = (0, _styled.default)("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 = (0, _styled.default)("div", {
  shouldForwardProp: prop => prop !== "odysseyDesignTokens"
})({
  display: "grid",
  gridTemplateAreas: `
    "header"
    "content"
    "footer"
  `,
  gridTemplateRows: "auto 1fr auto",
  overflow: "hidden"
});
const DrawerContent = (0, _styled.default)("div", {
  shouldForwardProp: prop => prop !== "odysseyDesignTokens" && prop !== "hasDividers"
})`
  grid-area: content;
  overflow-y: auto;
  padding: ${({
  hasDividers,
  odysseyDesignTokens
}) => hasDividers ? `${odysseyDesignTokens.Spacing5}` : `0 ${odysseyDesignTokens.Spacing5}`};
`;
const DrawerFooter = (0, _styled.default)("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] = (0, _react.useState)(false);
  const drawerContentRef = (0, _react.useRef)(null);
  const odysseyDesignTokens = (0, _OdysseyDesignTokensContext.useOdysseyDesignTokens)();
  const hasDividers = hasDividersProp || showDividers;
  const {
    i18n
  } = (0, _i18n.useTranslation)();
  const anchorDirection = i18n.dir() === "rtl" ? "left" : "right";
  (0, _react.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 = (0, _react.useMemo)(() => {
    return hasDividers || isContentScrollable;
  }, [hasDividers, isContentScrollable]);
  const hasFooter = (0, _react.useMemo)(() => primaryCallToActionComponent || secondaryCallToActionComponent || tertiaryCallToActionComponent, [primaryCallToActionComponent, secondaryCallToActionComponent, tertiaryCallToActionComponent]);
  const handleCloseButtonClick = (0, _react.useCallback)(event => onClose(event, "closeButtonClick"), [onClose]);
  const muiDrawerComponent = (0, _react.useMemo)(() => (0, _jsxRuntime.jsx)(_Drawer2.default, {
    anchor: anchorDirection,
    "data-se": testId,
    onClose: onClose,
    open: isOpen,
    sx: {
      ...(variant === "persistent" && {
        "& .MuiDrawer-paper": {
          transition: "none"
        }
      })
    },
    variant: variant,
    children: (0, _jsxRuntime.jsxs)(DrawerContentWrapper, {
      odysseyDesignTokens: odysseyDesignTokens,
      children: [(0, _jsxRuntime.jsxs)(DrawerHeader, {
        hasDividers: dividersVisible,
        odysseyDesignTokens: odysseyDesignTokens,
        translate: translate,
        children: [(0, _jsxRuntime.jsx)(_Typography.Heading5, {
          children: title
        }), (0, _jsxRuntime.jsx)(_Button.Button, {
          ariaLabel: ariaLabel,
          onClick: handleCloseButtonClick,
          size: "small",
          startIcon: (0, _jsxRuntime.jsx)(_index.CloseIcon, {}),
          variant: "floating"
        })]
      }), (0, _jsxRuntime.jsx)(DrawerContent, {
        ...(isContentScrollable && {
          tabIndex: 0
        }),
        hasDividers: dividersVisible,
        odysseyDesignTokens: odysseyDesignTokens,
        ref: drawerContentRef,
        children: children
      }), hasFooter && (0, _jsxRuntime.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" ? (0, _jsxRuntime.jsx)(_FullScreenOverlay.FullScreenOverlay, {
    children: muiDrawerComponent
  }) : muiDrawerComponent;
};
const MemoizedDrawer = exports.Drawer = (0, _react.memo)(Drawer);
MemoizedDrawer.displayName = "Drawer";
//# sourceMappingURL=Drawer.cjs.map