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 'select2';
var AttachBody = jQuery.fn.select2.amd.require('select2/dropdown/attachBody');
var customAttachBody = (function (_super) {
    __extends(customAttachBody, _super);
    function customAttachBody(decorated, $element, options) {
        return _super.call(this, decorated, $element, options) || this;
    }
    customAttachBody.prototype._resizeDropdown = function () {
        var _a;
        var uiSelectParent = null;
        if (((_a = this.$container.parent().get(0)) === null || _a === void 0 ? void 0 : _a.tagName.toLocaleLowerCase()) == "ui-select") {
            uiSelectParent = this.$container.parent();
        }
        var css = {
            width: (uiSelectParent == null ? this.$container.outerWidth(false) : uiSelectParent.outerWidth(false)) + 'px'
        };
        if (this.options.get('dropdownAutoWidth')) {
            css.minWidth = css.width;
            css.position = 'relative';
            css.width = 'auto';
        }
        this.$dropdown.css(css);
    };
    ;
    customAttachBody.prototype._positionDropdown = function () {
        var _a;
        var $window = $(window);
        var isCurrentlyAbove = this.$dropdown[0].classList
            .contains('select2-dropdown--above');
        var isCurrentlyBelow = this.$dropdown[0].classList
            .contains('select2-dropdown--below');
        var newDirection = null;
        var uiSelectParent = null;
        if (((_a = this.$container.parent().get(0)) === null || _a === void 0 ? void 0 : _a.tagName.toLocaleLowerCase()) == "ui-select") {
            uiSelectParent = this.$container.parent();
        }
        var offset = this.$container.offset();
        offset.bottom = offset.top + this.$container.outerHeight(false);
        var container = {
            height: this.$container.outerHeight(false)
        };
        container.top = offset.top;
        container.bottom = offset.top + container.height;
        var dropdown = {
            height: this.$dropdown.outerHeight(false)
        };
        var viewport = {
            top: $window.scrollTop(),
            bottom: $window.scrollTop() + $window.height()
        };
        var enoughRoomAbove = viewport.top < (offset.top - dropdown.height);
        var enoughRoomBelow = viewport.bottom > (offset.bottom + dropdown.height);
        var css = {
            left: uiSelectParent == null ? offset.left : uiSelectParent.offset().left,
            top: container.bottom
        };
        var $offsetParent = this.$dropdownParent;
        if ($offsetParent.css('position') === 'static') {
            $offsetParent = $offsetParent.offsetParent();
        }
        var parentOffset = {
            top: 0,
            left: 0
        };
        if ($.contains(document.body, $offsetParent[0]) ||
            $offsetParent[0].isConnected) {
            parentOffset = $offsetParent.offset();
        }
        css.top -= parentOffset.top;
        css.left -= parentOffset.left;
        if (!isCurrentlyAbove && !isCurrentlyBelow) {
            newDirection = 'below';
        }
        if (!enoughRoomBelow && enoughRoomAbove && !isCurrentlyAbove) {
            newDirection = 'above';
        }
        else if (!enoughRoomAbove && enoughRoomBelow && isCurrentlyAbove) {
            newDirection = 'below';
        }
        if (newDirection == 'above' ||
            (isCurrentlyAbove && newDirection !== 'below')) {
            css.top = container.top - parentOffset.top - dropdown.height;
        }
        if (newDirection != null) {
            this.$dropdown[0].classList.remove('select2-dropdown--below');
            this.$dropdown[0].classList.remove('select2-dropdown--above');
            this.$dropdown[0].classList.add('select2-dropdown--' + newDirection);
            this.$container[0].classList.remove('select2-container--below');
            this.$container[0].classList.remove('select2-container--above');
            this.$container[0].classList.add('select2-container--' + newDirection);
        }
        this.$dropdownContainer.css(css);
    };
    ;
    return customAttachBody;
}(AttachBody));
export { customAttachBody };

//# sourceMappingURL=custom-attach-body.js.map
