'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');
var IconClose = require('../_Icon/Icons/IconClose.js');
var index = require('../Popup/utils/index.js');
var Modal_tokens = require('./Modal.tokens.js');
var base = require('./variations/_view/base.js');
var ModalContext = require('./ModalContext.js');
var Modal_styles = require('./Modal.styles.js');
var useFocusTrap = require('../../hooks/useFocusTrap.js');
var PopupContext = require('../Popup/PopupContext.js');
var useForkRef = require('../../hooks/useForkRef.js');
var react = require('../../utils/react.js');
var useModal = require('./hooks/useModal.js');
var common = require('../../engines/common.js');
var Popup$1 = require('../Popup/Popup.js');
var Overlay = require('../Overlay/Overlay.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var React__default = /*#__PURE__*/_interopDefault(React);

var _IconClose;
var _excluded = ["id", "withAnimation", "onClose", "onOverlayClick", "onEscKeyDown", "closeOnEsc", "closeOnOverlayClick", "withBlur", "isFocusTrapped", "initialFocusRef", "focusAfterRef", "zIndex", "popupInfo", "children", "view", "opened", "isOpen", "hasBody", "hasClose", "resizable", "draggable", "overlay"];

// issue #823
var Popup = /*#__PURE__*/common.component(Popup$1.popupConfig);

/**
 * Modal.
 * Управляет показом/скрытием, подложкой и анимацией визуальной части модального окна.
 */
var modalRoot = function modalRoot(Root) {
  return /*#__PURE__*/React.forwardRef(function (_ref, outerRootRef) {
    var id = _ref.id,
      withAnimation = _ref.withAnimation,
      onClose = _ref.onClose,
      onOverlayClick = _ref.onOverlayClick,
      onEscKeyDown = _ref.onEscKeyDown,
      _ref$closeOnEsc = _ref.closeOnEsc,
      closeOnEsc = _ref$closeOnEsc === void 0 ? true : _ref$closeOnEsc,
      _ref$closeOnOverlayCl = _ref.closeOnOverlayClick,
      closeOnOverlayClick = _ref$closeOnOverlayCl === void 0 ? true : _ref$closeOnOverlayCl,
      withBlur = _ref.withBlur,
      _ref$isFocusTrapped = _ref.isFocusTrapped,
      isFocusTrapped = _ref$isFocusTrapped === void 0 ? true : _ref$isFocusTrapped,
      initialFocusRef = _ref.initialFocusRef,
      focusAfterRef = _ref.focusAfterRef,
      zIndex = _ref.zIndex,
      popupInfo = _ref.popupInfo,
      children = _ref.children,
      view = _ref.view,
      opened = _ref.opened,
      isOpen = _ref.isOpen,
      hasBody = _ref.hasBody,
      hasClose = _ref.hasClose,
      resizable = _ref.resizable,
      draggable = _ref.draggable,
      overlay = _ref.overlay,
      rest = _rollupPluginBabelHelpers.objectWithoutProperties(_ref, _excluded);
    var innerIsOpen = Boolean(isOpen || opened);
    var innerHasClose = hasClose === undefined && hasBody || hasClose;
    var trapRef = useFocusTrap.useFocusTrap(isFocusTrapped, initialFocusRef, focusAfterRef, true, !isFocusTrapped);
    var popupController = PopupContext.usePopupContext();
    var innerRef = useForkRef.useForkRef(trapRef, outerRootRef);
    var uniqId = react.safeUseId();
    var innerId = id || uniqId;
    var overlayBackgroundToken = withBlur ? "var(".concat(Modal_tokens.tokens.modalOverlayWithBlurColor, ")") : "var(".concat(Modal_tokens.tokens.modalOverlayColor, ")");
    var _useModal = useModal.useModal({
        id: innerId,
        isOpen: innerIsOpen,
        closeOnEsc: closeOnEsc,
        onEscKeyDown: onEscKeyDown,
        onClose: onClose,
        popupInfo: popupInfo
      }),
      modalInfo = _useModal.modalInfo;
    var transparent = React.useMemo(function () {
      return ModalContext.getIdLastModal(Array.from(popupController.items.values())) !== innerId;
    }, [innerId, popupController.items]);
    var onModalOverlayKeyDown = React.useCallback(function (event) {
      if (onOverlayClick) {
        onOverlayClick(event);
        return;
      }
      if (onClose) {
        onClose();
      }
    }, [closeOnOverlayClick, onOverlayClick, onClose]);
    var overlayNode = /*#__PURE__*/React__default.default.createElement(Overlay.Overlay, {
      className: Modal_tokens.classes.overlay,
      zIndex: zIndex || index.DEFAULT_Z_INDEX,
      backgroundColorProperty: overlayBackgroundToken,
      withBlur: withBlur,
      transparent: transparent,
      isClickable: closeOnOverlayClick,
      onOverlayClick: onModalOverlayKeyDown
    });
    return /*#__PURE__*/React__default.default.createElement(Popup, _rollupPluginBabelHelpers.extends({
      id: innerId,
      opened: innerIsOpen,
      ref: innerRef,
      popupInfo: modalInfo,
      withAnimation: withAnimation,
      zIndex: zIndex,
      overlay: /*#__PURE__*/React__default.default.createElement(Root, {
        view: view
      }, overlay || overlayNode),
      draggable: draggable,
      resizable: resizable
    }, rest), hasBody ? /*#__PURE__*/React__default.default.createElement(Root, {
      view: view
    }, /*#__PURE__*/React__default.default.createElement(Modal_styles.ModalBody, null, /*#__PURE__*/React__default.default.createElement(Modal_styles.ModalContent, null, innerHasClose && /*#__PURE__*/React__default.default.createElement(Modal_styles.CloseButton, {
      onClick: onClose,
      "data-test": "modal-close"
    }, _IconClose || (_IconClose = /*#__PURE__*/React__default.default.createElement(IconClose.IconClose, {
      size: "s",
      color: "currentColor"
    }))), children))) : /*#__PURE__*/React__default.default.createElement(React__default.default.Fragment, null, children));
  });
};
var modalConfig = {
  name: 'Modal',
  tag: 'div',
  layout: modalRoot,
  base: Modal_styles.base,
  variations: {
    view: {
      css: base.base
    }
  },
  defaults: {
    view: 'default'
  }
};

exports.modalConfig = modalConfig;
exports.modalRoot = modalRoot;
