function _array_like_to_array(arr, len) {
    if (len == null || len > arr.length) len = arr.length;
    for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
    return arr2;
}
function _array_without_holes(arr) {
    if (Array.isArray(arr)) return _array_like_to_array(arr);
}
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 _iterable_to_array(iter) {
    if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}
function _non_iterable_spread() {
    throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
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;
}
function _to_consumable_array(arr) {
    return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
}
function _unsupported_iterable_to_array(o, minLen) {
    if (!o) return;
    if (typeof o === "string") return _array_like_to_array(o, minLen);
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if (n === "Object" && o.constructor) n = o.constructor.name;
    if (n === "Map" || n === "Set") return Array.from(n);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
import { flip, shift, useFloating, FloatingPortal, offset as offsetMiddleware, autoUpdate } from "@floating-ui/react";
import React, { forwardRef } from "react";
import { safeUseId } from "../../utils";
import { FloatingContent, FloatingTarget, FloatingWrapper } from "./TimePicker.styles";
var getFloatingPortalProps = function getFloatingPortalProps(portal, wrappedId) {
    if (!portal) {
        return {
            id: wrappedId
        };
    }
    if (typeof portal === 'string') {
        return {
            id: portal
        };
    }
    return {
        root: portal
    };
};
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, offset = param.offset, zIndex = param.zIndex, disableFlip = param.disableFlip;
    var _ref, _ref1;
    var _useFloating = useFloating({
        whileElementsMounted: function whileElementsMounted(referenceEl, floatingEl, update) {
            return autoUpdate(referenceEl, floatingEl, update, {
                ancestorScroll: false,
                ancestorResize: false,
                layoutShift: false
            });
        },
        placement: placement,
        open: opened,
        middleware: [
            offsetMiddleware({
                mainAxis: (_ref = offset === null || offset === void 0 ? void 0 : offset[1]) !== null && _ref !== void 0 ? _ref : 0,
                crossAxis: (_ref1 = offset === null || offset === void 0 ? void 0 : offset[0]) !== null && _ref1 !== void 0 ? _ref1 : 0
            })
        ].concat(_to_consumable_array(disableFlip ? [] : [
            flip({
                fallbackAxisSideDirection: 'end'
            })
        ]), [
            shift()
        ])
    }), refs = _useFloating.refs, floatingStyles = _useFloating.floatingStyles;
    var wrappedId = safeUseId();
    var handleClick = function handleClick() {
        if (onToggle) {
            onToggle();
        }
    };
    // Проверка на target. Это может быть как ReactNode, так и функция, в которую пробрасывается ref.
    // Это нужно для более тонкой настройки reference-элемента, вокруг которого и будет позиционироваться выпадашка.
    // Пример: когда в Textfield под инпутом находится helperText (или еще что-либо),
    // но выпадающий список должен позиционироваться непосредственно возле самого инпута.
    var isTargetAsFunction = typeof target === 'function';
    return /*#__PURE__*/ React.createElement(FloatingWrapper, {
        ref: ref,
        id: wrappedId,
        style: {
            position: 'relative'
        }
    }, /*#__PURE__*/ React.createElement(FloatingTarget, {
        ref: isTargetAsFunction ? undefined : refs.setReference,
        onClick: handleClick
    }, typeof target === 'function' ? target(refs.setReference) : target), opened && /*#__PURE__*/ React.createElement(FloatingPortal, getFloatingPortalProps(portal, wrappedId), /*#__PURE__*/ React.createElement(FloatingContent, {
        ref: refs.setFloating,
        style: _object_spread_props(_object_spread({}, floatingStyles), {
            zIndex: zIndex || 1000
        })
    }, children)));
});
export { FloatingPopover };
