"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");
var _TimePickerstyles = require("./TimePicker.styles");
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 _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 _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);
}
var getFloatingPortalProps = function getFloatingPortalProps(portal, wrappedId) {
    if (!portal) {
        return {
            id: wrappedId
        };
    }
    if (typeof portal === 'string') {
        return {
            id: portal
        };
    }
    return {
        root: portal
    };
};
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, offset = param.offset, zIndex = param.zIndex, disableFlip = param.disableFlip;
    var _ref, _ref1;
    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,
        open: opened,
        middleware: [
            (0, _react.offset)({
                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 ? [] : [
            (0, _react.flip)({
                fallbackAxisSideDirection: 'end'
            })
        ]), [
            (0, _react.shift)()
        ])
    }), refs = _useFloating.refs, floatingStyles = _useFloating.floatingStyles;
    var wrappedId = (0, _utils.safeUseId)();
    var handleClick = function handleClick() {
        if (onToggle) {
            onToggle();
        }
    };
    // Проверка на target. Это может быть как ReactNode, так и функция, в которую пробрасывается ref.
    // Это нужно для более тонкой настройки reference-элемента, вокруг которого и будет позиционироваться выпадашка.
    // Пример: когда в Textfield под инпутом находится helperText (или еще что-либо),
    // но выпадающий список должен позиционироваться непосредственно возле самого инпута.
    var isTargetAsFunction = typeof target === 'function';
    return /*#__PURE__*/ _react1.default.createElement(_TimePickerstyles.FloatingWrapper, {
        ref: ref,
        id: wrappedId,
        style: {
            position: 'relative'
        }
    }, /*#__PURE__*/ _react1.default.createElement(_TimePickerstyles.FloatingTarget, {
        ref: isTargetAsFunction ? undefined : refs.setReference,
        onClick: handleClick
    }, typeof target === 'function' ? target(refs.setReference) : target), opened && /*#__PURE__*/ _react1.default.createElement(_react.FloatingPortal, getFloatingPortalProps(portal, wrappedId), /*#__PURE__*/ _react1.default.createElement(_TimePickerstyles.FloatingContent, {
        ref: refs.setFloating,
        style: _object_spread_props(_object_spread({}, floatingStyles), {
            zIndex: zIndex || 1000
        })
    }, children)));
});
