
import { __decorate } from '../../../../../../../external/tslib/tslib.es6.js';
import { Schemas } from '../../../../base/common/network.js';
import { joinPath } from '../../../../base/common/resources.js';
import { URI } from '../../../../base/common/uri.js';
import { memoize } from '../../../../base/common/decorators.js';
import { onUnexpectedError } from '../../../../base/common/errors.js';
import { parseLineAndColumnAware } from '../../../../base/common/extpath.js';
import { LogLevelToString } from '../../../../platform/log/common/log.js';
import { isUndefined } from '../../../../base/common/types.js';
import { EXTENSION_IDENTIFIER_WITH_LOG_REGEX } from '../../../../platform/environment/common/environmentService.js';

class BrowserWorkbenchEnvironmentService {
    get remoteAuthority() {
        return this.options.remoteAuthority;
    }
    get expectsResolverExtension() {
        return !!this.options.remoteAuthority?.includes("+") && !this.options.webSocketFactory;
    }
    get isBuilt() {
        return !!this.productService.commit;
    }
    get logLevel() {
        const logLevelFromPayload = this.payload?.get("logLevel");
        if (logLevelFromPayload) {
            return logLevelFromPayload.split(",").find(entry => !EXTENSION_IDENTIFIER_WITH_LOG_REGEX.test(entry));
        }
        return this.options.developmentOptions?.logLevel !== undefined ? LogLevelToString(this.options.developmentOptions?.logLevel) : undefined;
    }
    get extensionLogLevel() {
        const logLevelFromPayload = this.payload?.get("logLevel");
        if (logLevelFromPayload) {
            const result = [];
            for (const entry of logLevelFromPayload.split(",")) {
                const matches = EXTENSION_IDENTIFIER_WITH_LOG_REGEX.exec(entry);
                if (matches?.[1] && matches[2]) {
                    result.push([matches[1], matches[2]]);
                }
            }
            return result.length ? result : undefined;
        }
        return this.options.developmentOptions?.extensionLogLevel !== undefined ? this.options.developmentOptions?.extensionLogLevel.map(([extension, logLevel]) => ([extension, LogLevelToString(logLevel)])) : undefined;
    }
    get profDurationMarkers() {
        const profDurationMarkersFromPayload = this.payload?.get("profDurationMarkers");
        if (profDurationMarkersFromPayload) {
            const result = [];
            for (const entry of profDurationMarkersFromPayload.split(",")) {
                result.push(entry);
            }
            return result.length === 2 ? result : undefined;
        }
        return undefined;
    }
    get windowLogsPath() {
        return this.logsHome;
    }
    get logFile() {
        return joinPath(this.windowLogsPath, "window.log");
    }
    get userRoamingDataHome() {
        return URI.file("/User").with({
            scheme: Schemas.vscodeUserData
        });
    }
    get argvResource() {
        return joinPath(this.userRoamingDataHome, "argv.json");
    }
    get cacheHome() {
        return joinPath(this.userRoamingDataHome, "caches");
    }
    get workspaceStorageHome() {
        return joinPath(this.userRoamingDataHome, "workspaceStorage");
    }
    get localHistoryHome() {
        return joinPath(this.userRoamingDataHome, "History");
    }
    get stateResource() {
        return joinPath(this.userRoamingDataHome, "State", "storage.json");
    }
    get userDataSyncHome() {
        return joinPath(this.userRoamingDataHome, "sync", this.workspaceId);
    }
    get sync() {
        return undefined;
    }
    get keyboardLayoutResource() {
        return joinPath(this.userRoamingDataHome, "keyboardLayout.json");
    }
    get untitledWorkspacesHome() {
        return joinPath(this.userRoamingDataHome, "Workspaces");
    }
    get serviceMachineIdResource() {
        return joinPath(this.userRoamingDataHome, "machineid");
    }
    get extHostLogsPath() {
        return joinPath(this.logsHome, "exthost");
    }
    get debugExtensionHost() {
        if (!this.extensionHostDebugEnvironment) {
            this.extensionHostDebugEnvironment = this.resolveExtensionHostDebugEnvironment();
        }
        return this.extensionHostDebugEnvironment.params;
    }
    get isExtensionDevelopment() {
        if (!this.extensionHostDebugEnvironment) {
            this.extensionHostDebugEnvironment = this.resolveExtensionHostDebugEnvironment();
        }
        return this.extensionHostDebugEnvironment.isExtensionDevelopment;
    }
    get extensionDevelopmentLocationURI() {
        if (!this.extensionHostDebugEnvironment) {
            this.extensionHostDebugEnvironment = this.resolveExtensionHostDebugEnvironment();
        }
        return this.extensionHostDebugEnvironment.extensionDevelopmentLocationURI;
    }
    get extensionDevelopmentLocationKind() {
        if (!this.extensionHostDebugEnvironment) {
            this.extensionHostDebugEnvironment = this.resolveExtensionHostDebugEnvironment();
        }
        return this.extensionHostDebugEnvironment.extensionDevelopmentKind;
    }
    get extensionTestsLocationURI() {
        if (!this.extensionHostDebugEnvironment) {
            this.extensionHostDebugEnvironment = this.resolveExtensionHostDebugEnvironment();
        }
        return this.extensionHostDebugEnvironment.extensionTestsLocationURI;
    }
    get extensionEnabledProposedApi() {
        if (!this.extensionHostDebugEnvironment) {
            this.extensionHostDebugEnvironment = this.resolveExtensionHostDebugEnvironment();
        }
        return this.extensionHostDebugEnvironment.extensionEnabledProposedApi;
    }
    get debugRenderer() {
        if (!this.extensionHostDebugEnvironment) {
            this.extensionHostDebugEnvironment = this.resolveExtensionHostDebugEnvironment();
        }
        return this.extensionHostDebugEnvironment.debugRenderer;
    }
    get enableSmokeTestDriver() {
        return this.options.developmentOptions?.enableSmokeTestDriver;
    }
    get disableExtensions() {
        return this.payload?.get("disableExtensions") === "true" ? true : this.options.disabledExtensions ?? false;
    }
    get enableExtensions() {
        return this.options.enabledExtensions;
    }
    get webviewExternalEndpoint() {
        const endpoint = this.options.webviewEndpoint || this.productService.webviewContentExternalBaseUrlTemplate || "https://{{uuid}}.vscode-cdn.net/{{quality}}/{{commit}}/out/vs/workbench/contrib/webview/browser/pre/";
        const webviewExternalEndpointCommit = this.payload?.get("webviewExternalEndpointCommit");
        return endpoint.replace(
            "{{commit}}",
            webviewExternalEndpointCommit ?? this.productService.commit ?? "ef65ac1ba57f57f2a3961bfe94aa20481caca4c6"
        ).replace(
            "{{quality}}",
            (webviewExternalEndpointCommit ? "insider" : this.productService.quality) ?? "insider"
        );
    }
    get extensionTelemetryLogResource() {
        return joinPath(this.logsHome, "extensionTelemetry.log");
    }
    get disableTelemetry() {
        return false;
    }
    get disableExperiments() {
        return false;
    }
    get verbose() {
        return this.payload?.get("verbose") === "true";
    }
    get logExtensionHostCommunication() {
        return this.payload?.get("logExtensionHostCommunication") === "true";
    }
    get skipReleaseNotes() {
        return this.payload?.get("skipReleaseNotes") === "true";
    }
    get skipWelcome() {
        return this.payload?.get("skipWelcome") === "true";
    }
    get disableWorkspaceTrust() {
        return !this.options.enableWorkspaceTrust;
    }
    get isSessionsWindow() {
        return this.payload?.get("isSessionsWindow") === "true";
    }
    get profile() {
        return this.payload?.get("profile");
    }
    get editSessionId() {
        return this.options.editSessionId;
    }
    constructor(workspaceId, logsHome, options, productService) {
        this.workspaceId = workspaceId;
        this.logsHome = logsHome;
        this.options = options;
        this.productService = productService;
        this.extensionHostDebugEnvironment = undefined;
        if (options.workspaceProvider && Array.isArray(options.workspaceProvider.payload)) {
            try {
                this.payload = ( new Map(options.workspaceProvider.payload));
            } catch (error) {
                onUnexpectedError(error);
            }
        }
    }
    resolveExtensionHostDebugEnvironment() {
        const extensionHostDebugEnvironment = {
            params: {
                port: null,
                break: false
            },
            debugRenderer: false,
            isExtensionDevelopment: false,
            extensionDevelopmentLocationURI: undefined,
            extensionDevelopmentKind: undefined
        };
        if (this.payload) {
            for (const [key, value] of this.payload) {
                switch (key) {
                case "extensionDevelopmentPath":
                    if (!extensionHostDebugEnvironment.extensionDevelopmentLocationURI) {
                        extensionHostDebugEnvironment.extensionDevelopmentLocationURI = [];
                    }
                    extensionHostDebugEnvironment.extensionDevelopmentLocationURI.push(( URI.parse(value)));
                    extensionHostDebugEnvironment.isExtensionDevelopment = true;
                    break;
                case "extensionDevelopmentKind":
                    extensionHostDebugEnvironment.extensionDevelopmentKind = [value];
                    break;
                case "extensionTestsPath":
                    extensionHostDebugEnvironment.extensionTestsLocationURI = ( URI.parse(value));
                    break;
                case "debugRenderer":
                    extensionHostDebugEnvironment.debugRenderer = value === "true";
                    break;
                case "debugId":
                    extensionHostDebugEnvironment.params.debugId = value;
                    break;
                case "inspect-brk-extensions":
                    extensionHostDebugEnvironment.params.port = parseInt(value);
                    extensionHostDebugEnvironment.params.break = true;
                    break;
                case "inspect-extensions":
                    extensionHostDebugEnvironment.params.port = parseInt(value);
                    break;
                case "extensionEnvironment":
                    try {
                        extensionHostDebugEnvironment.params.env = JSON.parse(value);
                    } catch (error) {
                        onUnexpectedError(error);
                    }
                    break;
                case "enableProposedApi":
                    extensionHostDebugEnvironment.extensionEnabledProposedApi = [];
                    break;
                }
            }
        }
        const developmentOptions = this.options.developmentOptions;
        if (developmentOptions && !extensionHostDebugEnvironment.isExtensionDevelopment) {
            if (developmentOptions.extensions?.length) {
                extensionHostDebugEnvironment.extensionDevelopmentLocationURI = ( developmentOptions.extensions.map(e => URI.revive(e)));
                extensionHostDebugEnvironment.isExtensionDevelopment = true;
            }
            if (developmentOptions.extensionTestsPath) {
                extensionHostDebugEnvironment.extensionTestsLocationURI = URI.revive(developmentOptions.extensionTestsPath);
            }
        }
        return extensionHostDebugEnvironment;
    }
    get filesToOpenOrCreate() {
        if (this.payload) {
            const fileToOpen = this.payload.get("openFile");
            if (fileToOpen) {
                const fileUri = ( URI.parse(fileToOpen));
                if (( this.payload.has("gotoLineMode"))) {
                    const pathColumnAware = parseLineAndColumnAware(fileUri.path);
                    return [{
                        fileUri: fileUri.with({
                            path: pathColumnAware.path
                        }),
                        options: {
                            selection: !isUndefined(pathColumnAware.line) ? {
                                startLineNumber: pathColumnAware.line,
                                startColumn: pathColumnAware.column || 1
                            } : undefined
                        }
                    }];
                }
                return [{
                    fileUri
                }];
            }
        }
        return undefined;
    }
    get filesToDiff() {
        if (this.payload) {
            const fileToDiffPrimary = this.payload.get("diffFilePrimary");
            const fileToDiffSecondary = this.payload.get("diffFileSecondary");
            if (fileToDiffPrimary && fileToDiffSecondary) {
                return [{
                    fileUri: ( URI.parse(fileToDiffSecondary))
                }, {
                    fileUri: ( URI.parse(fileToDiffPrimary))
                }];
            }
        }
        return undefined;
    }
    get filesToMerge() {
        if (this.payload) {
            const fileToMerge1 = this.payload.get("mergeFile1");
            const fileToMerge2 = this.payload.get("mergeFile2");
            const fileToMergeBase = this.payload.get("mergeFileBase");
            const fileToMergeResult = this.payload.get("mergeFileResult");
            if (fileToMerge1 && fileToMerge2 && fileToMergeBase && fileToMergeResult) {
                return [{
                    fileUri: ( URI.parse(fileToMerge1))
                }, {
                    fileUri: ( URI.parse(fileToMerge2))
                }, {
                    fileUri: ( URI.parse(fileToMergeBase))
                }, {
                    fileUri: ( URI.parse(fileToMergeResult))
                }];
            }
        }
        return undefined;
    }
}
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate(
    [memoize],
    BrowserWorkbenchEnvironmentService.prototype,
    "remoteAuthority",
    null
));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate(
    [memoize],
    BrowserWorkbenchEnvironmentService.prototype,
    "expectsResolverExtension",
    null
));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate([memoize], BrowserWorkbenchEnvironmentService.prototype, "isBuilt", null));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate([memoize], BrowserWorkbenchEnvironmentService.prototype, "logLevel", null));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate(
    [memoize],
    BrowserWorkbenchEnvironmentService.prototype,
    "windowLogsPath",
    null
));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate([memoize], BrowserWorkbenchEnvironmentService.prototype, "logFile", null));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate(
    [memoize],
    BrowserWorkbenchEnvironmentService.prototype,
    "userRoamingDataHome",
    null
));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate(
    [memoize],
    BrowserWorkbenchEnvironmentService.prototype,
    "argvResource",
    null
));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate([memoize], BrowserWorkbenchEnvironmentService.prototype, "cacheHome", null));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate(
    [memoize],
    BrowserWorkbenchEnvironmentService.prototype,
    "workspaceStorageHome",
    null
));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate(
    [memoize],
    BrowserWorkbenchEnvironmentService.prototype,
    "localHistoryHome",
    null
));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate(
    [memoize],
    BrowserWorkbenchEnvironmentService.prototype,
    "stateResource",
    null
));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate(
    [memoize],
    BrowserWorkbenchEnvironmentService.prototype,
    "userDataSyncHome",
    null
));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate([memoize], BrowserWorkbenchEnvironmentService.prototype, "sync", null));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate(
    [memoize],
    BrowserWorkbenchEnvironmentService.prototype,
    "keyboardLayoutResource",
    null
));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate(
    [memoize],
    BrowserWorkbenchEnvironmentService.prototype,
    "untitledWorkspacesHome",
    null
));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate(
    [memoize],
    BrowserWorkbenchEnvironmentService.prototype,
    "serviceMachineIdResource",
    null
));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate(
    [memoize],
    BrowserWorkbenchEnvironmentService.prototype,
    "extHostLogsPath",
    null
));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate(
    [memoize],
    BrowserWorkbenchEnvironmentService.prototype,
    "debugExtensionHost",
    null
));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate(
    [memoize],
    BrowserWorkbenchEnvironmentService.prototype,
    "isExtensionDevelopment",
    null
));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate(
    [memoize],
    BrowserWorkbenchEnvironmentService.prototype,
    "extensionDevelopmentLocationURI",
    null
));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate(
    [memoize],
    BrowserWorkbenchEnvironmentService.prototype,
    "extensionDevelopmentLocationKind",
    null
));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate(
    [memoize],
    BrowserWorkbenchEnvironmentService.prototype,
    "extensionTestsLocationURI",
    null
));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate(
    [memoize],
    BrowserWorkbenchEnvironmentService.prototype,
    "extensionEnabledProposedApi",
    null
));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate(
    [memoize],
    BrowserWorkbenchEnvironmentService.prototype,
    "debugRenderer",
    null
));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate(
    [memoize],
    BrowserWorkbenchEnvironmentService.prototype,
    "enableSmokeTestDriver",
    null
));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate(
    [memoize],
    BrowserWorkbenchEnvironmentService.prototype,
    "disableExtensions",
    null
));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate(
    [memoize],
    BrowserWorkbenchEnvironmentService.prototype,
    "enableExtensions",
    null
));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate(
    [memoize],
    BrowserWorkbenchEnvironmentService.prototype,
    "webviewExternalEndpoint",
    null
));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate(
    [memoize],
    BrowserWorkbenchEnvironmentService.prototype,
    "extensionTelemetryLogResource",
    null
));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate(
    [memoize],
    BrowserWorkbenchEnvironmentService.prototype,
    "disableTelemetry",
    null
));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate(
    [memoize],
    BrowserWorkbenchEnvironmentService.prototype,
    "disableExperiments",
    null
));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate([memoize], BrowserWorkbenchEnvironmentService.prototype, "verbose", null));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate(
    [memoize],
    BrowserWorkbenchEnvironmentService.prototype,
    "logExtensionHostCommunication",
    null
));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate(
    [memoize],
    BrowserWorkbenchEnvironmentService.prototype,
    "skipReleaseNotes",
    null
));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate(
    [memoize],
    BrowserWorkbenchEnvironmentService.prototype,
    "skipWelcome",
    null
));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate(
    [memoize],
    BrowserWorkbenchEnvironmentService.prototype,
    "disableWorkspaceTrust",
    null
));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate(
    [memoize],
    BrowserWorkbenchEnvironmentService.prototype,
    "isSessionsWindow",
    null
));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate([memoize], BrowserWorkbenchEnvironmentService.prototype, "profile", null));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate(
    [memoize],
    BrowserWorkbenchEnvironmentService.prototype,
    "editSessionId",
    null
));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate(
    [memoize],
    BrowserWorkbenchEnvironmentService.prototype,
    "filesToOpenOrCreate",
    null
));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate(
    [memoize],
    BrowserWorkbenchEnvironmentService.prototype,
    "filesToDiff",
    null
));
BrowserWorkbenchEnvironmentService.__decorator = ( __decorate(
    [memoize],
    BrowserWorkbenchEnvironmentService.prototype,
    "filesToMerge",
    null
));

export { BrowserWorkbenchEnvironmentService };
