"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExtensionGlobalMemento = exports.ExtensionMemento = exports.ExtHostStorage = void 0;
const ide_core_common_1 = require("@opensumi/ide-core-common");
const vscode_1 = require("../../../common/vscode");
class ExtHostStorage {
    constructor(rpc) {
        this._onDidChangeStorage = new ide_core_common_1.Emitter();
        this.onDidChangeStorage = this._onDidChangeStorage.event;
        this.proxy = rpc.getProxy(vscode_1.MainThreadAPIIdentifier.MainThreadStorage);
    }
    getExtensionGlobalStorageUri(extensionId) {
        return ide_core_common_1.UriUtils.resolvePath(this._storagePath.globalStorageUri, extensionId);
    }
    getExtensionStorageUri(extensionId) {
        return ide_core_common_1.UriUtils.resolvePath(this._storagePath.storageUri, extensionId);
    }
    getExtensionLogUri(extensionId) {
        return ide_core_common_1.UriUtils.resolvePath(this._storagePath.logUri, extensionId);
    }
    async getValue(shared, key, defaultValue) {
        return this.proxy.$getValue(shared, key).then((value) => value || defaultValue);
    }
    async setValue(shared, key, value) {
        return this.proxy.$setValue(shared, key, value);
    }
    async $updateWorkspaceStorageData(data) {
        this._onDidChangeStorage.fire({ shared: false, data });
    }
    async $acceptStoragePath(paths) {
        this._storagePath = paths;
    }
}
exports.ExtHostStorage = ExtHostStorage;
class ExtensionMemento {
    constructor(id, global, storage) {
        this.id = id;
        this.global = global;
        this.storage = storage;
        this._init = this.storage.getValue(this.global, this.id, Object.create(null)).then((value) => {
            this.cache = value;
            return this;
        });
        this.storageListener = this.storage.onDidChangeStorage((e) => {
            if (e.shared === this.global) {
                this.cache = e.data[this.id] || {};
            }
        });
    }
    get keys() {
        return Object.keys(this.cache);
    }
    get whenReady() {
        return this._init;
    }
    get(key, defaultValue) {
        let value = this.cache[key];
        if (typeof value === 'undefined') {
            value = defaultValue;
        }
        return value;
    }
    update(key, value) {
        this.cache[key] = value;
        return this.storage.setValue(this.global, this.id, this.cache);
    }
    dispose() {
        this.storageListener.dispose();
    }
}
exports.ExtensionMemento = ExtensionMemento;
class ExtensionGlobalMemento extends ExtensionMemento {
    setKeysForSync() {
        // TODO: 目前 IDE 都会同步配置，该方法先空实现
    }
}
exports.ExtensionGlobalMemento = ExtensionGlobalMemento;
//# sourceMappingURL=ext.host.storage.js.map