"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "PopupRoot", {
    enumerable: true,
    get: function() {
        return PopupRoot;
    }
});
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _reactdraggable = /*#__PURE__*/ _interop_require_default(require("react-draggable"));
const _hooks = require("../../hooks");
const _utils = require("../../utils");
const _classnames = /*#__PURE__*/ _interop_require_default(require("classnames"));
const _PopupContext = require("./PopupContext");
const _Popupstyles = require("./Popup.styles");
const _Popuptokens = require("./Popup.tokens");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
const 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;
    }
};
const PopupRoot = /*#__PURE__*/ (0, _react.forwardRef)(({ id, position, setVisible, children, role, frame, zIndex, animationInfo, draggable, ...rest }, ref)=>{
    const contentRef = (0, _react.useRef)(null);
    const innerRef = (0, _hooks.useForkRef)(contentRef, ref);
    const uniqueId = (0, _utils.safeUseId)();
    const handleClass = `draggable-handle-${uniqueId.replace(/:/g, '-')}`;
    const popupController = (0, _PopupContext.usePopupContext)();
    const positionOffset = getDragPositionOffset(position?.transform);
    const currentPosition = positionOffset && draggable ? {
        ...position,
        transform: undefined
    } : position;
    const handleAnimationEnd = (0, _react.useCallback)((e)=>{
        if (!contentRef || e.target !== contentRef.current) {
            return;
        }
        e.stopPropagation();
        if (animationInfo?.endAnimation) {
            popupController.unregister(id);
            setVisible(false);
            animationInfo.setEndAnimation(false);
        }
    }, [
        popupController.unregister,
        animationInfo?.endAnimation,
        setVisible
    ]);
    const popupNode = /*#__PURE__*/ _react.default.createElement(_Popupstyles.PopupRootContainer, {
        className: (0, _classnames.default)(_Popuptokens.classes.root, handleClass),
        ref: innerRef,
        position: currentPosition,
        zIndex: zIndex,
        frame: frame,
        onAnimationEnd: handleAnimationEnd,
        onTransitionEnd: handleAnimationEnd,
        ...rest
    }, /*#__PURE__*/ _react.default.createElement(_Popupstyles.PopupView, {
        role: role
    }, children));
    if (!draggable) {
        return popupNode;
    }
    return /*#__PURE__*/ _react.default.createElement(_reactdraggable.default, {
        nodeRef: contentRef,
        handle: `.${handleClass}`,
        cancel: `.${_Popuptokens.classes.resizableHandleWrapper}`,
        defaultClassName: _Popuptokens.classes.draggablePopupWrapper,
        defaultClassNameDragging: _Popuptokens.classes.draggingPopupWrapper,
        positionOffset: positionOffset
    }, popupNode);
});
