import { objectWithoutProperties as _objectWithoutProperties, objectSpread2 as _objectSpread2, extends as _extends } from '../../_virtual/_rollupPluginBabelHelpers.js';
import React__default, { forwardRef, useRef, useCallback } from 'react';
import Draggable from 'react-draggable';
import cls from 'classnames';
import { usePopupContext } from './PopupContext.js';
import { PopupRootContainer, PopupView } from './Popup.styles.js';
import { classes } from './Popup.tokens.js';
import { useForkRef } from '../../hooks/useForkRef.js';
import { safeUseId } from '../../utils/react.js';

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__*/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 = _objectWithoutProperties(_ref, _excluded);
  var contentRef = useRef(null);
  var innerRef = useForkRef(contentRef, ref);
  var uniqueId = safeUseId();
  var handleClass = "draggable-handle-".concat(uniqueId.replace(/:/g, '-'));
  var popupController = usePopupContext();
  var positionOffset = getDragPositionOffset(position === null || position === void 0 ? void 0 : position.transform);
  var currentPosition = positionOffset && draggable ? _objectSpread2(_objectSpread2({}, position), {}, {
    transform: undefined
  }) : position;
  var handleAnimationEnd = 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.createElement(PopupRootContainer, _extends({
    className: cls(classes.root, handleClass),
    ref: innerRef,
    position: currentPosition,
    zIndex: zIndex,
    frame: frame,
    onAnimationEnd: handleAnimationEnd,
    onTransitionEnd: handleAnimationEnd
  }, rest), /*#__PURE__*/React__default.createElement(PopupView, {
    role: role
  }, children));
  if (!draggable) {
    return popupNode;
  }
  return /*#__PURE__*/React__default.createElement(Draggable, {
    nodeRef: contentRef,
    handle: ".".concat(handleClass),
    cancel: ".".concat(classes.resizableHandleWrapper),
    defaultClassName: classes.draggablePopupWrapper,
    defaultClassNameDragging: classes.draggingPopupWrapper,
    positionOffset: positionOffset
  }, popupNode);
});

export { PopupRoot };
