"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.LabelMenu = exports.UISidebar = void 0;
var aurelia_framework_1 = require("aurelia-framework");
var ui_internal_1 = require("../../utils/ui-internal");
var UISidebar = exports.UISidebar = (function () {
    function UISidebar(element, bindingEngine) {
        var _this = this;
        this.element = element;
        this.bindingEngine = bindingEngine;
        this.position = "start";
        this.width = "24rem";
        this.maxWidth = "40vw";
        this.minWidth = "4rem";
        this.align = "top";
        this.headTrigger = "peek";
        this.collapsed = false;
        this.peek = false;
        this.resizeable = false;
        this.collapsible = false;
        this.closeOnClick = false;
        this.startX = 0;
        this.isResizing = false;
        this.doResize = function (e) { return _this.resize(e); };
        this.endResize = function () { return _this.stopResize(); };
        this.resizeable = element.hasAttribute("resizeable");
        this.collapsible = element.hasAttribute("collapsible");
        this.closeOnClick = element.hasAttribute("close-on-click") && !isFalse(element.getAttribute("close-on-click"));
        this.obClick = ui_internal_1.UIInternal.subscribe(ui_internal_1.UIInternal.EVT_VIEWPORT_CLICK, function (target) {
            return !_this.peek || (!_this.closeOnClick && hasParent(target, "ui-sidebar__body", "ui-sidebar"))
                ? undefined
                : ui_internal_1.UIInternal.queueTask(function () { return (_this.peek = false); });
        });
        this.disposable = new Array();
    }
    UISidebar.prototype.bind = function () {
        var _this = this;
        this.convertLabel();
        this.getLastLabel();
        if (this.labels != null) {
            this.disposable.push(this.bindingEngine.collectionObserver(this.labels).subscribe(function (changeRecords) {
                _this.getLastLabel();
            }));
        }
    };
    UISidebar.prototype.labelChanged = function (newValue, oldValue) {
        if (newValue != oldValue) {
            if (oldValue != null && this.labels != null) {
                var oldLabel = this.labels.find(function (x) { return x.uniqueIdentifiant == oldValue; });
                if (oldLabel != null) {
                    this.labels.remove(oldLabel);
                }
            }
            this.convertLabel();
        }
    };
    UISidebar.prototype.convertLabel = function () {
        if (this.label != null) {
            this.labels = [];
            this.labels.push({
                name: this.label,
                isSelected: true,
                uniqueIdentifiant: this.label
            });
        }
    };
    UISidebar.prototype.currentLabelSelectedChanged = function (newValue, oldValue) {
        var _a;
        if (newValue == undefined) {
            this.currentLabelSelected = (_a = this.labels) === null || _a === void 0 ? void 0 : _a.last();
        }
    };
    UISidebar.prototype.getLastLabel = function () {
        var _a, _b, _c;
        if (((_a = this.labels) === null || _a === void 0 ? void 0 : _a.find(function (x) { return x.isSelected == true; })) == null) {
            if (((_b = this.labels) === null || _b === void 0 ? void 0 : _b.length) > 0) {
                this.labels.last().isSelected = true;
            }
        }
        this.currentLabelSelected = (_c = this.labels) === null || _c === void 0 ? void 0 : _c.find(function (x) { return x.isSelected; });
    };
    UISidebar.prototype.detached = function () {
        if (this.obClick) {
            this.obClick.dispose();
        }
    };
    Object.defineProperty(UISidebar.prototype, "toggleIcon", {
        get: function () {
            return "".concat(this.collapsed ? "expand" : "collapse", "-").concat(this.position);
        },
        enumerable: false,
        configurable: true
    });
    UISidebar.prototype.startResize = function ($event) {
        if ($event.button === 0 && !this.isResizing) {
            this.startX = $event.x || $event.clientX;
            this.isResizing = true;
            document.addEventListener("mousemove", this.doResize);
            document.addEventListener("mouseup", this.endResize);
            $event.stopEvent();
        }
    };
    UISidebar.prototype.resize = function ($event) {
        if (this.isResizing) {
            var diff = ($event.x || $event.clientX) - this.startX;
            if (this.position === "end") {
                diff = -1 * diff;
            }
            if (isRtl(this.element)) {
                diff = -1 * diff;
            }
            var newWidth = this.bodyEl.offsetWidth + diff;
            if (newWidth <= (this.maxWidth == "none" ? Number.MAX_SAFE_INTEGER : convertToPx(this.maxWidth)) && newWidth >= convertToPx(this.minWidth)) {
                this.width = newWidth + "px";
                this.startX = $event.x || $event.clientX;
            }
            $event.stopEvent();
        }
    };
    UISidebar.prototype.stopResize = function () {
        this.isResizing = false;
        document.removeEventListener("mousemove", this.doResize);
        document.removeEventListener("mouseup", this.endResize);
        this.element.dispatchEvent(ui_internal_1.UIInternal.createEvent("resizeEnd"));
    };
    UISidebar.prototype.selected = function (labelMenu) {
        this.labels.forEach(function (x) { return x.isSelected = false; });
        labelMenu.isSelected = true;
        this.currentLabelSelected = labelMenu;
        if (this.collapsible && this.collapsed) {
            this.collapsed = false;
        }
    };
    __decorate([
        (0, aurelia_framework_1.bindable)(),
        __metadata("design:type", String)
    ], UISidebar.prototype, "position", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)(),
        __metadata("design:type", String)
    ], UISidebar.prototype, "width", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)(),
        __metadata("design:type", String)
    ], UISidebar.prototype, "maxWidth", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)(),
        __metadata("design:type", String)
    ], UISidebar.prototype, "minWidth", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)(),
        __metadata("design:type", String)
    ], UISidebar.prototype, "titleBg", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)(),
        __metadata("design:type", String)
    ], UISidebar.prototype, "titleColor", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)(),
        __metadata("design:type", String)
    ], UISidebar.prototype, "titleWeight", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)(),
        __metadata("design:type", String)
    ], UISidebar.prototype, "align", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)(),
        __metadata("design:type", String)
    ], UISidebar.prototype, "headTrigger", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)({ defaultBindingMode: aurelia_framework_1.bindingMode.twoWay }),
        __metadata("design:type", Boolean)
    ], UISidebar.prototype, "collapsed", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)(),
        __metadata("design:type", String)
    ], UISidebar.prototype, "label", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)(),
        __metadata("design:type", Array)
    ], UISidebar.prototype, "labels", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)({ defaultBindingMode: aurelia_framework_1.bindingMode.twoWay }),
        __metadata("design:type", LabelMenu)
    ], UISidebar.prototype, "currentLabelSelected", void 0);
    __decorate([
        (0, aurelia_framework_1.computedFrom)("collapsed", "position"),
        __metadata("design:type", Object),
        __metadata("design:paramtypes", [])
    ], UISidebar.prototype, "toggleIcon", null);
    UISidebar = __decorate([
        (0, aurelia_framework_1.customElement)("ui-sidebar"),
        __metadata("design:paramtypes", [Element, aurelia_framework_1.BindingEngine])
    ], UISidebar);
    return UISidebar;
}());
var LabelMenu = (function () {
    function LabelMenu() {
    }
    return LabelMenu;
}());
exports.LabelMenu = LabelMenu;

//# sourceMappingURL=ui-sidebar.js.map
