"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.envValue = exports.Env = void 0;
exports.createEnvApiFactory = createEnvApiFactory;
const tslib_1 = require("tslib");
const crypto_1 = require("crypto");
const address_1 = tslib_1.__importDefault(require("address"));
const ide_core_common_1 = require("@opensumi/ide-core-common");
const vscode_1 = require("../../../../common/vscode");
const telemetry_ext_1 = require("./telemetry-ext");
class Env {
    constructor() {
        address_1.default.mac((err, macAddress) => {
            if (!err && macAddress) {
                this.macMachineId = (0, crypto_1.createHash)('sha256').update(macAddress, 'utf8').digest('hex');
            }
            else {
                this.macMachineId = (0, ide_core_common_1.uuid)();
            }
        });
        this.sessionId = (0, ide_core_common_1.uuid)();
    }
    get machineId() {
        return this.macMachineId;
    }
}
exports.Env = Env;
exports.envValue = new Env();
const telemetryExt = new telemetry_ext_1.TelemetryExtImpl();
function createEnvApiFactory(rpcProtocol, extension, envHost, exthostTerminal) {
    const proxy = rpcProtocol.getProxy(vscode_1.MainThreadAPIIdentifier.MainThreadEnv);
    const values = envHost.getEnvValues();
    const env = {
        appName: values.appName,
        appRoot: values.appRoot,
        appHost: values.appHost,
        uriScheme: values.uriScheme,
        language: values.language,
        sessionId: exports.envValue.sessionId,
        machineId: exports.envValue.machineId,
        remoteName: 'remoteName',
        shell: exthostTerminal.shellPath,
        uiKind: values.uiKind,
        clipboard: {
            readText() {
                return proxy.$clipboardReadText();
            },
            writeText(value) {
                return proxy.$clipboardWriteText(value);
            },
        },
        openExternal(target) {
            return proxy.$openExternal(target);
        },
        asExternalUri(target) {
            return envHost.asExternalUri(target);
        },
        get logLevel() {
            return envHost.logLevel;
        },
        get onDidChangeLogLevel() {
            return envHost.logLevelChangeEmitter.event;
        },
        get environmentVariableCollection() {
            return exthostTerminal.getEnvironmentVariableCollection(extension);
        },
        createTelemetryLogger(sender, options) {
            return telemetryExt.createTelemetryLogger(sender, options);
        },
        get isNewAppInstall() {
            const { firstSessionDate } = values;
            if (!firstSessionDate) {
                return true;
            }
            const installAge = Date.now() - new Date(firstSessionDate).getTime();
            return isNaN(installAge) ? false : installAge < 1000 * 60 * 60 * 24; // install age is less than a day
        },
        /**
         * 兼容 vscode api 用，该 api 主要与用户个人数据收集相关：https://privacy.microsoft.com/zh-cn/privacystatement
         */
        get isTelemetryEnabled() {
            return telemetryExt.isTelemetryEnabled;
        },
        /**
         * 同 isTelemetryEnabled
         */
        get onDidChangeTelemetryEnabled() {
            return telemetryExt.onDidChangeTelemetryEnabled;
        },
        get onDidChangeShell() {
            return exthostTerminal.onDidChangeShell;
        },
    };
    return Object.freeze(env);
}
//# sourceMappingURL=envApiFactory.js.map