var _a;
import { __decorate, __extends } from "tslib";
import Component from 'vue-class-component';
import { Emit, Prop, Watch } from 'vue-property-decorator';
import { ModulVue } from '../../../utils/vue/vue';
import { MCheckbox } from '../../checkbox/checkbox';
import { ACCORDION_TRANSITION_NAME, CHECKBOX_NAME, ICON_NAME, TREE_ICON_NAME } from '../../component-names';
import { MIcon } from '../../icon/icon';
import { MAccordionTransition } from '../../transitions/accordion-transition/accordion-transition';
import { TREE_NODE_NAME } from '../component-names';
import { MCheckboxes } from '../tree';
import { MTreeIcon } from '../tree-icon/tree-icon';
import WithRender from './tree-node.html?style=./tree-node.scss';
var MTreeNode = /** @class */ (function (_super) {
    __extends(MTreeNode, _super);
    function MTreeNode() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.internalOpen = false;
        _this.allChildrenAndSelfSelected = false;
        _this.selectedChildrenCount = 0;
        return _this;
    }
    MTreeNode.prototype.notifyParentOfChildCheckboxState = function () {
        if (this.withCheckboxes) {
            if (!this.hasChildren || this.isParentAutoSelect) {
                this.$emit('auto-select-child-checkbox-change', this.isSelected);
            }
            else if (this.isButtonAutoSelect && this.hasChildren) {
                this.onAutoSelectChildCheckboxChange(this.isSelected, true);
            }
        }
    };
    MTreeNode.prototype.emitClick = function (path) {
        return path;
    };
    MTreeNode.prototype.mounted = function () {
        this.internalOpen = this.open || this.isParentOfOpenedFolder() || this.isParentOfSelectedFile;
        if (this.isSelected) {
            this.notifyParentOfChildCheckboxState();
        }
    };
    MTreeNode.prototype.onClick = function () {
        if (this.isFolder) {
            this.internalOpen = !this.internalOpen;
            this.$emit('update:open', this.internalOpen);
        }
        else if (this.selectable) {
            this.emitClick(this.currentPath);
        }
    };
    MTreeNode.prototype.onAutoSelectButtonClick = function () {
        this.recursiveSelect();
    };
    MTreeNode.prototype.onAutoSelectChildCheckboxChange = function (selected, ignoreCount) {
        if (ignoreCount === void 0) { ignoreCount = false; }
        if (!ignoreCount) {
            this.selectedChildrenCount += selected ? 1 : -1;
        }
        var allChildrenSelected = this.selectedChildrenCount === (this.node.children ? this.node.children.length : -1);
        if (this.isCheckboxAutoSelect || (this.isParentAutoSelect && !selected)) { // Parent checkbox mode does not select parent by itself
            this.updateCheckboxParentNode(allChildrenSelected);
        }
        else if (this.isButtonAutoSelect) {
            this.updateButtonParentNode(allChildrenSelected, selected);
        }
    };
    MTreeNode.prototype.childHasSibling = function (index) {
        if (this.node.children) {
            return !!this.node.children[index + 1];
        }
        return false;
    };
    MTreeNode.prototype.onCheckboxClick = function () {
        if (this.hasChildren && (this.isCheckboxAutoSelect || this.isParentAutoSelect)) {
            this.recursiveSelect();
        }
        else {
            this.emitClick(this.currentPath);
        }
    };
    MTreeNode.prototype.recursiveSelect = function () {
        var childrenPaths = [];
        var addNodesToSelected = false;
        if (this.isButtonAutoSelect) {
            childrenPaths.concat(this.currentPath);
            if (!this.allChildrenAndSelfSelected) {
                addNodesToSelected = true;
            }
        }
        else if (!this.isButtonAutoSelect && !this.isSelected) {
            addNodesToSelected = true;
        }
        this.fetchChildrenPaths(this.node, childrenPaths, this.path + '/' + this.node.id, (this.isParentAutoSelect || this.isButtonAutoSelect));
        this.updateSelectedNodes(childrenPaths, addNodesToSelected);
    };
    MTreeNode.prototype.updateSelectedNodes = function (paths, addNodesToSelected) {
        var _this = this;
        if (paths === void 0) { paths = []; }
        paths = Array.from(new Set(paths));
        paths.forEach(function (path) {
            var nodeAlreadySelected = _this.selectedNodes.indexOf(path) !== -1;
            if ((addNodesToSelected && !nodeAlreadySelected) || (!addNodesToSelected && nodeAlreadySelected)) { // Prevent nodes to be pushed twice or unselected nodes to be removed
                _this.emitClick(path);
            }
        });
    };
    MTreeNode.prototype.fetchChildrenPaths = function (currentNode, childrenPath, path, includeParent) {
        var _this = this;
        if (includeParent === void 0) { includeParent = false; }
        if (currentNode.children && currentNode.children.length > 0) {
            currentNode.children.forEach(function (child) {
                _this.fetchChildrenPaths(child, childrenPath, path + '/' + child.id, includeParent);
            });
            if (includeParent) {
                childrenPath.push(path);
            }
        }
        else {
            childrenPath.push(path);
        }
    };
    MTreeNode.prototype.isParentOfOpenedFolder = function (currentNode) {
        var _this = this;
        if (currentNode === void 0) { currentNode = this.node; }
        var found = false;
        if (currentNode.children && currentNode.children.length > 0) {
            currentNode.children.forEach(function (child) {
                if (child.children && child.children.length > 0) {
                    if (child.open) {
                        found = true;
                    }
                    else {
                        found = _this.isParentOfOpenedFolder(child);
                    }
                }
            });
        }
        return found;
    };
    MTreeNode.prototype.updateCheckboxParentNode = function (allChildrenSelected) {
        if (allChildrenSelected && !this.isSelected) {
            this.emitClick(this.currentPath); // Auto-push current, emit to parent for recursivity
            this.$emit('auto-select-child-checkbox-change', true);
        }
        else if (this.isSelected) {
            this.emitClick(this.currentPath);
            this.$emit('auto-select-child-checkbox-change', false); // Not all children are selected, stop recursivity
        }
    };
    MTreeNode.prototype.updateButtonParentNode = function (allChildrenSelected, selected) {
        if (allChildrenSelected && selected && this.isSelected) { // New element selected was added, current and all children are selected
            this.allChildrenAndSelfSelected = true;
            this.$emit('auto-select-child-checkbox-change', true); // Notifies immidiate parent's button that current button is on
        }
        else if (this.allChildrenAndSelfSelected) { // Button was on, but something was removed.
            this.allChildrenAndSelfSelected = false;
            this.$emit('auto-select-child-checkbox-change', false); // Something was removed so every parent buttons must know
        }
    };
    Object.defineProperty(MTreeNode.prototype, "isParentOfSelectedFile", {
        get: function () {
            var _this = this;
            var pathMatchesSelectedNode = false;
            this.selectedNodes.forEach(function (selectedNode) {
                var parentPath = _this.currentPath + '/';
                if (selectedNode.indexOf(parentPath) !== -1) {
                    pathMatchesSelectedNode = true;
                }
            });
            return pathMatchesSelectedNode;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MTreeNode.prototype, "isDisabled", {
        get: function () {
            var isDisabled = false;
            var inDisabledNodes = this.disabledNodes && this.disabledNodes.indexOf(this.currentPath) !== -1;
            if (!this.selectable && !this.isFolder && !this.readonly || inDisabledNodes && !this.readonly) {
                isDisabled = true;
            }
            return isDisabled;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MTreeNode.prototype, "isReadonlyStyle", {
        get: function () {
            var isReadonly = false;
            if (!this.selectable && this.readonly) {
                isReadonly = true;
            }
            return isReadonly;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MTreeNode.prototype, "currentPath", {
        get: function () {
            return this.path + "/" + this.node.id;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MTreeNode.prototype, "label", {
        get: function () {
            return this.node.label || this.node.id;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MTreeNode.prototype, "propCheckboxes", {
        get: function () {
            return this.checkboxes || MCheckboxes.False;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MTreeNode.prototype, "moveSelectionZoneToCheckbox", {
        get: function () {
            return this.withCheckboxes && !this.isFolder;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MTreeNode.prototype, "displaySelectionButton", {
        get: function () {
            return this.hasChildren && this.checkboxes === MCheckboxes.WithButtonAutoSelect;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MTreeNode.prototype, "hasChildren", {
        get: function () {
            return !!this.node.children && this.node.children.length > 0;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MTreeNode.prototype, "isFolder", {
        get: function () {
            return this.node.hasChildren || !!this.node.children;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MTreeNode.prototype, "isSelected", {
        get: function () {
            return this.selectedNodes.indexOf(this.currentPath) !== -1;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MTreeNode.prototype, "isIndeterminate", {
        get: function () {
            return (this.isCheckboxAutoSelect || this.isParentAutoSelect) && this.isParentOfSelectedFile && !this.isSelected;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MTreeNode.prototype, "emptyContentMessage", {
        get: function () {
            return this.placeholder || this.$i18n.translate('m-tree-node:empty');
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MTreeNode.prototype, "withCheckboxes", {
        get: function () {
            return this.checkboxes !== MCheckboxes.False;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MTreeNode.prototype, "isParentAutoSelect", {
        get: function () {
            return this.checkboxes === MCheckboxes.WithParentAutoSelect;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MTreeNode.prototype, "isButtonAutoSelect", {
        get: function () {
            return this.checkboxes === MCheckboxes.WithButtonAutoSelect;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MTreeNode.prototype, "isCheckboxAutoSelect", {
        get: function () {
            return this.checkboxes === MCheckboxes.WithCheckboxAutoSelect;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MTreeNode.prototype, "hasRightIcon", {
        get: function () {
            return this.withCheckboxes && !!this.node.rightIconName;
        },
        enumerable: false,
        configurable: true
    });
    __decorate([
        Prop()
    ], MTreeNode.prototype, "node", void 0);
    __decorate([
        Prop({ default: false })
    ], MTreeNode.prototype, "open", void 0);
    __decorate([
        Prop({ default: [] })
    ], MTreeNode.prototype, "selectedNodes", void 0);
    __decorate([
        Prop()
    ], MTreeNode.prototype, "checkboxes", void 0);
    __decorate([
        Prop()
    ], MTreeNode.prototype, "selectable", void 0);
    __decorate([
        Prop()
    ], MTreeNode.prototype, "readonly", void 0);
    __decorate([
        Prop()
    ], MTreeNode.prototype, "useFilesIcons", void 0);
    __decorate([
        Prop({ default: '' })
    ], MTreeNode.prototype, "path", void 0);
    __decorate([
        Prop()
    ], MTreeNode.prototype, "placeholder", void 0);
    __decorate([
        Prop()
    ], MTreeNode.prototype, "hasSibling", void 0);
    __decorate([
        Prop()
    ], MTreeNode.prototype, "disabledNodes", void 0);
    __decorate([
        Watch('isSelected')
    ], MTreeNode.prototype, "notifyParentOfChildCheckboxState", null);
    __decorate([
        Emit('click')
    ], MTreeNode.prototype, "emitClick", null);
    MTreeNode = __decorate([
        WithRender,
        Component({
            name: TREE_NODE_NAME,
            components: (_a = {},
                _a[TREE_ICON_NAME] = MTreeIcon,
                _a[ACCORDION_TRANSITION_NAME] = MAccordionTransition,
                _a[CHECKBOX_NAME] = MCheckbox,
                _a[ICON_NAME] = MIcon,
                _a)
        })
    ], MTreeNode);
    return MTreeNode;
}(ModulVue));
export { MTreeNode };
//# sourceMappingURL=tree-node.js.map