"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TreeModel = void 0;
const ide_utils_1 = require("@opensumi/ide-utils");
const types_1 = require("../../types");
const treeState_1 = require("./treeState");
class TreeModel {
    constructor() {
        this.onChangeEmitter = new ide_utils_1.Emitter();
        this.dispatchChange = () => {
            this.onChangeEmitter.fire();
        };
    }
    get onChange() {
        return this.onChangeEmitter.event;
    }
    get ensureReady() {
        return this._ensureReady;
    }
    get root() {
        return this._root;
    }
    set root(root) {
        this._root = root;
        this.initState(root);
        if (this.root) {
            this._ensureReady = this.root.ensureLoaded();
        }
    }
    get state() {
        return this._state;
    }
    set state(state) {
        this._state = state;
    }
    init(root) {
        this.root = root;
        // 分支更新时通知树刷新
        this.root.watcher.on(types_1.TreeNodeEvent.BranchDidUpdate, this.dispatchChange);
    }
    initState(root) {
        this.state = new treeState_1.TreeStateManager(root);
    }
    /**
     * 根据给定的状态信息还原Tree组件
     *
     * TreeState中包含:
     *  - 可展开节点的展开状态
     *  - 滚动偏移
     *
     * TreeState中不包含:
     *  - 装饰器
     *  - 临时的输入框节点
     *
     */
    loadTreeState(state) {
        if (typeof state === 'string') {
            state = JSON.parse(state);
        }
        return this.state.loadTreeState(state);
    }
    /**
     * 返回一个与实际树状态同步的`TreeStateWatcher`
     *
     * TreeState中包含:
     *  - 可展开节点的展开状态
     *  - 滚动偏移
     *
     * TreeState中不包含:
     *  - 装饰器
     *  - 临时的输入框节点
     *
     * `TreeStateWatcher#onDidChange` 用于在状态变更时绑定监听函数
     *
     * `TreeStateWatcher#snapshot` 用于获取当前树的快照信息（用于状态恢复），返回state对象
     *
     * `TreeStateWatcher#toString` 用于将当前状态转换为JSON字符串
     */
    getTreeStateWatcher(atSurfaceExpandedDirsOnly = false) {
        return new treeState_1.TreeStateWatcher(this.state, atSurfaceExpandedDirsOnly);
    }
    resolveChildren(parent) {
        return Promise.resolve(Array.from(parent.children));
    }
}
exports.TreeModel = TreeModel;
//# sourceMappingURL=TreeModel.js.map