var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
import React from 'react';
import { ThemeFactory } from '../../lib/theming/ThemeFactory.js';
import { Popup } from '../../internal/Popup/index.js';
import { ComboBoxMenu } from '../../internal/CustomComboBox/index.js';
import { ThemeContext } from '../../lib/theming/ThemeContext.js';
import { TokenInputDataTids } from './TokenInput.js';
var TokenInputMenu = /** @class */ (function (_super) {
    __extends(TokenInputMenu, _super);
    function TokenInputMenu() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = {};
        _this.menu = null;
        _this.getMenuRef = function () { return _this.menu; };
        _this.handleMenuPositionChange = function (_, isFullyVisible) {
            if (!_this.state.forceMenuLeftAlign && !isFullyVisible && _this.props.menuAlign === 'cursor') {
                _this.setState({ forceMenuLeftAlign: true });
            }
        };
        _this.getPopupMargin = function () {
            var paddingY = parseInt(_this.theme.tokenInputPaddingYSmall, 10) || 0;
            var outlineWidth = parseInt(_this.theme.controlOutlineWidth, 10) || 0;
            var marginY = parseInt(_this.theme.tokenMarginYSmall, 10) || 0;
            return paddingY + outlineWidth + marginY;
        };
        _this.menuRef = function (node) { return (_this.menu = node); };
        return _this;
    }
    TokenInputMenu.prototype.render = function () {
        var _this = this;
        return (React.createElement(ThemeContext.Consumer, null, function (theme) {
            _this.theme = theme;
            return (React.createElement(ThemeContext.Provider, { value: ThemeFactory.create({
                    popupMargin: _this.getPopupMargin() + 'px',
                    popupBackground: _this.theme.tokenInputMenuPopupBg,
                }, theme) }, _this.renderMain()));
        }));
    };
    TokenInputMenu.prototype.getPopupMarginSize = function (t) {
        switch (this.props.size) {
            case 'large':
                return t.tokenInputPopupMarginLarge;
            case 'medium':
                return t.tokenInputPopupMarginMedium;
            case 'small':
            default:
                return t.tokenInputPopupMarginSmall;
        }
    };
    TokenInputMenu.prototype.renderMain = function () {
        var _a = this.props, loading = _a.loading, maxMenuHeight = _a.maxMenuHeight, renderTotalCount = _a.renderTotalCount, totalCount = _a.totalCount, opened = _a.opened, items = _a.items, renderNotFound = _a.renderNotFound, renderItem = _a.renderItem, onValueChange = _a.onValueChange, renderAddButton = _a.renderAddButton, anchorElementForCursor = _a.anchorElementForCursor, anchorElementRoot = _a.anchorElementRoot, menuWidth = _a.menuWidth;
        var menuAlign = this.state.forceMenuLeftAlign ? 'left' : this.props.menuAlign;
        return (React.createElement(Popup, { id: this.props.popupMenuId, "data-tid": TokenInputDataTids.tokenInputMenu, opened: !!opened, positions: menuAlign === 'cursor'
                ? ['bottom left', 'top left']
                : ['bottom left', 'top left', 'bottom right', 'top right'], anchorElement: menuAlign === 'cursor' ? anchorElementForCursor : anchorElementRoot, popupOffset: menuAlign === 'left'
                ? parseInt(this.theme.tokenInputPopupOffset)
                : 8 + parseInt(this.theme.tokenInputPopupOffset), margin: menuAlign === 'left' ? 1 : parseInt(this.getPopupMarginSize(this.theme)), hasShadow: true, width: menuAlign === 'cursor' ? 'auto' : menuWidth, withoutMobile: true, onPositionChange: this.handleMenuPositionChange },
            React.createElement(ComboBoxMenu, { size: this.props.size, items: items, loading: loading, hasMargin: false, maxMenuHeight: maxMenuHeight, onValueChange: onValueChange, opened: opened, refMenu: this.menuRef, renderTotalCount: renderTotalCount, renderItem: renderItem, renderNotFound: renderNotFound, totalCount: totalCount, renderAddButton: renderAddButton })));
    };
    TokenInputMenu.__KONTUR_REACT_UI__ = 'TokenInputMenu';
    TokenInputMenu.displayName = 'TokenInputMenu';
    return TokenInputMenu;
}(React.Component));
export { TokenInputMenu };
//# sourceMappingURL=TokenInputMenu.js.map