"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Sheet = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactTransitionGroup = require("react-transition-group");
var _classnames = _interopRequireDefault(require("classnames"));
var _Styles = require("./Styles");
var _IconButton = require("../IconButton");
var _Overlay = require("../Overlay");
var _Tooltip = require("../Tooltip");
var _Window = require("../Window");
var _excluded = ["children", "onClose", "controls", "open", "animation", "position", "mode", "disableFocusLock", "disableScrollLock", "disableBackgroundColor", "bodyStyle", "bodyClassName", "disableBackgroundClick"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
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 _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; }
var Sheet = exports.Sheet = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, ref) {
  var children = _ref.children,
    onClose = _ref.onClose,
    _ref$controls = _ref.controls,
    controls = _ref$controls === void 0 ? [] : _ref$controls,
    _ref$open = _ref.open,
    defaultOpen = _ref$open === void 0 ? false : _ref$open,
    _ref$animation = _ref.animation,
    animation = _ref$animation === void 0 ? "top" : _ref$animation,
    _ref$position = _ref.position,
    position = _ref$position === void 0 ? "center" : _ref$position,
    _ref$mode = _ref.mode,
    mode = _ref$mode === void 0 ? "normal" : _ref$mode,
    _ref$disableFocusLock = _ref.disableFocusLock,
    disableFocusLock = _ref$disableFocusLock === void 0 ? false : _ref$disableFocusLock,
    _ref$disableScrollLoc = _ref.disableScrollLock,
    disableScrollLock = _ref$disableScrollLoc === void 0 ? false : _ref$disableScrollLoc,
    _ref$disableBackgroun = _ref.disableBackgroundColor,
    disableBackgroundColor = _ref$disableBackgroun === void 0 ? false : _ref$disableBackgroun,
    bodyStyle = _ref.bodyStyle,
    bodyClassName = _ref.bodyClassName,
    _ref$disableBackgroun2 = _ref.disableBackgroundClick,
    disableBackgroundClick = _ref$disableBackgroun2 === void 0 ? false : _ref$disableBackgroun2,
    rest = _objectWithoutProperties(_ref, _excluded);
  var _useState = (0, _react.useState)(defaultOpen),
    _useState2 = _slicedToArray(_useState, 2),
    open = _useState2[0],
    setOpen = _useState2[1];
  var _useState3 = (0, _react.useState)(true),
    _useState4 = _slicedToArray(_useState3, 2),
    exited = _useState4[0],
    setExited = _useState4[1];
  (0, _react.useEffect)(function () {
    setOpen(defaultOpen);
  }, [defaultOpen]);
  var handleEnter = (0, _react.useCallback)(function () {
    setExited(false);
  }, []);
  var handleExited = (0, _react.useCallback)(function () {
    setExited(true);
    onClose === null || onClose === void 0 || onClose();
  }, [onClose]);
  var handleClose = (0, _react.useCallback)(function () {
    setOpen(false);
  }, []);
  var handleBackgroundClick = (0, _react.useCallback)(function (event) {
    if (!disableBackgroundClick) {
      event.preventDefault();
      handleClose();
    }
  }, [disableBackgroundClick, handleClose]);
  (0, _react.useImperativeHandle)(ref, function () {
    return {
      close: handleClose
    };
  });
  if (!open && exited) {
    return null;
  }
  return /*#__PURE__*/_react.default.createElement(_Window.Window, _extends({}, rest, {
    onClose: handleClose,
    disableFocusLock: disableFocusLock,
    disableScrollLock: disableScrollLock
  }), open && /*#__PURE__*/_react.default.createElement(_Overlay.Overlay, {
    onClick: handleBackgroundClick,
    disableBackgroundColor: disableBackgroundColor
  }), /*#__PURE__*/_react.default.createElement(_Styles.StyledSheetWrapper, {
    className: (0, _classnames.default)("c-sheet__wrapper", bodyClassName),
    style: bodyStyle,
    $position: position,
    $mode: mode
  }, controls.length > 0 && /*#__PURE__*/_react.default.createElement(_Styles.StyledSheetIcons, null, controls.map(function (_ref2, index) {
    var IconComponent = _ref2.icon,
      tooltip = _ref2.tooltip,
      onClick = _ref2.onClick,
      disabled = _ref2.disabled,
      className = _ref2.className;
    var maxDurationIn = 4;
    var maxDurationOut = 2;
    var control = index + 1;
    var durationOut = Math.max(0, maxDurationOut - control);
    var durationIn = Math.min(control, maxDurationIn);
    return /*#__PURE__*/_react.default.createElement(_reactTransitionGroup.Transition, {
      appear: true,
      in: open,
      timeout: maxDurationIn * 100,
      key: "c-sheet-control-".concat(index)
    }, function (state) {
      return /*#__PURE__*/_react.default.createElement(_Tooltip.Tooltip, {
        title: tooltip,
        disable: !tooltip || disabled
      }, /*#__PURE__*/_react.default.createElement(_Styles.StyledAnimatedSpan, {
        onClick: onClick,
        $disabled: disabled,
        $mode: mode,
        $state: state,
        className: className,
        $maxDurationIn: maxDurationIn / 10,
        $maxDurationOut: maxDurationOut / 10,
        $durationOut: durationOut / 10,
        $durationIn: durationIn / 10
      }, IconComponent));
    });
  })), /*#__PURE__*/_react.default.createElement(_Styles.StyledCssTransition, {
    appear: true,
    in: open,
    timeout: 400,
    classNames: "c-sheet__animation",
    $direction: animation,
    onEnter: handleEnter,
    onExited: handleExited
  }, /*#__PURE__*/_react.default.createElement(_Styles.StyledSheetWrapperPaper, {
    paperClass: "c-sheet__wrapper__paper"
  }, controls.length > 0 && /*#__PURE__*/_react.default.createElement(_Styles.StyledHeader, null, /*#__PURE__*/_react.default.createElement(_Styles.StyledIconsWrapperSmall, null, controls.map(function (_ref3, index) {
    var IconComponent = _ref3.icon,
      tooltip = _ref3.tooltip,
      onClick = _ref3.onClick,
      disabled = _ref3.disabled,
      className = _ref3.className;
    return /*#__PURE__*/_react.default.createElement(_Tooltip.Tooltip, {
      title: tooltip,
      disable: !tooltip || disabled,
      key: index
    }, /*#__PURE__*/_react.default.createElement(_IconButton.IconButton, {
      onClick: onClick,
      disabled: disabled,
      className: className,
      variant: "text gray"
    }, IconComponent));
  })), /*#__PURE__*/_react.default.createElement(_Styles.StyledListSeparator, null)), children))));
});
Sheet.displayName = "Sheet";
//# sourceMappingURL=Sheet.js.map