"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "FloatingPopover", {
    enumerable: true,
    get: function() {
        return FloatingPopover;
    }
});
var _react = require("@floating-ui/react");
var _react1 = /*#__PURE__*/ _interop_require_wildcard(require("react"));
var _utils = require("../../utils");
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 _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;
}
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;
}
// TODO: #2003
var LIST_PADDING = 2;
var FloatingPopover = /*#__PURE__*/ (0, _react1.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 = (0, _react.useFloating)({
        whileElementsMounted: function 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)()
        ]
    }), refs = _useFloating.refs, floatingStyles = _useFloating.floatingStyles, context = _useFloating.context;
    var wrappedId = (0, _utils.safeUseId)();
    var hover = (0, _react.useHover)(context, {
        enabled: trigger === 'hover',
        delay: {
            open: 0,
            close: 50
        },
        handleClose: (0, _react.safePolygon)({
            requireIntent: false
        })
    });
    var click = (0, _react.useClick)(context, {
        stickIfOpen: false,
        ignoreMouse: trigger === 'hover'
    });
    var getReferenceProps = (0, _react.useInteractions)([
        hover,
        click
    ]).getReferenceProps;
    return /*#__PURE__*/ _react1.default.createElement("div", {
        ref: ref,
        id: wrappedId,
        style: {
            position: 'relative',
            display: isInner ? 'block' : 'inline-block'
        }
    }, /*#__PURE__*/ _react1.default.createElement("div", _object_spread({
        ref: refs.setReference
    }, getReferenceProps()), target), opened && /*#__PURE__*/ _react1.default.createElement(_react.FloatingPortal, getFloatingPortalProps(portal, wrappedId), /*#__PURE__*/ _react1.default.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
    };
};
