"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Dialog = void 0;
var _Dialog2 = _interopRequireDefault(require("@mui/material/Dialog"));
var _DialogActions2 = _interopRequireDefault(require("@mui/material/DialogActions"));
var _DialogContent2 = _interopRequireDefault(require("@mui/material/DialogContent"));
var _DialogTitle2 = _interopRequireDefault(require("@mui/material/DialogTitle"));
var _DialogContentText2 = _interopRequireDefault(require("@mui/material/DialogContentText"));
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 _useUniqueId = require("./useUniqueId.cjs");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/*!
 * 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.
 */

const Dialog = ({
  primaryCallToActionComponent,
  secondaryCallToActionComponent,
  tertiaryCallToActionComponent,
  callToActionFirstComponent,
  callToActionSecondComponent,
  callToActionLastComponent,
  children,
  isOpen,
  onClose,
  testId,
  title,
  translate,
  variant = "default"
}) => {
  const {
    t
  } = (0, _i18n.useTranslation)();
  const [isContentScrollable, setIsContentScrollable] = (0, _react.useState)(false);
  const dialogContentRef = (0, _react.useRef)(null);
  const dialogTitleId = (0, _useUniqueId.useUniqueId)();
  const dialogLabelId = (0, _useUniqueId.useUniqueId)();
  (0, _react.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" ? (0, _jsxRuntime.jsx)(_DialogContentText2.default, {
    children: children
  }) : children;
  const actionButtons = [tertiaryCallToActionComponent || callToActionLastComponent, secondaryCallToActionComponent || callToActionSecondComponent, primaryCallToActionComponent || callToActionFirstComponent].filter(Boolean);
  const handleCloseButtonClick = (0, _react.useCallback)(event => onClose(event, "closeButtonClick"), [onClose]);
  const slotProps = (0, _react.useMemo)(() => ({
    backdrop: {
      variant: variant
    }
  }), [variant]);
  return (0, _jsxRuntime.jsx)(_FullScreenOverlay.FullScreenOverlay, {
    overlayType: "dialog",
    children: (0, _jsxRuntime.jsxs)(_Dialog2.default, {
      "aria-labelledby": dialogLabelId,
      "data-se": testId,
      onClose: onClose,
      open: isOpen,
      slotProps: slotProps,
      children: [(0, _jsxRuntime.jsxs)(_DialogTitle2.default, {
        id: dialogTitleId,
        translate: translate,
        children: [(0, _jsxRuntime.jsx)("span", {
          id: dialogLabelId,
          children: title
        }), (0, _jsxRuntime.jsx)(_Button.Button, {
          ariaLabel: t("close.text"),
          onClick: handleCloseButtonClick,
          size: "small",
          startIcon: (0, _jsxRuntime.jsx)(_index.CloseIcon, {}),
          variant: "floating"
        })]
      }), (0, _jsxRuntime.jsx)(_DialogContent2.default, {
        ...(isContentScrollable && {
          tabIndex: 0
        }),
        dividers: isContentScrollable,
        ref: dialogContentRef,
        children: content
      }), actionButtons.length > 0 && (0, _jsxRuntime.jsx)(_DialogActions2.default, {
        children: actionButtons.map((actionButton, index) => actionButton ? (0, _react.cloneElement)(actionButton, {
          key: index
        }) : null)
      })]
    })
  });
};
const MemoizedDialog = exports.Dialog = (0, _react.memo)(Dialog);
MemoizedDialog.displayName = "Dialog";
//# sourceMappingURL=Dialog.cjs.map