var _a, _b;
import { __decorate, __extends } from "tslib";
import Component from 'vue-class-component';
import { Emit, Prop, Watch } from 'vue-property-decorator';
import { I18N_NAME as FILTER_I18N_NAME } from '../../filters/filter-names';
import { i18nFilter } from '../../filters/i18n/i18n';
import { ModulVue } from '../../utils/vue/vue';
import { MESSAGE_NAME, TREE_NAME } from '../component-names';
import { MMessage } from '../message/message';
import { TREE_NODE_NAME } from './component-names';
import { MTreeNode } from './tree-node/tree-node';
import WithRender from './tree.html?style=./tree.scss';
export var MSelectionMode;
(function (MSelectionMode) {
    MSelectionMode["None"] = "none";
    MSelectionMode["Single"] = "single";
    MSelectionMode["Multiple"] = "multiple";
    MSelectionMode["Readonly"] = "readonly";
})(MSelectionMode || (MSelectionMode = {}));
export var MCheckboxes;
(function (MCheckboxes) {
    MCheckboxes["True"] = "true";
    MCheckboxes["False"] = "false";
    MCheckboxes["WithButtonAutoSelect"] = "with-button-auto-select";
    MCheckboxes["WithCheckboxAutoSelect"] = "with-checkbox-auto-select";
    MCheckboxes["WithParentAutoSelect"] = "with-parent-auto-select"; // Children can be selected by parent & children can unselect parent
})(MCheckboxes || (MCheckboxes = {}));
var MTree = /** @class */ (function (_super) {
    __extends(MTree, _super);
    function MTree() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.propSelectedNodes = _this.selectedNodes || [];
        _this.errorTree = false;
        _this.selectedNodesFound = [];
        return _this;
    }
    MTree.prototype.onClick = function (path) {
        if (!this.pathIsDisabled(path)) {
            if (this.propSelectedNodes.indexOf(path) === -1) {
                if (this.selectionMode === MSelectionMode.Multiple) {
                    this.propSelectedNodes.push(path);
                }
                else {
                    this.propSelectedNodes = [path];
                }
            }
            else if (this.selectionMode === MSelectionMode.Multiple) {
                this.propSelectedNodes.splice(this.propSelectedNodes.indexOf(path), 1);
            }
        }
        return path;
    };
    MTree.prototype.created = function () {
        this.browseTree();
    };
    MTree.prototype.browseTree = function () {
        var _this = this;
        this.errorTree = false;
        this.tree.forEach(function (node) {
            _this.browseNode(node);
        });
        this.propSelectedNodes.forEach(function (selectedNode) {
            if (_this.selectedNodesFound.indexOf(selectedNode) === -1) {
                console.error("modUL - The selected node was not found: \"" + selectedNode + "\"");
            }
        });
    };
    MTree.prototype.pathIsDisabled = function (path) {
        return this.propDisabledNodes.indexOf(path) !== -1;
    };
    MTree.prototype.browseNode = function (node, path) {
        var _this = this;
        if (path === void 0) { path = ''; }
        if (node.id.trim() === '') {
            this.errorTree = true;
        }
        var currentPath = path + '/' + node.id;
        if (this.propSelectedNodes.indexOf(currentPath) !== -1) {
            this.selectedNodesFound.push(currentPath);
        }
        if (node.children) {
            node.children.forEach(function (childNode) {
                _this.browseNode(childNode, currentPath);
            });
        }
    };
    Object.defineProperty(MTree.prototype, "propTreeEmpty", {
        get: function () {
            return !this.tree.length;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MTree.prototype, "propDisabledNodes", {
        get: function () {
            return this.disabledNodes || [];
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MTree.prototype, "selectable", {
        get: function () {
            return this.selectionMode !== MSelectionMode.None && !this.isReadonly;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MTree.prototype, "isReadonly", {
        get: function () {
            return this.selectionMode === MSelectionMode.Readonly;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MTree.prototype, "isMultipleSelectWithCheckboxes", {
        get: function () {
            return this.selectionMode === MSelectionMode.Multiple && this.withCheckboxes;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MTree.prototype, "withCheckboxes", {
        get: function () {
            return this.checkboxes !== MCheckboxes.False;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MTree.prototype, "isSingleNodeTree", {
        get: function () {
            return this.tree.length === 1;
        },
        enumerable: false,
        configurable: true
    });
    __decorate([
        Prop()
    ], MTree.prototype, "tree", void 0);
    __decorate([
        Prop({
            default: MSelectionMode.Single,
            validator: function (value) {
                return value === MSelectionMode.None ||
                    value === MSelectionMode.Single ||
                    value === MSelectionMode.Multiple ||
                    value === MSelectionMode.Readonly;
            }
        })
    ], MTree.prototype, "selectionMode", void 0);
    __decorate([
        Prop({
            default: MCheckboxes.False,
            validator: function (value) {
                return value === MCheckboxes.False ||
                    value === MCheckboxes.True ||
                    value === MCheckboxes.WithButtonAutoSelect ||
                    value === MCheckboxes.WithCheckboxAutoSelect ||
                    value === MCheckboxes.WithParentAutoSelect;
            }
        })
    ], MTree.prototype, "checkboxes", void 0);
    __decorate([
        Prop()
    ], MTree.prototype, "selectedNodes", void 0);
    __decorate([
        Prop()
    ], MTree.prototype, "useFilesIcons", void 0);
    __decorate([
        Prop()
    ], MTree.prototype, "disabledNodes", void 0);
    __decorate([
        Emit('select')
    ], MTree.prototype, "onClick", null);
    __decorate([
        Watch('tree')
    ], MTree.prototype, "browseTree", null);
    MTree = __decorate([
        WithRender,
        Component({
            components: (_a = {},
                _a[TREE_NODE_NAME] = MTreeNode,
                _a[MESSAGE_NAME] = MMessage,
                _a),
            filters: (_b = {},
                _b[FILTER_I18N_NAME] = i18nFilter,
                _b)
        })
    ], MTree);
    return MTree;
}(ModulVue));
export { MTree };
var TreePlugin = {
    install: function (v, options) {
        v.component(TREE_NAME, MTree);
    }
};
export default TreePlugin;
//# sourceMappingURL=tree.js.map