"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AnchorTab = exports.UIAnchorTab = void 0;
var aurelia_framework_1 = require("aurelia-framework");
var UIAnchorTab = exports.UIAnchorTab = (function () {
    function UIAnchorTab() {
        this.useParentHeight = false;
        this.scroll = true;
    }
    UIAnchorTab.prototype.bind = function () {
        this.tabList.forEach(function (x) { return x.isSelected = false; });
        var tabVisible = this.tabList.find(function (x) { return x.isVisible; });
        if (tabVisible != null) {
            tabVisible.isSelected = true;
        }
    };
    UIAnchorTab.prototype.attached = function () {
        var _this = this;
        this.tabList.forEach(function (tab) {
            tab.element = _this.refElement.ownerDocument.getElementById(tab.id);
        });
        this.refElement.addEventListener('scroll', function () {
            var containerElement = _this.containerElement();
            var scrollPercentage = Math.round(containerElement.scrollTop / (containerElement.scrollHeight - containerElement.clientHeight) * 100);
            _this.tabList.forEach(function (tab) {
                var _a;
                if (tab.isVisible) {
                    var position = (_a = tab.element) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect();
                    if (position != null && position.top <= Math.round((containerElement.clientHeight / 100) * scrollPercentage)) {
                        _this.tabList.find(function (x) { return x.isSelected; }).isSelected = false;
                        tab.isSelected = true;
                    }
                }
            });
        });
    };
    UIAnchorTab.prototype.navigateToAnchor = function (tab) {
        if (this.scroll && this.mustScroll()) {
            document.getElementById(tab.id).scrollIntoView({ behavior: "smooth", block: "start" });
        }
        else {
            this.tabList.find(function (x) { return x.isSelected; }).isSelected = false;
            tab.isSelected = true;
        }
    };
    UIAnchorTab.prototype.mustScroll = function () {
        var containerElement = this.containerElement();
        return containerElement.scrollHeight > containerElement.clientHeight;
    };
    UIAnchorTab.prototype.containerElement = function () {
        return this.useParentHeight ? this.refElement.parentElement : this.refElement;
    };
    __decorate([
        aurelia_framework_1.bindable,
        __metadata("design:type", Array)
    ], UIAnchorTab.prototype, "tabList", void 0);
    __decorate([
        aurelia_framework_1.bindable,
        __metadata("design:type", HTMLElement)
    ], UIAnchorTab.prototype, "refElement", void 0);
    __decorate([
        aurelia_framework_1.bindable,
        __metadata("design:type", Boolean)
    ], UIAnchorTab.prototype, "useParentHeight", void 0);
    __decorate([
        aurelia_framework_1.bindable,
        __metadata("design:type", Boolean)
    ], UIAnchorTab.prototype, "scroll", void 0);
    UIAnchorTab = __decorate([
        (0, aurelia_framework_1.customElement)("ui-anchor-tab"),
        aurelia_framework_1.autoinject
    ], UIAnchorTab);
    return UIAnchorTab;
}());
var AnchorTab = (function () {
    function AnchorTab(id, name, isVisible) {
        if (isVisible === void 0) { isVisible = true; }
        this.id = id;
        this.name = name;
        this.isVisible = isVisible;
    }
    return AnchorTab;
}());
exports.AnchorTab = AnchorTab;

//# sourceMappingURL=ui-anchor-tab.js.map
