"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "FloatingPopover", {
    enumerable: true,
    get: function() {
        return FloatingPopover;
    }
});
const _react = require("@floating-ui/react");
const _react1 = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _utils = require("../../utils");
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;
}
// TODO: #2003
const LIST_PADDING = 2;
const FloatingPopover = /*#__PURE__*/ (0, _react1.forwardRef)(({ target, children, opened, onToggle, placement, portal, offset, isInner, trigger, zIndex }, ref)=>{
    const { refs, floatingStyles, context } = (0, _react.useFloating)({
        whileElementsMounted (referenceEl, floatingEl, update) {
            return (0, _react.autoUpdate)(referenceEl, floatingEl, update, {
                ancestorScroll: false,
                ancestorResize: false,
                layoutShift: false
            });
        },
        placement: placement === 'auto' ? undefined : placement,
        open: opened,
        onOpenChange: onToggle,
        middleware: [
            placement === 'auto' && (0, _react.autoPlacement)(),
            (0, _react.offset)({
                mainAxis: isInner ? LIST_PADDING * 2 : offset?.[1],
                crossAxis: isInner ? -LIST_PADDING : offset?.[0]
            }),
            (0, _react.flip)({
                fallbackAxisSideDirection: 'end'
            }),
            (0, _react.shift)()
        ]
    });
    const wrappedId = (0, _utils.safeUseId)();
    const hover = (0, _react.useHover)(context, {
        enabled: trigger === 'hover',
        delay: {
            open: 0,
            close: 50
        },
        handleClose: (0, _react.safePolygon)({
            requireIntent: false
        })
    });
    const click = (0, _react.useClick)(context, {
        stickIfOpen: false,
        ignoreMouse: trigger === 'hover'
    });
    const { getReferenceProps } = (0, _react.useInteractions)([
        hover,
        click
    ]);
    return /*#__PURE__*/ _react1.default.createElement("div", {
        ref: ref,
        id: wrappedId,
        style: {
            position: 'relative',
            display: isInner ? 'block' : 'inline-block'
        }
    }, /*#__PURE__*/ _react1.default.createElement("div", {
        ref: refs.setReference,
        ...getReferenceProps()
    }, target), opened && /*#__PURE__*/ _react1.default.createElement(_react.FloatingPortal, getFloatingPortalProps(portal, wrappedId), /*#__PURE__*/ _react1.default.createElement("div", {
        ref: refs.setFloating,
        style: {
            ...floatingStyles,
            zIndex
        }
    }, children)));
});
// root - принимает ref контейнера портала.
// id - если есть портал - не используется, если портала нет - подставляется 'wrappedId'.
const getFloatingPortalProps = (portal, wrappedId)=>{
    if (!portal) {
        return {
            id: wrappedId
        };
    }
    if (typeof portal === 'string') {
        return {
            id: portal
        };
    }
    return {
        root: portal
    };
};
