function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
function _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
function ownKeys(object, enumerableOnly) {
    var keys = Object.keys(object);
    if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);
        if (enumerableOnly) {
            symbols = symbols.filter(function(sym) {
                return Object.getOwnPropertyDescriptor(object, sym).enumerable;
            });
        }
        keys.push.apply(keys, symbols);
    }
    return keys;
}
function _object_spread_props(target, source) {
    source = source != null ? source : {};
    if (Object.getOwnPropertyDescriptors) {
        Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
    } else {
        ownKeys(Object(source)).forEach(function(key) {
            Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
        });
    }
    return target;
}
import { flip, shift, useFloating, FloatingPortal, offset as offsetMiddleware, autoPlacement, autoUpdate, useHover, useInteractions, safePolygon, useClick } from "@floating-ui/react";
import React, { forwardRef } from "react";
import { safeUseId } from "../../utils";
// TODO: #2003
var LIST_PADDING = 2;
var FloatingPopover = /*#__PURE__*/ forwardRef(function(param, ref) {
    var target = param.target, children = param.children, opened = param.opened, onToggle = param.onToggle, placement = param.placement, portal = param.portal, _param_offset = param.offset, offset = _param_offset === void 0 ? [
        0,
        8
    ] : _param_offset, isInner = param.isInner, trigger = param.trigger, zIndex = param.zIndex;
    var _useFloating = useFloating({
        whileElementsMounted: function whileElementsMounted(referenceEl, floatingEl, update) {
            return autoUpdate(referenceEl, floatingEl, update, {
                ancestorScroll: false,
                ancestorResize: false,
                layoutShift: false
            });
        },
        placement: placement === 'auto' ? undefined : placement,
        open: opened,
        onOpenChange: onToggle,
        middleware: [
            placement === 'auto' && autoPlacement(),
            offsetMiddleware({
                mainAxis: isInner ? LIST_PADDING * 2 : offset[1],
                crossAxis: isInner ? -LIST_PADDING : offset[0]
            }),
            flip({
                fallbackAxisSideDirection: 'end'
            }),
            shift()
        ]
    }), refs = _useFloating.refs, floatingStyles = _useFloating.floatingStyles, context = _useFloating.context;
    var wrappedId = safeUseId();
    var hover = useHover(context, {
        enabled: trigger === 'hover',
        delay: {
            open: 0,
            close: 50
        },
        handleClose: safePolygon({
            requireIntent: false
        })
    });
    var click = useClick(context, {
        stickIfOpen: false,
        ignoreMouse: trigger === 'hover'
    });
    var getReferenceProps = useInteractions([
        hover,
        click
    ]).getReferenceProps;
    return /*#__PURE__*/ React.createElement("div", {
        ref: ref,
        id: wrappedId,
        style: {
            position: 'relative',
            display: isInner ? 'block' : 'inline-block'
        }
    }, /*#__PURE__*/ React.createElement("div", _object_spread({
        ref: refs.setReference
    }, getReferenceProps()), target), opened && /*#__PURE__*/ React.createElement(FloatingPortal, getFloatingPortalProps(portal, wrappedId), /*#__PURE__*/ React.createElement("div", {
        ref: refs.setFloating,
        style: _object_spread_props(_object_spread({}, floatingStyles), {
            zIndex: zIndex || 1000
        })
    }, children)));
});
// root - принимает ref контейнера портала.
// id - если есть портал - не используется, если портала нет - подставляется 'wrappedId'.
var getFloatingPortalProps = function getFloatingPortalProps(portal, wrappedId) {
    if (!portal) {
        return {
            id: wrappedId
        };
    }
    if (typeof portal === 'string') {
        return {
            id: portal
        };
    }
    return {
        root: portal
    };
};
export { FloatingPopover };
