"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FloatingPopup = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
///
/// Fork of Popup component from @gravity-ui/uikit
/// This fork does not use floating focus manager
///
const react_1 = require("react");
// eslint-disable-next-line import/no-extraneous-dependencies
const react_2 = require("@floating-ui/react");
const uikit_1 = require("@gravity-ui/uikit");
const classname_1 = require("../../../../../../../classname.js");
require("./FloatingPopup.css");
const b = (0, classname_1.cn)('yfm-table-floating-popup');
const FloatingPopup = function YfmTableFloatingPopup(props) {
    const { anchorElement, zIndex = 1000, style, className, qa, children, floatingStyles: floatingStylesFromProps, } = props;
    const nodeId = (0, react_2.useFloatingNodeId)();
    const parentId = (0, react_2.useFloatingParentNodeId)();
    const middleware = (0, react_1.useMemo)(() => [(0, react_2.offset)(props.offset)], [props.offset]);
    const { refs, elements, floatingStyles, placement, context: { open }, update, } = (0, react_2.useFloating)({
        nodeId,
        open: props.open,
        placement: props.placement,
        middleware,
    });
    const { getFloatingProps } = (0, react_2.useInteractions)();
    (0, react_1.useEffect)(() => {
        if (anchorElement !== undefined && anchorElement !== refs.reference.current) {
            refs.setReference(anchorElement);
        }
    }, [anchorElement, refs]);
    (0, react_1.useEffect)(() => {
        if (elements.reference && elements.floating) {
            return (0, react_2.autoUpdate)(elements.reference, elements.floating, update);
        }
        return undefined;
    }, [elements, update]);
    function wrapper(node) {
        if (parentId === null) {
            return (0, jsx_runtime_1.jsx)(react_2.FloatingTree, { children: node });
        }
        return node;
    }
    return wrapper((0, jsx_runtime_1.jsx)(react_2.FloatingNode, { id: nodeId, children: open ? ((0, jsx_runtime_1.jsx)(uikit_1.Portal, { children: (0, jsx_runtime_1.jsx)("div", { ref: refs.setFloating, style: {
                    position: 'absolute',
                    top: 0,
                    left: 0,
                    zIndex,
                    width: 'max-content',
                    pointerEvents: 'auto',
                    outline: 'none',
                    ...floatingStylesFromProps,
                    ...floatingStyles,
                }, "data-floating-ui-placement": placement, ...getFloatingProps(), children: (0, jsx_runtime_1.jsx)("div", { className: b(null, className), style: style, "data-qa": qa, children: children }) }) })) : null }));
};
exports.FloatingPopup = FloatingPopup;
//# sourceMappingURL=FloatingPopup.js.map
