"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useDrawerManager = exports.default = exports.DrawerManager = exports.DrawerContext = void 0;
var _compat = require("preact/compat");
var _useId = _interopRequireDefault(require("../utilities/useId"));
var _jsxRuntime = require("preact/compat/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const DrawerContext = exports.DrawerContext = (0, _compat.createContext)(null);

/**
 * The `DrawerManager` feature is useful when there are multiple help drawer links on a
 * page, as it defines the behavior of what happens when a user already has one open and
 * tries to open another. The `DrawerManager` manages the simplest behavior of closing
 * all other drawers when a new one opens.
 *
 * At the top level of your app or page, you can
 * define a `<DrawerManager>` [_context provider_](https://react.dev/learn/passing-data-deeply-with-context)
 * and then in specific parts of your app that manage individual drawers, you can tap into
 * the management behavior by calling the `useDrawerManager` hook. The hook provides the
 * open/closed status of the drawer as well as functions for opening, closing, or toggling
 * the drawer.
 *
 * Here is a minimal example of implementation:
 *
 * ```tsx
 * import { Button, DrawerManager, useDrawerManager } from '@cmsgov/design-system';
 *
 * const ManagedDrawer = (props) => {
 *   const { toggleDrawer, closeDrawer, isDrawerOpen } = useDrawerManager();
 *
 *   return (
 *     <>
 *       <Drawer
 *         {...props}
 *         onCloseClick={closeDrawer}
 *         isOpen={isDrawerOpen}
 *       >
 *       <Button onClick={toggleDrawer}>Click to open drawer</Button>
 *     </>
 *   );
 * }
 *
 * // Using components that use the `useDrawerManager` hook inside an app that is
 * // wrapped in a `DrawerManager` context provider:
 *
 * function App() {
 *   return (
 *     <DrawerManager>
 *       ... any content
 *       <ManagedDrawer {...propsForDrawer1} />
 *       <ManagedDrawer {...propsForDrawer2} />
 *       <ManagedDrawer {...propsForDrawer3} />
 *     </DrawerManager>
 *   );
 * }
 * ```
 *
 * Here is a description of the object that the hook returns:
 *
 * ```ts
 * {
 *   isDrawerOpen: boolean; // whether it's open
 *   openDrawer: () => any; // function that opens it
 *   closeDrawer: () => any; // function that closes it
 *   toggleDrawer: () => any; // function that toggles it
 * }
 * ```
 *
 * [See also the documentation on the drawer component](https://design.cms.gov/components/drawer/).
 */
const DrawerManager = props => {
  const [currentID, setCurrentID] = (0, _compat.useState)(null);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(DrawerContext.Provider, {
    value: {
      currentID,
      setCurrentID
    },
    ...props
  });
};
exports.DrawerManager = DrawerManager;
const useDrawerManager = () => {
  const {
    currentID,
    setCurrentID
  } = (0, _compat.useContext)(DrawerContext);
  const id = (0, _useId.default)();
  const isDrawerOpen = currentID === id;
  const openDrawer = () => setCurrentID(id);
  const closeDrawer = () => setCurrentID(null);
  const toggleDrawer = () => isDrawerOpen ? closeDrawer() : openDrawer();
  return {
    openDrawer,
    closeDrawer,
    toggleDrawer,
    isDrawerOpen
  };
};
exports.useDrawerManager = useDrawerManager;
var _default = exports.default = DrawerManager;