/**
 * Copyright (c) 2020-present, Goldman Sachs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { action, makeObservable, observable } from 'mobx';
import { FileSystem_Directory, openNode, populateDirectoryTreeNodeChildren, reprocessOpenNodes, } from '../utils/FileSystemTreeUtils.js';
export class FileSystemState {
    root;
    directoryTreeData;
    selectedNode;
    filesIndex = new Map();
    constructor(root) {
        this.root = new FileSystem_Directory(root);
        makeObservable(this, {
            root: observable,
            directoryTreeData: observable.ref,
            selectedNode: observable.ref,
            reprocessNodeTree: action,
            setDirectoryTreeData: action,
            onTreeNodeSelect: action,
            setSelectedNode: action,
        });
    }
    getOrCreateDirectory(directoryName) {
        return FileSystem_Directory.getOrCreateDirectory(this.root, directoryName, true);
    }
    setDirectoryTreeData(directoryTreeData) {
        this.directoryTreeData = directoryTreeData;
    }
    reprocessNodeTree(generationResult, treeData, openedNodeIds) {
        reprocessOpenNodes(treeData, this.filesIndex, this.root, openedNodeIds);
        // select the current file node if available, else select the first output
        const selectedFileNodePath = generationResult.length === 1 ||
            (this.selectedNode === undefined && generationResult.length !== 0)
            ? generationResult[0].value.fileName
            : this.selectedNode?.fileNode.path;
        if (selectedFileNodePath) {
            const file = this.filesIndex.get(selectedFileNodePath);
            if (file) {
                const node = openNode(file, treeData);
                if (node) {
                    this.onTreeNodeSelect(node, treeData);
                }
            }
            else {
                this.selectedNode = undefined;
            }
        }
        this.setDirectoryTreeData({ ...treeData });
    }
    onTreeNodeSelect(node, treeData) {
        if (node.childrenIds?.length) {
            node.isOpen = !node.isOpen;
            if (node.fileNode instanceof FileSystem_Directory) {
                populateDirectoryTreeNodeChildren(node, treeData);
            }
        }
        this.setSelectedNode(node);
        this.setDirectoryTreeData({ ...treeData });
    }
    setSelectedNode(node) {
        if (this.selectedNode) {
            this.selectedNode.isSelected = false;
        }
        if (node) {
            node.isSelected = true;
        }
        this.selectedNode = node;
    }
}
//# sourceMappingURL=FileSystemState.js.map