"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MainThreadEnv = 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 http_opener_1 = require("@opensumi/ide-core-browser/lib/opener/http-opener");
const ide_core_common_1 = require("@opensumi/ide-core-common");
const browser_1 = require("@opensumi/ide-logs/lib/browser");
const vscode_1 = require("../../../common/vscode");
const ext_types_1 = require("../../../common/vscode/ext-types");
const main_thread_storage_1 = require("./main.thread.storage");
let MainThreadEnv = class MainThreadEnv {
    // 检测下支持的协议，以防打开内部协议
    // 支持 http/https/mailto/projectScheme 协议
    isSupportedLink(uri) {
        return http_opener_1.HttpOpener.standardSupportedLinkSchemes.has(uri.scheme) || uri.scheme === this.appConfig.uriScheme;
    }
    constructor(rpcProtocol, storage) {
        this.rpcProtocol = rpcProtocol;
        this.storage = storage;
        this.proxy = this.rpcProtocol.getProxy(vscode_1.ExtHostAPIIdentifier.ExtHostEnv);
        this.eventDispose = this.loggerManger.onDidChangeLogLevel((level) => {
            this.proxy.$fireChangeLogLevel(level);
        });
        this.setLogLevel();
        this.setEnvValues();
    }
    async setEnvValues() {
        const { appName, uriScheme, appHost, appRoot, customVSCodeEngineVersion } = this.appConfig;
        const firstSessionDateValue = await this.storage.$getValue(true, ide_core_common_1.firstSessionDateStorageKey);
        this.proxy.$setEnvValues({
            customVSCodeEngineVersion,
            appName,
            uriScheme,
            appHost,
            appRoot,
            language: (0, ide_core_common_1.getCodeLanguage)(),
            uiKind: this.appConfig.isElectronRenderer ? ext_types_1.UIKind.Desktop : ext_types_1.UIKind.Web,
            firstSessionDate: firstSessionDateValue === null || firstSessionDateValue === void 0 ? void 0 : firstSessionDateValue.date,
        });
    }
    dispose() {
        this.eventDispose.dispose();
    }
    async setLogLevel() {
        const value = await this.loggerManger.getGlobalLogLevel();
        await this.proxy.$setLogLevel(value);
    }
    async $clipboardReadText() {
        try {
            const value = await this.clipboardService.readText();
            return value;
        }
        catch (e) {
            return '';
        }
    }
    $clipboardWriteText(text) {
        return new Promise(async (resolve) => {
            try {
                await this.clipboardService.writeText(text);
            }
            catch (e) { }
            resolve();
        });
    }
    async $openExternal(target) {
        if (this.isSupportedLink(ide_core_common_1.URI.from(target))) {
            return await this.openerService.open(target.toString(true));
        }
        return false;
    }
    getWindowId() {
        return this.applicationService.windowId;
    }
    async $asExternalUri(target) {
        const { uriScheme } = this.appConfig;
        const uri = ide_core_common_1.URI.from(target);
        // 如果是 appUriScheme，则在 query 加入当前 windowId
        if (uri.scheme === uriScheme) {
            const windowId = this.getWindowId();
            let query = uri.query;
            if (!query) {
                query = `windowId=${windowId}`;
            }
            else {
                query += `&windowId=${windowId}`;
            }
            return uri.withQuery(query).codeUri;
        }
        const externalUri = this.externalUriService.resolveExternalUri(uri);
        return externalUri.codeUri;
    }
};
exports.MainThreadEnv = MainThreadEnv;
tslib_1.__decorate([
    (0, di_1.Autowired)(browser_1.ILoggerManagerClient),
    tslib_1.__metadata("design:type", Object)
], MainThreadEnv.prototype, "loggerManger", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_core_browser_1.IOpenerService),
    tslib_1.__metadata("design:type", Object)
], MainThreadEnv.prototype, "openerService", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_core_browser_1.IClipboardService),
    tslib_1.__metadata("design:type", Object)
], MainThreadEnv.prototype, "clipboardService", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_core_browser_1.IExternalUriService),
    tslib_1.__metadata("design:type", Object)
], MainThreadEnv.prototype, "externalUriService", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_core_browser_1.AppConfig),
    tslib_1.__metadata("design:type", Object)
], MainThreadEnv.prototype, "appConfig", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_core_common_1.IApplicationService),
    tslib_1.__metadata("design:type", Object)
], MainThreadEnv.prototype, "applicationService", void 0);
exports.MainThreadEnv = MainThreadEnv = tslib_1.__decorate([
    (0, di_1.Injectable)({ multiple: true }),
    tslib_1.__param(0, (0, di_1.Optional)(ide_connection_1.IRPCProtocol)),
    tslib_1.__metadata("design:paramtypes", [Object, main_thread_storage_1.MainThreadStorage])
], MainThreadEnv);
//# sourceMappingURL=main.thread.env.js.map