import _Dialog from "@mui/material/Dialog";
import _DialogActions from "@mui/material/DialogActions";
import _DialogContent from "@mui/material/DialogContent";
import _DialogTitle from "@mui/material/DialogTitle";
import _DialogContentText from "@mui/material/DialogContentText";
/*!
 * 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 { cloneElement, 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 { useUniqueId } from "./useUniqueId.js";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const Dialog = ({
  primaryCallToActionComponent,
  secondaryCallToActionComponent,
  tertiaryCallToActionComponent,
  callToActionFirstComponent,
  callToActionSecondComponent,
  callToActionLastComponent,
  children,
  isOpen,
  onClose,
  testId,
  title,
  translate,
  variant = "default"
}) => {
  const {
    t
  } = useTranslation();
  const [isContentScrollable, setIsContentScrollable] = useState(false);
  const dialogContentRef = useRef(null);
  const dialogTitleId = useUniqueId();
  const dialogLabelId = useUniqueId();
  useEffect(() => {
    let frameId;
    const handleContentScroll = () => {
      const dialogContentElement = dialogContentRef.current;
      if (dialogContentElement) {
        cancelAnimationFrame(frameId);
        setIsContentScrollable(dialogContentElement.scrollHeight > dialogContentElement.clientHeight);
      }
      frameId = requestAnimationFrame(handleContentScroll);
    };
    if (isOpen) {
      frameId = requestAnimationFrame(handleContentScroll);
    }
    return () => {
      cancelAnimationFrame(frameId);
    };
  }, [isOpen]);
  const content = typeof children === "string" ? _jsx(_DialogContentText, {
    children: children
  }) : children;
  const actionButtons = [tertiaryCallToActionComponent || callToActionLastComponent, secondaryCallToActionComponent || callToActionSecondComponent, primaryCallToActionComponent || callToActionFirstComponent].filter(Boolean);
  const handleCloseButtonClick = useCallback(event => onClose(event, "closeButtonClick"), [onClose]);
  const slotProps = useMemo(() => ({
    backdrop: {
      variant: variant
    }
  }), [variant]);
  return _jsx(FullScreenOverlay, {
    overlayType: "dialog",
    children: _jsxs(_Dialog, {
      "aria-labelledby": dialogLabelId,
      "data-se": testId,
      onClose: onClose,
      open: isOpen,
      slotProps: slotProps,
      children: [_jsxs(_DialogTitle, {
        id: dialogTitleId,
        translate: translate,
        children: [_jsx("span", {
          id: dialogLabelId,
          children: title
        }), _jsx(Button, {
          ariaLabel: t("close.text"),
          onClick: handleCloseButtonClick,
          size: "small",
          startIcon: _jsx(CloseIcon, {}),
          variant: "floating"
        })]
      }), _jsx(_DialogContent, {
        ...(isContentScrollable && {
          tabIndex: 0
        }),
        dividers: isContentScrollable,
        ref: dialogContentRef,
        children: content
      }), actionButtons.length > 0 && _jsx(_DialogActions, {
        children: actionButtons.map((actionButton, index) => actionButton ? cloneElement(actionButton, {
          key: index
        }) : null)
      })]
    })
  });
};
const MemoizedDialog = memo(Dialog);
MemoizedDialog.displayName = "Dialog";
export { MemoizedDialog as Dialog };
//# sourceMappingURL=Dialog.js.map