import * as tslib_1 from "tslib";
import { Component, Input, Output, EventEmitter, ViewEncapsulation, ChangeDetectionStrategy, TemplateRef } from '@angular/core';
var TreeComponent = /** @class */ (function () {
    function TreeComponent() {
        /** tree node selection event */
        this.onNodeSelect = new EventEmitter();
        /** tree node expand event */
        this.onExpandNode = new EventEmitter();
        /**
         * input tree-nodes value
         * @default []
         */
        this.treeData = [];
        /**
         * Whether is disabled or not
         * @default false
         */
        this.disabled = false;
        /**
         * Whether upward propagation events, default: true
         * @default true
         */
        this.propagateSelectionUp = true;
        /**
         * Whether downward propagation events, default: true
         * @default true
         */
        this.propagateSelectionDown = true;
        /** private variable tree-list
         *  @docs-private
         */
        this._treeList = [];
    }
    TreeComponent.prototype.ngOnInit = function () {
        if (this.selectionMode) {
            this.transferTreeToList(this.treeData);
        }
    };
    /**
     * transfer tree to list
     * @docs-private
     */
    TreeComponent.prototype.transferTreeToList = function (treeData) {
        var _this = this;
        if (treeData.length) {
            treeData.forEach(function (node) {
                _this._treeList.push(node);
                if (node.children && node.children.length) {
                    _this.transferTreeToList(node.children);
                }
            });
        }
    };
    /**
     * tree node expand event
     * @docs-private
     */
    TreeComponent.prototype.expandNode = function (event) {
        this.onExpandNode.emit(event);
    };
    /**
     * listen tree-node click event
     * @docs-private
     */
    TreeComponent.prototype.onNodeClick = function (node) {
        if (!node.selectable || this.disabled) {
            return;
        }
        switch (this.selectionMode) {
            case 'candidate':
                this.clickCandidate(node);
                break;
            case 'selected':
                this.clickSelected(node);
                break;
            case 'navigation':
                this.clickNavigation(node);
                break;
            default:
                return;
        }
    };
    /**
     * candidate mode click event handler
     * @docs-private
     */
    TreeComponent.prototype.clickCandidate = function (node) {
        if (!node.isSelected) {
            if (this.propagateSelectionDown) {
                this.propagateDown(node, true);
            }
            if (this.propagateSelectionUp) {
                this.propagateUp(node, true);
            }
            this.onNodeSelect.emit(node);
        }
    };
    /**
     * selected mode click event handler
     * @docs-private
     */
    TreeComponent.prototype.clickSelected = function (node) {
        if (node.isSelected || node.isPartSelected) {
            if (this.propagateSelectionDown) {
                this.propagateDown(node, false);
            }
            if (this.propagateSelectionUp) {
                this.propagateUp(node, false);
            }
            this.onNodeSelect.emit(node);
        }
    };
    /**
     * click navigation node
     * @param node
     * @docs-private
     */
    TreeComponent.prototype.clickNavigation = function (node) {
        this.onNodeSelect.emit(node);
    };
    /**
     * select event downward propagate handler
     * @docs-private
     */
    TreeComponent.prototype.propagateDown = function (node, select) {
        node.isSelected = select;
        node.show = select;
        if (node.children && node.children.length) {
            for (var _i = 0, _a = node.children; _i < _a.length; _i++) {
                var child = _a[_i];
                this.propagateDown(child, select);
            }
        }
    };
    /**
     * select event upward propagate handler
     * @docs-private
     */
    TreeComponent.prototype.propagateUp = function (node, select) {
        node.isSelected = select;
        node.show = select;
        if (node.parent) {
            var nodeParent = void 0;
            nodeParent = this.getParent(node.parent);
            if (nodeParent && nodeParent.children && nodeParent.children.length) {
                var selectedCount = 0;
                for (var _i = 0, _a = nodeParent.children; _i < _a.length; _i++) {
                    var child = _a[_i];
                    if (child.isSelected === select || (child.isSelected === undefined && !child.selectable)) {
                        selectedCount++;
                    }
                }
                if (nodeParent.children.length === selectedCount) {
                    nodeParent.isSelected = select;
                    nodeParent.show = select;
                }
            }
            if (nodeParent && nodeParent.isSelected === select && nodeParent.parent) {
                this.propagateUp(nodeParent, select);
            }
        }
    };
    /**
     * get tree-node parent tree-node
     * @docs-private
     */
    TreeComponent.prototype.getParent = function (treeNodeParent) {
        var parentNode;
        parentNode = this._treeList.find(function (node) {
            return treeNodeParent.id === node.id;
        });
        return parentNode;
    };
    tslib_1.__decorate([
        Output(),
        tslib_1.__metadata("design:type", Object)
    ], TreeComponent.prototype, "onNodeSelect", void 0);
    tslib_1.__decorate([
        Output(),
        tslib_1.__metadata("design:type", Object)
    ], TreeComponent.prototype, "onExpandNode", void 0);
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", Array)
    ], TreeComponent.prototype, "treeData", void 0);
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", String)
    ], TreeComponent.prototype, "selectionMode", void 0);
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", Boolean)
    ], TreeComponent.prototype, "disabled", void 0);
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", Boolean)
    ], TreeComponent.prototype, "propagateSelectionUp", void 0);
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", Boolean)
    ], TreeComponent.prototype, "propagateSelectionDown", void 0);
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", TemplateRef)
    ], TreeComponent.prototype, "optionTpl", void 0);
    TreeComponent = tslib_1.__decorate([
        Component({
            selector: 'nb-tree',
            template: "<ul class=\"nb-tree-node-list\" [ngClass]=\"{'nb-tree-selectable':selectionMode}\" [class.nb-tree-mode-candidate]=\"selectionMode==='candidate'\" [class.nb-tree-mode-selected]=\"selectionMode==='selected'\" [class.nb-tree-mode-navigation]=\"selectionMode==='navigation'\" [class.nb-tree-disabled]=\"disabled\"><nb-tree-node *ngFor=\"let node of treeData\" [node]=\"node\" (onExpandNode)=\"expandNode($event)\" [optionTpl]=\"optionTpl\"></nb-tree-node></ul>",
            encapsulation: ViewEncapsulation.None,
            changeDetection: ChangeDetectionStrategy.OnPush,
            preserveWhitespaces: false,
            host: {
                'class': 'nb-widget nb-tree'
            }
        }),
        tslib_1.__metadata("design:paramtypes", [])
    ], TreeComponent);
    return TreeComponent;
}());
export { TreeComponent };
//# sourceMappingURL=tree.js.map