"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.FullScreenDrawer = FullScreenDrawer;
var _react = _interopRequireWildcard(require("react"));
var _reactDom = require("react-dom");
var _classnames = _interopRequireDefault(require("classnames"));
var _useReturnFocusOnClose = require("../hooks/use-return-focus-on-close");
var _useFocusTrap = require("../hooks/use-focus-trap");
var _useTranslation = require("../hooks/use-translation");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
/*
 full-screen-drawer-disabled-header - disable main usecase header so when drawer is shown user cannot interact with other part of app
 full-screen-drawer-body - all other functionality in the drawer
 It is expected that FullScreenDrawerHeader will be used inside full-screen-drawer-body
*/
function FullScreenDrawerContainer({
  children,
  onDismiss,
  className,
  ariaLabel
}) {
  const {
    t
  } = (0, _useTranslation.useTranslation)();
  const drawerContainerRef = (0, _react.useRef)(null);
  (0, _useFocusTrap.useFocusTrap)(drawerContainerRef);
  (0, _react.useEffect)(() => {
    // The drawer should get focus when opened, so the following Tab will move focus to the first focusable element within
    drawerContainerRef.current?.focus();
  }, []);
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    // when aria-modal="true", screen readers might limit interaction to the modal content only and, for example, not announce the content of typeahead
    role: "dialog",
    "aria-label": ariaLabel ?? t('Full-screen drawer'),
    tabIndex: -1 // Shouldn't be navigated directly by pressing Tab, but should be implicitly focusable when modal is opened and .focus() is called.
    ,
    ref: drawerContainerRef,
    "test-id": "full-screen-drawer-container",
    className: (0, _classnames.default)('full-screen-drawer-container', className),
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: "full-screen-drawer-disabled-header",
      "test-id": "full-screen-drawer-disabled-header",
      onClick: onDismiss
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: "full-screen-drawer-body",
      children: children
    })]
  });
}

/** A drawer is a panel that slides in from the bottom side of the screen and overlap page content.
 * @uxr-only-supported
 * @midnight-gray-supported
 * @alpine-light-supported
 */

function FullScreenDrawer({
  show = false,
  children,
  onDismiss,
  ariaLabel,
  className,
  rootContainerId = 'full-screen-modal-root'
}) {
  const rootContainer = document.getElementById(rootContainerId);
  (0, _useReturnFocusOnClose.useReturnFocusOnClose)({
    isShown: show
  });
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
    children: show && /*#__PURE__*/(0, _reactDom.createPortal)(/*#__PURE__*/(0, _jsxRuntime.jsx)(FullScreenDrawerContainer, {
      ariaLabel: ariaLabel,
      onDismiss: onDismiss,
      className: className,
      children: children
    }), rootContainer)
  });
}