"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReactSelectMenuWithPopper = void 0;
var react_1 = __importStar(require("react"));
var prop_types_1 = __importDefault(require("prop-types"));
var ramda_1 = require("ramda");
var nanoid_1 = require("nanoid");
var Paper_1 = __importDefault(require("@mui/material/Paper"));
var PopupBoundariesContext_1 = require("../contexts/PopupBoundariesContext");
var Popper_1 = require("../Popper");
var styles_1 = require("./styles");
var DEFAULT_MAX_WIDTH = 521;
var ReactSelectMenuWithPopper = function (props) {
    var _a = props.selectProps, inputRef = _a.inputRef, menuIsOpen = _a.menuIsOpen, inputValue = _a.inputValue, innerRef = props.innerRef, children = props.children, innerProps = props.innerProps;
    var _b = (0, react_1.useState)('id'), key = _b[0], setKey = _b[1];
    (0, react_1.useEffect)(function () {
        setKey((0, nanoid_1.nanoid)());
    }, [inputValue]);
    var styles = (0, styles_1.useStyles)();
    var popupBoundariesContext = (0, react_1.useContext)(PopupBoundariesContext_1.PopupBoundariesContext);
    var hasBoundariesElement = Boolean(popupBoundariesContext === null || popupBoundariesContext === void 0 ? void 0 : popupBoundariesContext.element);
    var boundariesWidth = hasBoundariesElement ? popupBoundariesContext.element.clientWidth : window.innerWidth;
    return (react_1.default.createElement(Popper_1.Popper, { anchorEl: inputRef.current, open: menuIsOpen, className: styles.popper, key: key, modifiers: [
            {
                name: 'preventOverflow',
                enabled: true,
                options: {
                    altAxis: true,
                    altBoundary: true,
                    tether: false,
                    boundary: hasBoundariesElement ? popupBoundariesContext.element : document.body,
                    padding: 5
                }
            }
        ], placement: "bottom-start" },
        react_1.default.createElement(Paper_1.default, __assign({ ref: innerRef, style: {
                minWidth: Math.min((0, ramda_1.prop)('clientWidth', inputRef.current), boundariesWidth),
                maxWidth: Math.min(DEFAULT_MAX_WIDTH, boundariesWidth)
            } }, innerProps), children)));
};
exports.ReactSelectMenuWithPopper = ReactSelectMenuWithPopper;
exports.ReactSelectMenuWithPopper.propTypes = {
    children: prop_types_1.default.oneOfType([prop_types_1.default.element, prop_types_1.default.array]).isRequired,
    selectProps: prop_types_1.default.shape({
        inputRef: prop_types_1.default.object.isRequired,
        menuIsOpen: prop_types_1.default.bool.isRequired,
        inputValue: prop_types_1.default.string
    }).isRequired,
    innerRef: prop_types_1.default.oneOfType([
        prop_types_1.default.oneOf([null]),
        prop_types_1.default.func,
        prop_types_1.default.shape({
            current: prop_types_1.default.any
        })
    ]).isRequired,
    innerProps: prop_types_1.default.object.isRequired
};
