"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MainThreadSecret = void 0;
const tslib_1 = require("tslib");
const di_1 = require("@opensumi/di");
const ide_connection_1 = require("@opensumi/ide-connection");
const ide_core_browser_1 = require("@opensumi/ide-core-browser");
const services_1 = require("@opensumi/ide-core-browser/lib/services");
const vscode_1 = require("../../../common/vscode");
let MainThreadSecret = class MainThreadSecret extends ide_core_browser_1.Disposable {
    constructor(rpcProtocol) {
        super();
        this._proxy = rpcProtocol.getProxy(vscode_1.ExtHostAPIIdentifier.ExtHostSecret);
        this.addDispose(this.credentialsService.onDidChangePassword((e) => {
            const extensionId = e.service.substring(this.appConfig.uriScheme.length);
            this._proxy.$onDidChangePassword({ extensionId, key: e.account });
        }));
    }
    getFullKey(extensionId) {
        return `${this.appConfig.uriScheme}${extensionId}`;
    }
    async $getPassword(extensionId, key) {
        const fullKey = this.getFullKey(extensionId);
        const password = await this.credentialsService.getPassword(fullKey, key);
        const decrypted = password && (await this.cryptoService.decrypt(password));
        if (decrypted) {
            try {
                const value = JSON.parse(decrypted);
                if (value.extensionId === extensionId) {
                    return value.content;
                }
            }
            catch (_) {
                throw new Error('Cannot get password');
            }
        }
        return undefined;
    }
    async $setPassword(extensionId, key, value) {
        const fullKey = this.getFullKey(extensionId);
        const toEncrypt = JSON.stringify({
            extensionId,
            content: value,
        });
        const encrypted = await this.cryptoService.encrypt(toEncrypt);
        return this.credentialsService.setPassword(fullKey, key, encrypted);
    }
    async $deletePassword(extensionId, key) {
        try {
            const fullKey = this.getFullKey(extensionId);
            await this.credentialsService.deletePassword(fullKey, key);
        }
        catch (_) {
            throw new Error('Cannot delete password');
        }
    }
};
exports.MainThreadSecret = MainThreadSecret;
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_core_browser_1.AppConfig),
    tslib_1.__metadata("design:type", Object)
], MainThreadSecret.prototype, "appConfig", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_core_browser_1.ICredentialsService),
    tslib_1.__metadata("design:type", Object)
], MainThreadSecret.prototype, "credentialsService", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(services_1.ICryptoService),
    tslib_1.__metadata("design:type", Object)
], MainThreadSecret.prototype, "cryptoService", void 0);
exports.MainThreadSecret = MainThreadSecret = tslib_1.__decorate([
    (0, di_1.Injectable)({ multiple: true }),
    tslib_1.__param(0, (0, di_1.Optional)(Symbol())),
    tslib_1.__metadata("design:paramtypes", [Object])
], MainThreadSecret);
//# sourceMappingURL=main.thread.secret.js.map