'use strict';

var clsx = require('clsx');
var React = require('react');
var Dimmer = require('../../dimmer/Dimmer.js');
var Drawer = require('../../drawer/Drawer.js');
var OverlayIdProvider = require('../../provider/overlay/OverlayIdProvider.js');
var SlidingPanel = require('../../slidingPanel/SlidingPanel.js');
var CloseButton = require('../closeButton/CloseButton.js');
var documentIosClick = require('../domHelpers/documentIosClick.js');
var useConditionalListener = require('../hooks/useConditionalListener/useConditionalListener.js');
require('../../provider/direction/DirectionProvider.js');
var breakpoint = require('../propsValues/breakpoint.js');
var useMedia = require('../hooks/useMedia.js');
var position = require('../propsValues/position.js');
var jsxRuntime = require('react/jsx-runtime');

const INITIAL_Y_POSITION = 0;
const CONTENT_SCROLL_THRESHOLD = 1;
const MOVE_OFFSET_THRESHOLD = 50;
/**
 * Neptune: https://transferwise.github.io/neptune/components/bottom-sheet/
 *
 * Neptune Web: https://transferwise.github.io/neptune-web/components/overlays/BottomSheet
 *
 */
const BottomSheet = ({
  role = 'dialog',
  ...props
}) => {
  const bottomSheetReference = React.useRef(null);
  const topBarReference = React.useRef(null);
  const contentReference = React.useRef(null);
  const [pressed, setPressed] = React.useState(false);
  /**
   * Used to track `requestAnimationFrame` requests
   *
   * @see https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame#return_value
   */
  const animationId = React.useRef(0);
  /**
   * Difference between initial coordinate ({@link initialYCoordinate})
   * and new position (when user moves component), it's get calculated on `onTouchMove` and `onMouseMove` events
   *
   * @see {@link calculateOffsetAfterMove}
   */
  const moveOffset = React.useRef(0);
  const initialYCoordinate = React.useRef(0);
  // apply shadow to the bottom of top-bar when scroll over content
  useConditionalListener.useConditionalListener({
    attachListener: props.open && !documentIosClick.isServerSide(),
    callback: () => {
      if (topBarReference.current !== null) {
        const {
          classList
        } = topBarReference.current;
        if (!isContentScrollPositionAtTop()) {
          classList.add('np-bottom-sheet--top-bar--shadow');
        } else {
          classList.remove('np-bottom-sheet--top-bar--shadow');
        }
      }
    },
    eventType: 'scroll',
    parent: documentIosClick.isServerSide() ? undefined : document
  });
  function move(newHeight) {
    if (bottomSheetReference.current !== null) {
      bottomSheetReference.current.style.transform = `translateY(${newHeight}px)`;
    }
  }
  function close(event) {
    setPressed(false);
    moveOffset.current = INITIAL_Y_POSITION;
    if (bottomSheetReference.current !== null) {
      bottomSheetReference.current.style.removeProperty('transform');
    }
    if (props.onClose) {
      props.onClose(event);
    }
  }
  const onSwipeStart = event => {
    initialYCoordinate.current = ('touches' in event ? event.touches[0] : event).clientY;
    setPressed(true);
  };
  const onSwipeMove = event => {
    if (pressed) {
      const {
        clientY
      } = 'touches' in event ? event.touches[0] : event;
      const offset = calculateOffsetAfterMove(clientY);
      // check whether move is to the bottom only and content scroll position is at the top
      if (offset > INITIAL_Y_POSITION && isContentScrollPositionAtTop()) {
        moveOffset.current = offset;
        animationId.current = requestAnimationFrame(() => {
          if (animationId.current !== undefined && bottomSheetReference.current !== null) {
            move(offset);
          }
        });
      }
    }
  };
  function onSwipeEnd(event) {
    // stop moving component
    cancelAnimationFrame(animationId.current);
    setPressed(false);
    // check whether move down is strong enough
    // and content scroll position is at the top to close the component
    if (moveOffset.current > MOVE_OFFSET_THRESHOLD && isContentScrollPositionAtTop()) {
      close(event);
    }
    // otherwise move component back to default (initial) position
    else {
      move(INITIAL_Y_POSITION);
    }
    moveOffset.current = INITIAL_Y_POSITION;
  }
  function isContentScrollPositionAtTop() {
    return contentReference?.current?.scrollTop !== undefined && contentReference.current.scrollTop <= CONTENT_SCROLL_THRESHOLD;
  }
  /**
   * Calculates how hard user moves component,
   * result value used to determine whether to hide component or re-position to default state
   *
   * @param afterMoveYCoordinate
   */
  function calculateOffsetAfterMove(afterMoveYCoordinate) {
    return afterMoveYCoordinate - initialYCoordinate.current;
  }
  /**
   * Set `max-height` for content part (in order to keep it scrollable for content overflow cases) of the component
   * and ensures space for safe zone (32px) at the top.
   */
  function setContentMaxHeight() {
    const safeZoneHeight = '64px';
    const topbarHeight = '32px';
    const windowHight = documentIosClick.isServerSide() ? 0 : window.innerHeight;
    /**
     * Calculate _real_ height of the screen (taking into account parts of browser interface).
     *
     * See https://css-tricks.com/the-trick-to-viewport-units-on-mobile for more details.
     */
    const screenHeight = `${windowHight * 0.01 * 100}px`;
    return {
      maxHeight: `calc(${screenHeight} - ${safeZoneHeight} - ${topbarHeight})`
    };
  }
  const is400Zoom = useMedia.useMedia(`(max-width: ${breakpoint.Breakpoint.ZOOM_400}px)`);
  const overlayId = React.useContext(OverlayIdProvider.OverlayIdContext);
  return is400Zoom ? /*#__PURE__*/jsxRuntime.jsx(Drawer, {
    "aria-labelledby": props['aria-labelledby'],
    "aria-label": props['aria-label'],
    role: role,
    open: props.open,
    className: props.className,
    onClose: close,
    children: props.children
  }) : /*#__PURE__*/jsxRuntime.jsx(Dimmer.default, {
    open: props.open,
    fadeContentOnEnter: true,
    fadeContentOnExit: true,
    onClose: close,
    children: /*#__PURE__*/jsxRuntime.jsx(SlidingPanel.default, {
      ref: bottomSheetReference,
      open: props.open,
      position: position.Position.BOTTOM,
      className: clsx.clsx('np-bottom-sheet', props.className),
      children: /*#__PURE__*/jsxRuntime.jsxs("div", {
        id: overlayId,
        "aria-labelledby": props['aria-labelledby'] || undefined,
        "aria-label": props['aria-label'] || undefined,
        role: role,
        "aria-modal": true,
        onTouchStart: onSwipeStart,
        onTouchMove: onSwipeMove,
        onTouchEnd: onSwipeEnd,
        onMouseDown: onSwipeStart,
        onMouseMove: onSwipeMove,
        onMouseUp: onSwipeEnd,
        children: [/*#__PURE__*/jsxRuntime.jsxs("div", {
          ref: topBarReference,
          className: "np-bottom-sheet--top-bar",
          children: [/*#__PURE__*/jsxRuntime.jsx("div", {
            className: "np-bottom-sheet--handler"
          }), /*#__PURE__*/jsxRuntime.jsx(CloseButton.CloseButton, {
            size: "sm",
            className: "sr-only np-bottom-sheet--close-btn",
            onClick: close
          })]
        }), /*#__PURE__*/jsxRuntime.jsx("div", {
          ref: contentReference,
          style: setContentMaxHeight(),
          className: "np-bottom-sheet--content",
          children: props.children
        })]
      })
    })
  });
};

module.exports = BottomSheet;
//# sourceMappingURL=BottomSheet.js.map
