'use strict';

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

var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');
var Draggable = require('react-draggable');
var cls = require('classnames');
var PopupContext = require('./PopupContext.js');
var Popup_styles = require('./Popup.styles.js');
var Popup_tokens = require('./Popup.tokens.js');
var useForkRef = require('../../hooks/useForkRef.js');
var react = require('../../utils/react.js');

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

var React__default = /*#__PURE__*/_interopDefault(React);
var Draggable__default = /*#__PURE__*/_interopDefault(Draggable);
var cls__default = /*#__PURE__*/_interopDefault(cls);

var _excluded = ["id", "position", "setVisible", "children", "role", "frame", "zIndex", "animationInfo", "draggable"];
var getDragPositionOffset = function getDragPositionOffset(transform) {
  switch (transform) {
    case 'translate(-50%, -50%)':
      return {
        x: '-50%',
        y: '-50%'
      };
    case 'translateX(-50%)':
      return {
        x: '-50%',
        y: '0%'
      };
    case 'translateY(-50%)':
      return {
        x: '0%',
        y: '-50%'
      };
    default:
      return undefined;
  }
};

/**
 * Корень Popup.
 * Управляет показом/скрытием и анимацией всплывающего окна.
 */
var PopupRoot = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
  var id = _ref.id,
    position = _ref.position,
    setVisible = _ref.setVisible,
    children = _ref.children,
    role = _ref.role,
    frame = _ref.frame,
    zIndex = _ref.zIndex,
    animationInfo = _ref.animationInfo,
    draggable = _ref.draggable,
    rest = _rollupPluginBabelHelpers.objectWithoutProperties(_ref, _excluded);
  var contentRef = React.useRef(null);
  var innerRef = useForkRef.useForkRef(contentRef, ref);
  var uniqueId = react.safeUseId();
  var handleClass = "draggable-handle-".concat(uniqueId.replace(/:/g, '-'));
  var popupController = PopupContext.usePopupContext();
  var positionOffset = getDragPositionOffset(position === null || position === void 0 ? void 0 : position.transform);
  var currentPosition = positionOffset && draggable ? _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, position), {}, {
    transform: undefined
  }) : position;
  var handleAnimationEnd = React.useCallback(function (e) {
    if (!contentRef || e.target !== contentRef.current) {
      return;
    }
    e.stopPropagation();
    if (animationInfo !== null && animationInfo !== void 0 && animationInfo.endAnimation) {
      popupController.unregister(id);
      setVisible(false);
      animationInfo.setEndAnimation(false);
    }
  }, [popupController.unregister, animationInfo === null || animationInfo === void 0 ? void 0 : animationInfo.endAnimation, setVisible]);
  var popupNode = /*#__PURE__*/React__default.default.createElement(Popup_styles.PopupRootContainer, _rollupPluginBabelHelpers.extends({
    className: cls__default.default(Popup_tokens.classes.root, handleClass),
    ref: innerRef,
    position: currentPosition,
    zIndex: zIndex,
    frame: frame,
    onAnimationEnd: handleAnimationEnd,
    onTransitionEnd: handleAnimationEnd
  }, rest), /*#__PURE__*/React__default.default.createElement(Popup_styles.PopupView, {
    role: role
  }, children));
  if (!draggable) {
    return popupNode;
  }
  return /*#__PURE__*/React__default.default.createElement(Draggable__default.default, {
    nodeRef: contentRef,
    handle: ".".concat(handleClass),
    cancel: ".".concat(Popup_tokens.classes.resizableHandleWrapper),
    defaultClassName: Popup_tokens.classes.draggablePopupWrapper,
    defaultClassNameDragging: Popup_tokens.classes.draggingPopupWrapper,
    positionOffset: positionOffset
  }, popupNode);
});

exports.PopupRoot = PopupRoot;
