var _excluded = ["as", "className", "children", "closeOnClickOutside", "disableContentStyle", "footer", "onClose", "open", "title", "closeButtonTitle", "onExitAnimationComplete", "onAnimationComplete", "disableInitialAnimation", "preventOnClickOutsideDataAttributes"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
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; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { useCallback, useEffect, useRef, useState } from 'react';
import { useUIDSeed } from 'react-uid';
import styled from 'styled-components';
import FocusLock from 'react-focus-lock';
import { RemoveScroll } from 'react-remove-scroll';
import { AnimatePresence, motion, useDragControls } from 'framer-motion';
import { fromKebabToCamelCase, isBoolean, isElement } from '../../common/utils';
import { useOnClickOutside } from '../../common/Hooks';
import { Button, Icon, Portal, Typography, useKeyPress, useMedia } from '../..';
var CROSS_SIZE = 5;
var PADDING_MOBILE = 3;
var PADDING_Y = 4;
var PADDING_X = 5;
var displayName = 'Drawer';
var PREVENT_CLICK_OUTSIDE_ATTRIBUTE = 'drawerPreventClickOutside';
var Container = styled(motion.div).withConfig({
  displayName: "Drawer__Container",
  componentId: "sc-1ebdzul-0"
})(["box-sizing:border-box;width:100%;height:100%;background:", ";position:fixed;z-index:", ";right:0;top:0;display:flex;flex-direction:column;box-shadow:0 -10px 16px 0 ", ";", "{width:", "px;}", "{width:", "px;}"], function (_ref) {
  var theme = _ref.theme;
  return theme.color.card;
}, function (_ref2) {
  var theme = _ref2.theme;
  return theme.zIndex.overlay;
}, function (_ref3) {
  var theme = _ref3.theme;
  return theme.color.shadowModal;
}, function (_ref4) {
  var theme = _ref4.theme;
  return theme.media.greaterThan(theme.breakpoints.sm);
}, function (_ref5) {
  var theme = _ref5.theme;
  return theme.spacing.unit(100);
}, function (_ref6) {
  var theme = _ref6.theme;
  return theme.media.greaterThan(theme.breakpoints.xl);
}, function (_ref7) {
  var theme = _ref7.theme;
  return theme.spacing.unit(120);
});
var CloseButton = styled(Button.Icon).withConfig({
  displayName: "Drawer__CloseButton",
  componentId: "sc-1ebdzul-1"
})(["position:absolute;top:", "px;right:", "px;", "{top:", "px;right:", "px;}"], function (p) {
  return p.theme.spacing.unit(PADDING_Y);
}, function (p) {
  return p.theme.spacing.unit(PADDING_X);
}, function (p) {
  return p.theme.media.lessThan(p.theme.breakpoints.sm);
}, function (p) {
  return p.theme.spacing.unit(PADDING_MOBILE);
}, function (p) {
  return p.theme.spacing.unit(PADDING_MOBILE);
});
var Content = styled.div.withConfig({
  displayName: "Drawer__Content",
  componentId: "sc-1ebdzul-2"
})(["overflow-y:auto;overflow-x:hidden;margin-bottom:", "px;padding:1px ", "px;", "{padding:1px ", "px;}"], function (p) {
  return p.theme.spacing.unit(PADDING_Y);
}, function (p) {
  return p.theme.spacing.unit(PADDING_X);
}, function (p) {
  return p.theme.media.lessThan(p.theme.breakpoints.sm);
}, function (p) {
  return p.theme.spacing.unit(PADDING_MOBILE);
});
var Footer = styled.div.withConfig({
  displayName: "Drawer__Footer",
  componentId: "sc-1ebdzul-3"
})(["margin-top:auto;padding:0 ", "px ", "px ", "px;", "{padding:0 ", "px ", "px ", "px;}"], function (p) {
  return p.theme.spacing.unit(PADDING_X);
}, function (p) {
  return p.theme.spacing.unit(PADDING_Y);
}, function (p) {
  return p.theme.spacing.unit(PADDING_X);
}, function (p) {
  return p.theme.media.lessThan(p.theme.breakpoints.sm);
}, function (p) {
  return p.theme.spacing.unit(PADDING_MOBILE);
}, function (p) {
  return p.theme.spacing.unit(PADDING_MOBILE);
}, function (p) {
  return p.theme.spacing.unit(PADDING_MOBILE);
});
var H2 = styled.h2.withConfig({
  displayName: "Drawer__H2",
  componentId: "sc-1ebdzul-4"
})(["padding-right:", "px;"], function (p) {
  return p.theme.spacing.unit(4);
});
var TitleWrapper = styled.div.withConfig({
  displayName: "Drawer__TitleWrapper",
  componentId: "sc-1ebdzul-5"
})(["padding:", ";", "{padding:", ";}margin-bottom:", "px;min-height:", "px;flex:0 0 auto;"], function (p) {
  return "".concat(p.theme.spacing.unit(PADDING_Y), "px \n     ").concat(p.theme.spacing.unit(PADDING_X), "px \n     0 \n     ").concat(p.theme.spacing.unit(PADDING_X), "px");
}, function (p) {
  return p.theme.media.lessThan(p.theme.breakpoints.sm);
}, function (p) {
  return "".concat(p.theme.spacing.unit(PADDING_MOBILE), "px ").concat(p.theme.spacing.unit(PADDING_MOBILE), "px 0 ").concat(p.theme.spacing.unit(PADDING_MOBILE), "px");
}, function (p) {
  return p.theme.spacing.unit(2);
}, function (p) {
  return p.theme.spacing.unit(CROSS_SIZE);
});
var animationProps = {
  initial: {
    opacity: 0,
    x: '100%'
  },
  animate: {
    opacity: 1,
    x: '0%'
  },
  exit: {
    opacity: 0,
    x: '100%'
  },
  transition: {
    ease: 'easeInOut',
    duration: 0.2
  }
};
var noInitialAnimationProps = {
  initial: {
    opacity: 1,
    x: '0%'
  }
};
var components = {
  CloseButton: CloseButton,
  Container: Container,
  Content: Content,
  Footer: Footer,
  TitleWrapper: TitleWrapper
};
var Title = function Title(_ref8) {
  var title = _ref8.title,
    uid = _ref8.uid;
  var isDesktop = useMedia(function (t) {
    return t.media.greaterThan(t.breakpoints.sm);
  });
  return React.createElement("span", {
    id: uid
  }, isElement(title) ? title : React.createElement(Typography, {
    as: H2,
    type: isDesktop ? 'title3' : 'title1'
  }, title));
};
export var Drawer = React.forwardRef(function (_ref9, ref) {
  var as = _ref9.as,
    className = _ref9.className,
    children = _ref9.children,
    _ref9$closeOnClickOut = _ref9.closeOnClickOutside,
    closeOnClickOutside = _ref9$closeOnClickOut === void 0 ? true : _ref9$closeOnClickOut,
    disableContentStyle = _ref9.disableContentStyle,
    footer = _ref9.footer,
    onClose = _ref9.onClose,
    isOpenExternal = _ref9.open,
    title = _ref9.title,
    closeButtonTitle = _ref9.closeButtonTitle,
    onExitAnimationComplete = _ref9.onExitAnimationComplete,
    onAnimationComplete = _ref9.onAnimationComplete,
    disableInitialAnimation = _ref9.disableInitialAnimation,
    preventOnClickOutsideDataAttributes = _ref9.preventOnClickOutsideDataAttributes,
    rest = _objectWithoutProperties(_ref9, _excluded);
  var isControlled = isBoolean(isOpenExternal);
  var escapePress = useKeyPress('Escape');
  var _useState = useState(true),
    _useState2 = _slicedToArray(_useState, 2),
    isOpenInternal = _useState2[0],
    setIsOpenInternal = _useState2[1];
  var isOpen = isControlled ? isOpenExternal : isOpenInternal;
  var internalDrawerRef = useRef(null);
  var drawerRef = ref || internalDrawerRef;
  var isDesktop = useMedia(function (t) {
    return t.media.greaterThan(t.breakpoints.sm);
  }) || false;
  var seed = useUIDSeed();
  var uid = seed(displayName);
  var dragControls = useDragControls();
  var startDrag = useCallback(function (event) {
    dragControls.start(event, {
      snapToCursor: false
    });
  }, [dragControls]);
  var handleClose = useCallback(function () {
    setIsOpenInternal(false);
    if (onClose) {
      onClose();
    }
  }, [onClose]);
  var handleDragEnd = useCallback(function () {
    handleClose();
  }, [handleClose]);
  useOnClickOutside(drawerRef, function (e) {
    if (!closeOnClickOutside) return;
    var preventingDataAttributes = [PREVENT_CLICK_OUTSIDE_ATTRIBUTE].concat(_toConsumableArray(preventOnClickOutsideDataAttributes || []));
    var preventOnClickOutside = e instanceof Event && e.composedPath().some(function (target) {
      if (!(target instanceof HTMLElement)) return false;
      return preventingDataAttributes.some(function (dataAttribute) {
        var _target$dataset;
        var formattedDataAttribute = fromKebabToCamelCase(dataAttribute.replace('data-', ''));
        return (_target$dataset = target.dataset) === null || _target$dataset === void 0 ? void 0 : _target$dataset[formattedDataAttribute];
      });
    });
    if (!preventOnClickOutside) handleClose();
  });
  useEffect(function () {
    if (isOpen && escapePress) {
      handleClose();
    }
  }, [escapePress, handleClose, isOpen]);
  return React.createElement(AnimatePresence, {
    onExitComplete: onExitAnimationComplete
  }, isOpen ? React.createElement(Portal, null, React.createElement(FocusLock, {
    disabled: isDesktop
  }, React.createElement(RemoveScroll, {
    enabled: !isDesktop
  }, React.createElement(Container, _extends({
    as: as,
    className: className,
    "aria-labelledby": uid
  }, animationProps, disableInitialAnimation ? noInitialAnimationProps : {}, {
    ref: drawerRef,
    dragControls: dragControls,
    dragListener: false,
    drag: "x",
    onDragEnd: handleDragEnd,
    onAnimationComplete: onAnimationComplete
  }, rest), React.createElement(TitleWrapper, {
    onTouchStart: startDrag
  }, title && React.createElement(Title, {
    title: title,
    uid: uid
  }), React.createElement(CloseButton, {
    onClick: handleClose
  }, React.createElement(Icon.Cross16, {
    title: closeButtonTitle
  }))), disableContentStyle ? children : React.createElement(Content, null, children), footer && React.createElement(Footer, null, footer))))) : null);
});
Drawer.components = components;
Drawer.displayName = displayName;