import * as tslib_1 from "tslib";
import { Component, Input, Output, EventEmitter, ViewEncapsulation, ChangeDetectionStrategy, TemplateRef } from '@angular/core';
var SideBarComponent = /** @class */ (function () {
    function SideBarComponent() {
        /** click navi event */
        this.onNavi = new EventEmitter();
        /** toggle side-bar */
        this.toggle = new EventEmitter();
        // search-box option
        /** search-box placeholder */
        this.searchPlaceholder = '联想搜索';
        /** Use search recommendation or not */
        this.isSuggestion = true;
        /**
         * storage tree-nodes as list
         * @docs-private
         */
        this._listTreeNodes = [];
        /**
         * storage search matched tree-nodes as list
         * @docs-private
         */
        this._listSearchNodes = [];
        /**
         * tree title node
         * @docs-private
         */
        this.root = '';
        /**
         * side-bar tree data
         * @docs-private
         */
        this.treeData = [];
        /**
         * search-box suggestion content
         * @docs-private
         */
        this.suggestionValue = [];
        /**
         * side-bar tree-node expanded or not
         * @docs-private
         */
        this.expanded = true;
        /**
         * judge treeData has data or not
         * @docs-private
         */
        this.hasTreeData = true;
    }
    SideBarComponent.prototype.ngOnInit = function () {
        this.setSelectNodeId();
        this.initTreeNodeProperty();
        this.tempDataIndex();
    };
    /**
     * set selected node id
     * @docs-private
     */
    SideBarComponent.prototype.setSelectNodeId = function () {
        if (!this.selectedNodeId && this.data.tree && this.data.tree.length > 0) {
            var firstChild = void 0;
            firstChild = this.getFirstChild(this.data.tree[0]);
            if (firstChild) {
                this.selectedNodeId = firstChild.id;
                this.onNavi.emit(firstChild);
            }
        }
    };
    /**
     * side-bar static search fuzzy match
     * @param {string} keyWord 搜索的字符串片段
     * @param {string} fullWord 完整的字符串
     * @returns boolean 字符串片段在字符串中的位置
     */
    SideBarComponent.prototype.fuzzySearch = function (keyWord, fullWord) {
        return fullWord.toLowerCase().includes(keyWord.trim().toLowerCase());
    };
    /**
     * init tree node property
     * @docs-private
     */
    SideBarComponent.prototype.initTreeNodeProperty = function () {
        if (this.data.tree) {
            this.treeData = JSON.parse(JSON.stringify(this.data.tree));
            this.initTree(this.treeData, !!this.data.expanded);
            this.transferTreeToList(this.data.tree);
            this.hasTreeData = this.checkTreeData();
        }
    };
    /**
     * temp data index
     * @docs-private
     */
    SideBarComponent.prototype.tempDataIndex = function () {
        this.tempData = this.data;
    };
    /**
     * change data-tree when data soure change
     * @docs-private
     */
    SideBarComponent.prototype.changeTree = function () {
        if (this.tempData !== this.data) {
            this.selectedNodeId = '';
            this.setSelectNodeId();
            this.initTreeNodeProperty();
            this.tempDataIndex();
        }
        return this.treeData;
    };
    /**
     * init tree data
     * @docs-private
     */
    SideBarComponent.prototype.initTree = function (treeData, expanded) {
        var _this = this;
        if (treeData.length) {
            treeData.forEach(function (node) {
                node.isExpanded = expanded;
                node.selectable = node.selectable !== false ? true : false;
                node.show = true;
                if (_this.selectedNodeId === node.id) {
                    node.isSelected = true;
                    _this.selectedNode = node;
                }
                if (node.children && node.children.length) {
                    _this.initTree(node.children, expanded);
                }
            });
        }
    };
    /**
     * transfer tree to list
     * @docs-private
     */
    SideBarComponent.prototype.transferTreeToList = function (treeData) {
        var _this = this;
        if (treeData.length) {
            treeData.forEach(function (node) {
                node.isExpanded = false;
                node.selectable = node.selectable !== false ? true : false;
                node.show = false;
                _this._listTreeNodes.push(node);
                if (node.children && node.children.length) {
                    _this.transferTreeToList(node.children);
                }
            });
        }
    };
    /**
     * listen the search-box search keyword suggest event
     * @docs-private
     */
    SideBarComponent.prototype.searchSuggestion = function (event) {
        var suggestionNodes = [];
        suggestionNodes = this.getSuggestionNodes(event);
        this.suggestionValue = suggestionNodes.length ? suggestionNodes.map(function (node) { return node.name; }) : [];
    };
    /**
     * get matched keyword the suggest tree-node
     * @docs-private
     */
    SideBarComponent.prototype.getSuggestionNodes = function (event) {
        var listMatchedTreeNodes = [];
        for (var _i = 0, _a = this._listTreeNodes; _i < _a.length; _i++) {
            var node = _a[_i];
            if (node.name && this.fuzzySearch(event, node.name)) {
                listMatchedTreeNodes.push(node);
            }
        }
        return listMatchedTreeNodes.slice(0, 5);
    };
    /**
     * check tree data has data or not
     * @docs-private
     */
    SideBarComponent.prototype.checkTreeData = function () {
        return this.treeData && this.treeData.length ? true : false;
    };
    /**
     * listen search-box clear keyword event
     * @docs-private
     */
    SideBarComponent.prototype.clear = function (event) {
        this.search(event);
    };
    /**
     * listen tree-root refresh tree event
     * @docs-private
     */
    SideBarComponent.prototype.refreshTree = function () {
        this.search();
    };
    /**
     * listen search-box filter tree-node event
     * @docs-private
     */
    SideBarComponent.prototype.search = function (event) {
        if (event === void 0) { event = ''; }
        /**
         * when the keyword is null, show the all navi item
         * @docs-private
         */
        if (!event) {
            this.treeData = JSON.parse(JSON.stringify(this.data.tree));
            this.initTree(this.treeData, !!this.data.expanded);
            this.hasTreeData = this.checkTreeData();
            return;
        }
        this._listTreeNodes = [];
        if (this.data.tree) {
            this.transferTreeToList(this.data.tree);
        }
        this._listSearchNodes = [];
        // get all matched keyword tree-nodes
        for (var _i = 0, _a = this._listTreeNodes; _i < _a.length; _i++) {
            var node = _a[_i];
            if (node.name && this.fuzzySearch(event, node.name)) {
                this.searchNodes(node);
            }
        }
        var rootNodes = [];
        rootNodes = this.getRootNodes(this._listSearchNodes);
        for (var _b = 0, rootNodes_1 = rootNodes; _b < rootNodes_1.length; _b++) {
            var root = rootNodes_1[_b];
            for (var _c = 0, _d = this._listSearchNodes; _c < _d.length; _c++) {
                var node = _d[_c];
                this.renderSelectedNode(root, node);
            }
        }
        this.treeData = rootNodes;
        this.hasTreeData = this.checkTreeData();
    };
    /**
     * find all the tree nodes associated with the key words
     * @docs-private
     */
    SideBarComponent.prototype.searchNodes = function (node) {
        if (node.parent) {
            this.setSearchNodes(node);
            var nodeParent = void 0;
            nodeParent = this.getParentNode(node.parent);
            if (nodeParent) {
                if (nodeParent.parent) {
                    this.searchNodes(nodeParent);
                }
                else {
                    this.setSearchNodes(nodeParent);
                }
            }
        }
        else {
            this.setSearchNodes(node);
        }
    };
    /**
     * get tree-node parent node
     * @docs-private
     */
    SideBarComponent.prototype.getParentNode = function (parent) {
        var parentNode;
        parentNode = this._listTreeNodes.find(function (node) {
            return parent.id === node.id;
        });
        return parentNode;
    };
    /**
     * push matched keyword search node into _listSearchNodes
     * @docs-private
     */
    SideBarComponent.prototype.setSearchNodes = function (node) {
        if (!this.isExistRepetition(node)) {
            this._listSearchNodes.push(node);
        }
    };
    /**
     * judge in _listSearchNodes has repeat node or not
     * @docs-private
     */
    SideBarComponent.prototype.isExistRepetition = function (targetNode) {
        if (this._listSearchNodes.length === 0) {
            return false;
        }
        else {
            var nodeFinded = void 0;
            nodeFinded = this._listSearchNodes.find(function (node) {
                return targetNode.id === node.id;
            });
            if (nodeFinded) {
                return true;
            }
            else {
                return false;
            }
        }
    };
    /**
     * get root nodes
     * @docs-private
     */
    SideBarComponent.prototype.getRootNodes = function (rootNodes) {
        return rootNodes.filter(function (node) {
            return !node.parent;
        });
    };
    /**
     * render filtered tree-node
     * @docs-private
     */
    SideBarComponent.prototype.renderSelectedNode = function (rootNode, targetNode) {
        if (rootNode.id === targetNode.id) {
            rootNode.isExpanded = true;
            rootNode.selectable = rootNode.selectable !== false ? true : false;
            rootNode.show = true;
        }
        else {
            if (rootNode.children && rootNode.children.length) {
                for (var _i = 0, _a = rootNode.children; _i < _a.length; _i++) {
                    var child = _a[_i];
                    if (targetNode.id === child.id) {
                        child.isExpanded = true;
                        child.selectable = child.selectable !== false ? true : false;
                        child.show = true;
                    }
                    else {
                        this.renderSelectedNode(child, targetNode);
                    }
                }
            }
        }
    };
    /**
     * throw the navigation click event out
     * @docs-private
     */
    SideBarComponent.prototype.nodeSelect = function (event) {
        var targetNode;
        targetNode = this.setSelectedNode(event);
        if (targetNode) {
            this.selectedNode.isSelected = false;
            targetNode.isSelected = true;
            this.selectedNode = targetNode;
        }
        this.onNavi.emit(this.selectedNode);
    };
    /**
     * fold or unfold side-bar
     * @docs-private
     */
    SideBarComponent.prototype.fold = function () {
        this.expanded = !this.expanded;
        this.toggle.emit(this.expanded);
    };
    /**
     * set selected node
     * @docs-private
     */
    SideBarComponent.prototype.setSelectedNode = function (node) {
        if (node && node.children && node.children.length > 0) {
            var targetNode = this.getFirstChild(node);
            if (targetNode) {
                return targetNode;
            }
        }
        else {
            return node;
        }
    };
    /**
     * get node the first child
     * @param node
     * @docs-private
     */
    SideBarComponent.prototype.getFirstChild = function (node) {
        if (node && node.children && node.children.length) {
            var firstChild = void 0;
            firstChild = node.children[0];
            if (firstChild && firstChild.children && firstChild.children.length > 0) {
                return this.getFirstChild(firstChild);
            }
            else {
                return firstChild;
            }
        }
        else {
            return node;
        }
    };
    tslib_1.__decorate([
        Output(),
        tslib_1.__metadata("design:type", Object)
    ], SideBarComponent.prototype, "onNavi", void 0);
    tslib_1.__decorate([
        Output(),
        tslib_1.__metadata("design:type", Object)
    ], SideBarComponent.prototype, "toggle", void 0);
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", Object)
    ], SideBarComponent.prototype, "data", void 0);
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", String)
    ], SideBarComponent.prototype, "selectedNodeId", void 0);
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", TemplateRef)
    ], SideBarComponent.prototype, "optionTpl", void 0);
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", String)
    ], SideBarComponent.prototype, "searchPlaceholder", void 0);
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", Boolean)
    ], SideBarComponent.prototype, "isSuggestion", void 0);
    SideBarComponent = tslib_1.__decorate([
        Component({
            selector: 'nb-side-bar',
            template: "<div *ngIf=\"expanded\" class=\"nb-side-bar-content\"><div><span>{{ data.title }}</span> <button nb-button theme=\"transparent\" class=\"nb-side-bar-fold iconfont icon-collapse\" (click)=\"fold()\"></button></div><nb-search-box [placeholder]=\"searchPlaceholder\" [isSuggestion]=\"isSuggestion\" size=\"small\" (onSearchSuggestion)=\"searchSuggestion($event)\" [suggestionValue]=\"suggestionValue\" (onClear)=\"clear($event)\" (onSearch)=\"search($event)\"></nb-search-box><a *ngIf=\"data.root\" nb-button class=\"nb-side-bar-root\" (click)=\"refreshTree()\">{{ data.root }}</a><nb-tree *ngIf=\"hasTreeData\" [treeData]=\"changeTree()\" [optionTpl]=\"optionTpl\" selectionMode=\"navigation\" (onNodeSelect)=\"nodeSelect($event)\"></nb-tree><div *ngIf=\"!hasTreeData\" class=\"nb-side-bar-no-tree\">暂无数据！</div></div><div *ngIf=\"!expanded\" class=\"nb-side-bar-content-collapse\"><div><button nb-button theme=\"transparent\" class=\"nb-side-bar-fold iconfont icon-expansion\" (click)=\"fold()\"></button></div></div>",
            encapsulation: ViewEncapsulation.None,
            changeDetection: ChangeDetectionStrategy.OnPush,
            preserveWhitespaces: false,
            host: {
                'class': 'nb-widget nb-side-bar'
            }
        }),
        tslib_1.__metadata("design:paramtypes", [])
    ], SideBarComponent);
    return SideBarComponent;
}());
export { SideBarComponent };
//# sourceMappingURL=side-bar.js.map