"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get handlePosition () {
        return handlePosition;
    },
    get popupConfig () {
        return popupConfig;
    },
    get popupRoot () {
        return popupRoot;
    }
});
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _hooks = require("../../hooks");
const _utils = require("../../utils");
const _Portal = require("../Portal");
const _ui = require("./ui");
const _PopupRoot = require("./PopupRoot");
const _hooks1 = require("./hooks");
const _Popuptokens = require("./Popup.tokens");
const _Popupstyles = require("./Popup.styles");
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 handlePosition = (placement, offset)=>{
    let x = '0rem';
    let y = '0rem';
    if (offset) {
        const [_x, _y] = offset;
        x = typeof _x === 'number' ? `${_x}rem` : _x;
        y = typeof _y === 'number' ? `${_y}rem` : _y;
    }
    if (!placement || placement === 'center') {
        return {
            left: `calc(50% + ${x})`,
            top: `calc(50% - ${y})`,
            transform: 'translate(-50%, -50%)'
        };
    }
    let left;
    let right;
    let top;
    let bottom;
    let transform;
    const placements = placement.split('-');
    placements.forEach((placementValue)=>{
        switch(placementValue){
            case 'left':
                left = x;
                break;
            case 'right':
                right = x;
                break;
            case 'top':
                top = y;
                break;
            case 'bottom':
                bottom = y;
                break;
            default:
                break;
        }
    });
    const isCenteredX = left === undefined && right === undefined;
    const isCenteredY = top === undefined && bottom === undefined;
    if (isCenteredX) {
        transform = 'translateX(-50%)';
    }
    if (isCenteredY) {
        transform = 'translateY(-50%)';
    }
    return {
        left: isCenteredX ? `calc(50% + ${x})` : left,
        right,
        top: isCenteredY ? `calc(50% - ${y})` : top,
        bottom,
        transform
    };
};
const popupRoot = (Root)=>/*#__PURE__*/ (0, _react.forwardRef)(({ id, isOpen = false, opened = false, placement = 'center', offset = [
        0,
        0
    ], frame = 'document', children, overlay, // eslint-disable-next-line @typescript-eslint/no-unused-vars
    role, zIndex, popupInfo, withAnimation = false, className, draggable, resizable: outerResizable, ...rest }, outerRootRef)=>{
        const innerIsOpen = isOpen || opened;
        const uniqId = (0, _utils.safeUseId)();
        const innerId = id || uniqId;
        const { isVisible, animationInfo, setVisible, rootId } = (0, _hooks1.usePopup)({
            isOpen: innerIsOpen,
            id: innerId,
            popupInfo,
            withAnimation
        });
        const portalRef = (0, _react.useRef)(null);
        const contentRef = (0, _react.useRef)(null);
        const innerRef = (0, _hooks.useForkRef)(contentRef, outerRootRef);
        if (!isVisible && !innerIsOpen) {
            return null;
        }
        const cls = (0, _utils.cx)(className, animationInfo?.endAnimation ? _Popuptokens.classes.endAnimation : '', animationInfo?.endTransition ? _Popuptokens.classes.endTransition : '');
        const rootNode = /*#__PURE__*/ _react.default.createElement(Root, {
            className: cls
        }, overlay, /*#__PURE__*/ _react.default.createElement(_PopupRoot.PopupRoot, {
            id: innerId,
            ref: innerRef,
            draggable: draggable,
            position: handlePosition(placement, offset),
            zIndex: zIndex,
            frame: frame,
            animationInfo: animationInfo,
            setVisible: setVisible,
            ...rest
        }, /*#__PURE__*/ _react.default.createElement(_ui.Resizable, {
            resizable: outerResizable,
            placement: placement
        }, children)));
        if (typeof frame !== 'string' && frame && frame.current && _utils.canUseDOM) {
            return /*#__PURE__*/ _react.default.createElement(_Portal.Portal, {
                container: frame.current
            }, rootNode);
        }
        const withFrameId = typeof frame === 'string' && frame !== 'document';
        const containerElement = withFrameId && _utils.canUseDOM && document.getElementById(frame);
        if (containerElement) {
            return /*#__PURE__*/ _react.default.createElement(_Portal.Portal, {
                container: containerElement
            }, /*#__PURE__*/ _react.default.createElement(_Popupstyles.StyledPortalContainer, {
                ref: portalRef
            }, portalRef.current && /*#__PURE__*/ _react.default.createElement(_Portal.Portal, {
                container: portalRef.current
            }, rootNode)), ' ');
        }
        const globalPortal = _utils.canUseDOM && document.getElementById(rootId);
        return /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, globalPortal && /*#__PURE__*/ _react.default.createElement(_Portal.Portal, {
            container: globalPortal
        }, rootNode));
    });
const popupConfig = {
    name: 'Popup',
    tag: 'div',
    layout: popupRoot,
    base: _Popupstyles.base,
    variations: {},
    defaults: {}
};
