"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BasicTreeModel = exports.BasicTreeService = void 0;
const ide_utils_1 = require("@opensumi/ide-utils");
const tree_1 = require("../tree");
const types_1 = require("../types");
const tree_node_define_1 = require("./tree-node.define");
const types_2 = require("./types");
class BasicTreeService extends tree_1.Tree {
    constructor(_treeData, _resolveChildren, _sortComparator, treeOptions = {}) {
        super();
        this._treeData = _treeData;
        this._resolveChildren = _resolveChildren;
        this._sortComparator = _sortComparator;
        this.treeOptions = treeOptions;
        this.selectedDecoration = new tree_1.Decoration(types_2.DECORATIONS.SELECTED); // 选中态
        this.focusedDecoration = new tree_1.Decoration(types_2.DECORATIONS.FOCUSED); // 焦点态
        this.contextMenuDecoration = new tree_1.Decoration(types_2.DECORATIONS.ACTIVED); // 右键菜单激活态
        this.loadingDecoration = new tree_1.Decoration(types_2.DECORATIONS.LOADING); // 加载态
        // 选中态的节点
        this._selectedNodes = [];
        this.disposableCollection = new ide_utils_1.DisposableCollection();
        this.decorationDisposableCollection = new ide_utils_1.DisposableCollection();
        this.onDidUpdateTreeModelEmitter = new ide_utils_1.Emitter();
        this.sortComparator = (a, b) => {
            if (this._sortComparator) {
                const result = this._sortComparator(a.raw, b.raw);
                if (typeof result !== 'undefined') {
                    return result;
                }
            }
            return super.sortComparator(a, b);
        };
        // 清空其他选中/焦点态节点，更新当前焦点节点
        this.activeFocusedDecoration = (target) => {
            var _a;
            if (this._contextMenuNode) {
                this.clearDecoration(this.contextMenuDecoration);
                this.clearDecoration(this.focusedDecoration);
                this.clearDecoration(this.selectedDecoration);
                this._contextMenuNode = undefined;
            }
            if (target) {
                if (this.selectedNodes.length > 0) {
                    this.clearDecoration(this.selectedDecoration);
                }
                if (this.focusedNode) {
                    this.clearDecoration(this.focusedDecoration);
                    this._focusedNode = undefined;
                }
                this.selectedDecoration.addTarget(target);
                this.focusedDecoration.addTarget(target);
                this._focusedNode = target;
                this._selectedNodes = [target];
                (_a = this.model) === null || _a === void 0 ? void 0 : _a.dispatchChange();
            }
        };
        this.activeContextMenuDecoration = (target) => {
            var _a;
            if (this._contextMenuNode) {
                this.clearDecoration(this.contextMenuDecoration);
                this._contextMenuNode = undefined;
            }
            if (this.focusedNode) {
                this.clearDecoration(this.focusedDecoration);
                this._focusedNode = undefined;
            }
            this.contextMenuDecoration.addTarget(target);
            this._contextMenuNode = target;
            (_a = this.model) === null || _a === void 0 ? void 0 : _a.dispatchChange();
        };
        // 取消选中节点焦点
        this.enactiveFocusedDecoration = () => {
            var _a;
            if (this.focusedNode) {
                this.clearDecoration(this.focusedDecoration);
                this._focusedNode = undefined;
                (_a = this.model) === null || _a === void 0 ? void 0 : _a.dispatchChange();
            }
        };
        this.disposableCollection.push(this.onDidUpdateTreeModelEmitter);
    }
    setUpTreeModel() {
        this._root = new tree_node_define_1.BasicTreeRoot(this, undefined, { children: this._treeData, label: '', command: '', icon: '' }, {
            treeName: this.treeOptions.treeName,
        });
        this._model = new BasicTreeModel();
        this._model.init(this._root);
        this.initDecorations(this._root);
        this.onDidUpdateTreeModelEmitter.fire(this._model);
    }
    resetState() {
        this._selectedNodes = [];
        this._contextMenuNode = undefined;
        this._focusedNode = undefined;
    }
    get onDidUpdateTreeModel() {
        return this.onDidUpdateTreeModelEmitter.event;
    }
    get model() {
        return this._model;
    }
    get root() {
        return this._root;
    }
    get decorations() {
        return this._decorations;
    }
    initDecorations(root) {
        this.decorationDisposableCollection.dispose();
        this._decorations = new tree_1.DecorationsManager(root);
        this._decorations.addDecoration(this.selectedDecoration);
        this._decorations.addDecoration(this.focusedDecoration);
        this._decorations.addDecoration(this.contextMenuDecoration);
        this._decorations.addDecoration(this.loadingDecoration);
        this.decorationDisposableCollection.push(root.watcher.on(types_1.TreeNodeEvent.WillResolveChildren, (target) => {
            this.loadingDecoration.addTarget(target);
        }));
        this.decorationDisposableCollection.push(root.watcher.on(types_1.TreeNodeEvent.DidResolveChildren, (target) => {
            this.loadingDecoration.removeTarget(target);
        }));
        this.decorationDisposableCollection.push(this._decorations);
    }
    updateTreeData(treeData) {
        this._treeData = treeData;
        this.resetState();
        this.setUpTreeModel();
    }
    async resolveChildren(parent) {
        if (this._resolveChildren) {
            return this.transformTreeNode(parent, this._resolveChildren(parent === null || parent === void 0 ? void 0 : parent.raw) || []);
        }
        else {
            return this.transformTreeNode(parent, (parent === null || parent === void 0 ? void 0 : parent.raw.children) || []);
        }
    }
    transformTreeNode(parent, nodes) {
        if (!nodes) {
            return [];
        }
        const result = [];
        for (const node of nodes) {
            if (node.children) {
                result.push(new tree_node_define_1.BasicCompositeTreeNode(this, parent, node));
            }
            else {
                result.push(new tree_node_define_1.BasicTreeNode(this, parent, node));
            }
        }
        return result;
    }
    get selectedNodes() {
        return this._selectedNodes;
    }
    get focusedNode() {
        return this._focusedNode;
    }
    get contextMenuNode() {
        return this._contextMenuNode;
    }
    clearDecoration(decoration) {
        for (const target of decoration.appliedTargets.keys()) {
            decoration.removeTarget(target);
        }
    }
    dispose() {
        super.dispose();
        this.disposableCollection.dispose();
        this.decorationDisposableCollection.dispose();
    }
}
exports.BasicTreeService = BasicTreeService;
class BasicTreeModel extends tree_1.TreeModel {
}
exports.BasicTreeModel = BasicTreeModel;
//# sourceMappingURL=tree-service.js.map