function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useRef, useState } from 'react';
import FocusLock from 'react-focus-lock';
import { AnimatePresence, motion } from 'framer-motion';
import { RemoveScroll } from 'react-remove-scroll';
import { Flexbox, Icon, Portal, theme, useMedia, useOnClickOutside } from '../..';
import { Backdrop, StyledBottomSheet, StyledIconButton } from './BottomSheet.styles';
import { isBoolean } from '../../common/utils';
import { TRANSITION_DURATION } from './constants';
var BottomSheet = function BottomSheet(_ref) {
  var children = _ref.children,
    className = _ref.className,
    closeOnClickOutside = _ref.closeOnClickOutside,
    fullScreenMobile = _ref.fullScreenMobile,
    height = _ref.height,
    invertedColors = _ref.invertedColors,
    onCloseExternal = _ref.onClose,
    isOpenExternal = _ref.open,
    _ref$showBackdrop = _ref.showBackdrop,
    showBackdrop = _ref$showBackdrop === void 0 ? true : _ref$showBackdrop,
    title = _ref.title;
  var internalRef = useRef(null);
  var isControlled = isBoolean(isOpenExternal);
  var _useState = useState(true),
    _useState2 = _slicedToArray(_useState, 2),
    isOpenInternal = _useState2[0],
    setIsOpenInternal = _useState2[1];
  var isMobile = useMedia(function (t) {
    return t.media.lessThan(theme.breakpoints.sm);
  }) || false;
  var onClose = function onClose() {
    setIsOpenInternal(false);
    if (typeof onCloseExternal === 'function') {
      onCloseExternal();
    }
  };
  useOnClickOutside(internalRef, function () {
    if (closeOnClickOutside) {
      onClose();
    }
  });
  var shouldRender = isControlled ? isOpenExternal : isOpenInternal;
  return React.createElement(Portal, null, React.createElement(AnimatePresence, null, shouldRender ? React.createElement(FocusLock, {
    disabled: !isMobile
  }, React.createElement(RemoveScroll, {
    enabled: isMobile
  }, showBackdrop && React.createElement(motion.div, {
    key: "backdrop",
    initial: {
      opacity: 0,
      zIndex: theme.zIndex.overlay,
      position: 'fixed'
    },
    exit: {
      opacity: 0
    },
    animate: {
      opacity: 1
    },
    transition: {
      type: 'ease',
      duration: TRANSITION_DURATION
    }
  }, React.createElement(Backdrop, {
    container: true,
    alignItems: "center",
    justifyContent: "center"
  })), React.createElement(StyledBottomSheet, {
    key: "bottomsheet",
    initial: {
      bottom: '-100%',
      position: 'fixed',
      zIndex: theme.zIndex.modal
    },
    exit: {
      bottom: '-100%'
    },
    animate: {
      bottom: 0
    },
    transition: {
      type: 'ease',
      duration: TRANSITION_DURATION
    },
    className: className,
    $fullScreenMobile: fullScreenMobile,
    height: height,
    $invertedColors: invertedColors,
    ref: internalRef
  }, React.createElement(Flexbox, {
    container: true,
    direction: "column",
    gap: 2
  }, React.createElement(Flexbox, {
    container: true,
    item: true,
    justifyContent: "space-between",
    alignItems: "center"
  }, React.createElement(Flexbox, {
    item: true
  }, title), typeof onClose === 'function' && React.createElement(Flexbox, {
    item: true,
    alignSelf: "flex-start"
  }, React.createElement(StyledIconButton, {
    variant: "primary",
    onClick: onClose,
    size: "s",
    $invertedColors: invertedColors
  }, React.createElement(Icon.Cross16, null)))), React.createElement(Flexbox, {
    item: true
  }, children))))) : null));
};
export default BottomSheet;