import React, { forwardRef, useRef } from "react";
import { useForkRef } from "../../hooks";
import { canUseDOM, cx, safeUseId } from "../../utils";
import { Portal } from "../Portal";
import { Resizable } from "./ui";
import { PopupRoot } from "./PopupRoot";
import { usePopup } from "./hooks";
import { classes } from "./Popup.tokens";
import { StyledPortalContainer, base } from "./Popup.styles";
export 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
    };
};
/**
 * Базовый компонент Popup.
 */ export const popupRoot = (Root)=>/*#__PURE__*/ 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 = safeUseId();
        const innerId = id || uniqId;
        const { isVisible, animationInfo, setVisible, rootId } = usePopup({
            isOpen: innerIsOpen,
            id: innerId,
            popupInfo,
            withAnimation
        });
        const portalRef = useRef(null);
        const contentRef = useRef(null);
        const innerRef = useForkRef(contentRef, outerRootRef);
        if (!isVisible && !innerIsOpen) {
            return null;
        }
        const cls = cx(className, animationInfo?.endAnimation ? classes.endAnimation : '', animationInfo?.endTransition ? classes.endTransition : '');
        const rootNode = /*#__PURE__*/ React.createElement(Root, {
            className: cls
        }, overlay, /*#__PURE__*/ React.createElement(PopupRoot, {
            id: innerId,
            ref: innerRef,
            draggable: draggable,
            position: handlePosition(placement, offset),
            zIndex: zIndex,
            frame: frame,
            animationInfo: animationInfo,
            setVisible: setVisible,
            ...rest
        }, /*#__PURE__*/ React.createElement(Resizable, {
            resizable: outerResizable,
            placement: placement
        }, children)));
        if (typeof frame !== 'string' && frame && frame.current && canUseDOM) {
            return /*#__PURE__*/ React.createElement(Portal, {
                container: frame.current
            }, rootNode);
        }
        const withFrameId = typeof frame === 'string' && frame !== 'document';
        const containerElement = withFrameId && canUseDOM && document.getElementById(frame);
        if (containerElement) {
            return /*#__PURE__*/ React.createElement(Portal, {
                container: containerElement
            }, /*#__PURE__*/ React.createElement(StyledPortalContainer, {
                ref: portalRef
            }, portalRef.current && /*#__PURE__*/ React.createElement(Portal, {
                container: portalRef.current
            }, rootNode)), ' ');
        }
        const globalPortal = canUseDOM && document.getElementById(rootId);
        return /*#__PURE__*/ React.createElement(React.Fragment, null, globalPortal && /*#__PURE__*/ React.createElement(Portal, {
            container: globalPortal
        }, rootNode));
    });
export const popupConfig = {
    name: 'Popup',
    tag: 'div',
    layout: popupRoot,
    base,
    variations: {},
    defaults: {}
};
