var _excluded = ["actions", "actionsClassName", "alert", "analytics", "analyticsLabelOverride", "analyticsEventTypeOverride", "onAnalyticsEvent", "ariaCloseLabel", "children", "className", "headerClassName", "heading", "id", "onEnter", "onExit", "size"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { useEffect, useRef } from 'react';
import CloseButton from '../CloseButton/CloseButton';
import NativeDialog from '../NativeDialog/NativeDialog';
import classNames from 'classnames';
import useDialogAnalytics from './useDialogAnalytics';
import useId from '../utilities/useId';
import { t } from '../i18n';
import { useBodyScrollPrevention } from './useBodyScrollPrevention';
/**
 * For information about how and when to use this component,
 * [refer to its full documentation page](https://design.cms.gov/components/modal-dialog/).
 */
export var Dialog = function Dialog(props) {
  var _modalProps$isOpen;
  var actions = props.actions,
    actionsClassName = props.actionsClassName,
    alert = props.alert,
    analytics = props.analytics,
    analyticsLabelOverride = props.analyticsLabelOverride,
    analyticsEventTypeOverride = props.analyticsEventTypeOverride,
    onAnalyticsEvent = props.onAnalyticsEvent,
    ariaCloseLabel = props.ariaCloseLabel,
    children = props.children,
    className = props.className,
    headerClassName = props.headerClassName,
    heading = props.heading,
    id = props.id,
    onEnter = props.onEnter,
    onExit = props.onExit,
    size = props.size,
    modalProps = _objectWithoutProperties(props, _excluded);
  var rootId = useId('dialog--', id);
  var headingRef = useDialogAnalytics(props);
  var headingId = "".concat(rootId, "__heading");
  var contentId = "".concat(rootId, "__content");
  var dialogClassNames = classNames('ds-c-dialog', className, size && "ds-c-dialog--".concat(size));
  var headerClassNames = classNames('ds-c-dialog__header', headerClassName);
  var actionsClassNames = classNames('ds-c-dialog__actions', actionsClassName);
  var containerRef = useRef();
  useEffect(function () {
    if (onEnter) onEnter();
  }, []);

  // Set initial focus
  useEffect(function () {
    var _containerRef$current;
    (_containerRef$current = containerRef.current) === null || _containerRef$current === void 0 ? void 0 : _containerRef$current.focus();
  }, [containerRef]);
  useBodyScrollPrevention((_modalProps$isOpen = modalProps.isOpen) !== null && _modalProps$isOpen !== void 0 ? _modalProps$isOpen : true);
  return /*#__PURE__*/React.createElement(NativeDialog, _extends({
    className: dialogClassNames,
    showModal: true,
    exit: onExit
  }, modalProps, {
    id: rootId,
    boundingBoxRef: containerRef
  }), /*#__PURE__*/React.createElement("div", {
    className: "ds-c-dialog__window",
    role: "document",
    ref: containerRef,
    tabIndex: -1,
    "aria-labelledby": headingId
  }, /*#__PURE__*/React.createElement("header", {
    className: headerClassNames
  }, heading && /*#__PURE__*/React.createElement("h1", {
    className: "ds-c-dialog__heading",
    id: headingId,
    ref: headingRef
  }, heading), /*#__PURE__*/React.createElement(CloseButton, {
    "aria-label": ariaCloseLabel !== null && ariaCloseLabel !== void 0 ? ariaCloseLabel : t('dialog.ariaCloseLabel'),
    className: "ds-c-dialog__close",
    id: "".concat(rootId, "__close"),
    onClick: onExit
  })), /*#__PURE__*/React.createElement("main", {
    role: "main",
    className: "ds-c-dialog__body"
  }, /*#__PURE__*/React.createElement("div", {
    id: contentId
  }, children), actions && /*#__PURE__*/React.createElement("div", {
    className: actionsClassNames
  }, actions))));
};
export default Dialog;