"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.Dialog = void 0;
var _compat = _interopRequireWildcard(require("preact/compat"));
var _CloseButton = _interopRequireDefault(require("../CloseButton/CloseButton"));
var _NativeDialog = _interopRequireDefault(require("../NativeDialog/NativeDialog"));
var _classnames = _interopRequireDefault(require("classnames"));
var _useDialogAnalytics = _interopRequireDefault(require("./useDialogAnalytics"));
var _useId = _interopRequireDefault(require("../utilities/useId"));
var _i18n = require("../i18n");
var _useBodyScrollPrevention = require("./useBodyScrollPrevention");
var _excluded = ["actions", "actionsClassName", "alert", "analytics", "analyticsLabelOverride", "analyticsEventTypeOverride", "onAnalyticsEvent", "ariaCloseLabel", "children", "className", "headerClassName", "heading", "id", "onEnter", "onExit", "size"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
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; }
/**
 * For information about how and when to use this component,
 * [refer to its full documentation page](https://design.cms.gov/components/modal-dialog/).
 */
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 = (0, _useId.default)('dialog--', id);
  var headingRef = (0, _useDialogAnalytics.default)(props);
  var headingId = "".concat(rootId, "__heading");
  var contentId = "".concat(rootId, "__content");
  var dialogClassNames = (0, _classnames.default)('ds-c-dialog', className, size && "ds-c-dialog--".concat(size));
  var headerClassNames = (0, _classnames.default)('ds-c-dialog__header', headerClassName);
  var actionsClassNames = (0, _classnames.default)('ds-c-dialog__actions', actionsClassName);
  var containerRef = (0, _compat.useRef)();
  (0, _compat.useEffect)(function () {
    if (onEnter) onEnter();
  }, []);

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