/**
 * DevExtreme (cjs/ui/gantt/ui.gantt.treelist.nodes_state.js)
 * Version: 23.2.6 (build 24140-0112)
 * Build date: Sun May 19 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
exports.GanttTreeListNodesState = exports.GanttTreeListNodeState = void 0;
let GanttTreeListNodeState = function() {
    function GanttTreeListNodeState(treeListNode) {
        var _treeListNode$parent;
        this.collapsed = false;
        this.key = treeListNode.key;
        this.children = treeListNode.children.map(node => node.key);
        this.parentKey = null === (_treeListNode$parent = treeListNode.parent) || void 0 === _treeListNode$parent ? void 0 : _treeListNode$parent.key
    }
    var _proto = GanttTreeListNodeState.prototype;
    _proto.hasChildren = function() {
        return this.children.length > 0
    };
    _proto.removeChild = function(state) {
        const index = this.children.indexOf(state.key);
        if (index > -1) {
            this.children = this.children.splice(index, 1)
        }
    };
    _proto.equal = function(state) {
        if (!state || state.key !== this.key || state.parentKey !== this.parentKey) {
            return false
        }
        if (this.children.length !== state.children.length || this.children.some((value, index) => value !== state.children[index])) {
            return false
        }
        return true
    };
    return GanttTreeListNodeState
}();
exports.GanttTreeListNodeState = GanttTreeListNodeState;
let GanttTreeListNodesState = function() {
    function GanttTreeListNodesState() {
        this._resetHash()
    }
    var _proto2 = GanttTreeListNodesState.prototype;
    _proto2.clear = function() {
        this._resetHash()
    };
    _proto2.applyNodes = function(nodes, rootValue) {
        if (this._rootValue !== rootValue) {
            this._resetHash();
            this._rootValue = rootValue
        }
        this._removeNonExistentNodes(nodes.map(node => node.key));
        nodes.forEach(node => this._applyNode(node));
        this._validateHash()
    };
    _proto2.saveExpandedState = function(expandedKeys) {
        this._hasCollapsed = false;
        this._forEachState(state => {
            if (state.hasChildren() && !expandedKeys.includes(state.key)) {
                state.collapsed = true;
                this._hasCollapsed = true
            }
        })
    };
    _proto2.getExpandedKeys = function() {
        if (this._hasCollapsed) {
            const keys = [];
            this._forEachState(state => {
                if (state.hasChildren() && !state.collapsed) {
                    keys.push(state.key)
                }
            });
            return keys
        }
        return null
    };
    _proto2._resetHash = function() {
        this._nodeHash = {};
        this._hasCollapsed = false
    };
    _proto2._getNodeState = function(key) {
        return this._nodeHash[key]
    };
    _proto2._removeNonExistentNodes = function(existingKeys) {
        if (existingKeys) {
            this._forEachState(state => {
                if (!existingKeys.includes(state.key)) {
                    this._removeStateWithChildren(state)
                }
            })
        }
    };
    _proto2._removeStateWithChildren = function(key) {
        const state = this._getNodeState(key);
        if (state) {
            state.children.forEach(child => this._removeStateWithChildren(child));
            const parent = this._getNodeState(state.parentKey);
            if (parent) {
                parent.removeChild(state)
            }
            delete this._nodeHash[key]
        }
    };
    _proto2._applyNode = function(node) {
        const nodeState = new GanttTreeListNodeState(node);
        const oldState = this._getNodeState(node.key);
        if (!(null !== oldState && void 0 !== oldState && oldState.equal(nodeState))) {
            this._nodeHash[node.key] = nodeState;
            this._expandTreelineToNode(node.key)
        }
    };
    _proto2._expandTreelineToNode = function(key) {
        const state = this._getNodeState(key);
        let parent = this._getNodeState(null === state || void 0 === state ? void 0 : state.parentKey);
        while (parent) {
            parent.collapsed = false;
            parent = this._getNodeState(parent.parentKey)
        }
    };
    _proto2._validateHash = function() {
        Object.keys(this._nodeHash).forEach(key => {
            const state = this._getNodeState(key);
            const parentKey = null === state || void 0 === state ? void 0 : state.parentKey;
            if (parentKey !== this._rootValue && !this._getNodeState(parentKey)) {
                this._removeStateWithChildren(key)
            }
        })
    };
    _proto2._forEachState = function(callback) {
        Object.keys(this._nodeHash).forEach(key => {
            const state = this._nodeHash[key];
            if (state) {
                callback(state)
            }
        })
    };
    return GanttTreeListNodesState
}();
exports.GanttTreeListNodesState = GanttTreeListNodesState;
