import React, { useRef, useEffect } from 'react';
import { createPortal } from 'react-dom';
import classNames from 'classnames';
import { useReturnFocusOnClose } from '../hooks/use-return-focus-on-close';
import { useFocusTrap } from '../hooks/use-focus-trap';
import { useTranslation } from '../hooks/use-translation';
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
/*
 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
  } = useTranslation();
  const drawerContainerRef = useRef(null);
  useFocusTrap(drawerContainerRef);
  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__*/_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: classNames('full-screen-drawer-container', className),
    children: [/*#__PURE__*/_jsx("div", {
      className: "full-screen-drawer-disabled-header",
      "test-id": "full-screen-drawer-disabled-header",
      onClick: onDismiss
    }), /*#__PURE__*/_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
 */

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