
import { __decorate, __param } from '../../../../../../../external/tslib/tslib.es6.js';
import { Emitter } from '../../../../base/common/event.js';
import { Disposable } from '../../../../base/common/lifecycle.js';
import { IProductService } from '../../../../platform/product/common/productService.service.js';
import { IAuthenticationService, IAuthenticationExtensionsService } from '../../authentication/common/authentication.service.js';
import { asJson } from '../../../../platform/request/common/request.js';
import { IRequestService } from '../../../../platform/request/common/request.service.js';
import { CancellationToken } from '../../../../base/common/cancellation.js';
import { IExtensionService } from '../../extensions/common/extensions.service.js';
import { ILogService } from '../../../../platform/log/common/log.service.js';
import { RawContextKey } from '../../../../platform/contextkey/common/contextkey.js';
import { IContextKeyService } from '../../../../platform/contextkey/common/contextkey.service.js';
import { registerWorkbenchContribution2, WorkbenchPhase } from '../../../common/contributions.js';
import { ThrottledDelayer, RunOnceScheduler, timeout } from '../../../../base/common/async.js';
import { IHostService } from '../../host/browser/host.service.js';
import { IConfigurationService } from '../../../../platform/configuration/common/configuration.service.js';
import { getErrorMessage } from '../../../../base/common/errors.js';
import { isUndefined, isString } from '../../../../base/common/types.js';
import { StorageScope, StorageTarget } from '../../../../platform/storage/common/storage.js';
import { IStorageService } from '../../../../platform/storage/common/storage.service.js';
import { IWorkbenchEnvironmentService } from '../../environment/common/environmentService.service.js';
import { isWeb } from '../../../../base/common/platform.js';
import { IDefaultAccountService } from '../../../../platform/defaultAccount/common/defaultAccount.service.js';
import { ITelemetryService } from '../../../../platform/telemetry/common/telemetry.service.js';
import { distinct } from '../../../../base/common/arrays.js';
import { equals } from '../../../../base/common/objects.js';
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';

const DEFAULT_ACCOUNT_SIGN_IN_COMMAND = "workbench.actions.accounts.signIn";
var DefaultAccountStatus;
(function(DefaultAccountStatus) {
    DefaultAccountStatus["Uninitialized"] = "uninitialized";
    DefaultAccountStatus["Unavailable"] = "unavailable";
    DefaultAccountStatus["Available"] = "available";
})(DefaultAccountStatus || (DefaultAccountStatus = {}));
const CONTEXT_DEFAULT_ACCOUNT_STATE = ( new RawContextKey("defaultAccountStatus", DefaultAccountStatus.Uninitialized));
const CACHED_POLICY_DATA_KEY = "defaultAccount.cachedPolicyData";
const ACCOUNT_DATA_POLL_INTERVAL_MS = 60 * 60 * 1000;
function toDefaultAccountConfig(defaultChatAgent) {
    return {
        preferredExtensions: [defaultChatAgent.chatExtensionId, defaultChatAgent.extensionId],
        authenticationProvider: {
            default: {
                id: defaultChatAgent.provider.default.id,
                name: defaultChatAgent.provider.default.name
            },
            enterprise: {
                id: defaultChatAgent.provider.enterprise.id,
                name: defaultChatAgent.provider.enterprise.name
            },
            enterpriseProviderConfig: `${defaultChatAgent.completionsAdvancedSetting}.authProvider`,
            enterpriseProviderUriSetting: defaultChatAgent.providerUriSetting,
            scopes: defaultChatAgent.providerScopes
        },
        entitlementUrl: defaultChatAgent.entitlementUrl,
        tokenEntitlementUrl: defaultChatAgent.tokenEntitlementUrl,
        mcpRegistryDataUrl: defaultChatAgent.mcpRegistryDataUrl
    };
}
let DefaultAccountProvider = class DefaultAccountProvider extends Disposable {
    get defaultAccount() {
        return this._defaultAccount?.defaultAccount ?? null;
    }
    get policyData() {
        return this._policyData?.policyData ?? null;
    }
    constructor(
        defaultAccountConfig,
        configurationService,
        authenticationService,
        authenticationExtensionsService,
        telemetryService,
        extensionService,
        requestService,
        logService,
        environmentService,
        contextKeyService,
        storageService,
        hostService
    ) {
        super();
        this.defaultAccountConfig = defaultAccountConfig;
        this.configurationService = configurationService;
        this.authenticationService = authenticationService;
        this.authenticationExtensionsService = authenticationExtensionsService;
        this.telemetryService = telemetryService;
        this.extensionService = extensionService;
        this.requestService = requestService;
        this.logService = logService;
        this.environmentService = environmentService;
        this.storageService = storageService;
        this.hostService = hostService;
        this._defaultAccount = null;
        this._policyData = null;
        this._onDidChangeDefaultAccount = this._register(( new Emitter()));
        this.onDidChangeDefaultAccount = this._onDidChangeDefaultAccount.event;
        this._onDidChangePolicyData = this._register(( new Emitter()));
        this.onDidChangePolicyData = this._onDidChangePolicyData.event;
        this.initialized = false;
        this.updateThrottler = this._register(( new ThrottledDelayer(100)));
        this.accountDataPollScheduler = this._register(( new RunOnceScheduler(() => this.refetchPolicyData(), ACCOUNT_DATA_POLL_INTERVAL_MS)));
        this.accountStatusContext = CONTEXT_DEFAULT_ACCOUNT_STATE.bindTo(contextKeyService);
        this._policyData = this.getCachedPolicyData();
        this.initPromise = this.init().finally(() => {
            this.telemetryService.publicLog2("defaultaccount:status", {
                status: this.defaultAccount ? "available" : "unavailable",
                initial: true
            });
            this.initialized = true;
        });
    }
    getCachedPolicyData() {
        const cached = this.storageService.get(CACHED_POLICY_DATA_KEY, StorageScope.APPLICATION);
        if (cached) {
            try {
                const {
                    accountId,
                    policyData
                } = JSON.parse(cached);
                if (accountId && policyData) {
                    this.logService.debug("[DefaultAccount] Initializing with cached policy data");
                    return {
                        accountId,
                        policyData,
                        isTokenEntitlementsDataFetched: false,
                        isMcpRegistryDataFetched: false
                    };
                }
            } catch (error) {
                this.logService.error(
                    "[DefaultAccount] Failed to parse cached policy data",
                    getErrorMessage(error)
                );
            }
        }
        return null;
    }
    async init() {
        if (isWeb && !this.environmentService.remoteAuthority) {
            this.logService.debug("[DefaultAccount] Running in web without remote, skipping initialization");
            return;
        }
        try {
            await this.extensionService.whenInstalledExtensionsRegistered();
            this.logService.debug("[DefaultAccount] Installed extensions registered.");
        } catch (error) {
            this.logService.error(
                "[DefaultAccount] Error while waiting for installed extensions to be registered",
                getErrorMessage(error)
            );
        }
        this.logService.debug("[DefaultAccount] Starting initialization");
        await this.doUpdateDefaultAccount(false, false);
        this.logService.debug("[DefaultAccount] Initialization complete");
        this._register(this.onDidChangeDefaultAccount(account => {
            this.telemetryService.publicLog2("defaultaccount:status", {
                status: account ? "available" : "unavailable",
                initial: false
            });
        }));
        this._register(this.authenticationService.onDidChangeSessions(e => {
            const defaultAccountProvider = this.getDefaultAccountAuthenticationProvider();
            if (e.providerId !== defaultAccountProvider.id) {
                return;
            }
            if (this.defaultAccount && e.event.removed?.some(session => session.id === this.defaultAccount?.sessionId)) {
                this.setDefaultAccount(null);
            } else {
                this.logService.debug(
                    "[DefaultAccount] Sessions changed for default account provider, updating default account"
                );
                this.updateDefaultAccount();
            }
        }));
        this._register(
            this.authenticationExtensionsService.onDidChangeAccountPreference(async e => {
                const defaultAccountProvider = this.getDefaultAccountAuthenticationProvider();
                if (e.providerId !== defaultAccountProvider.id) {
                    return;
                }
                this.logService.debug(
                    "[DefaultAccount] Account preference changed for default account provider, updating default account"
                );
                this.updateDefaultAccount();
            })
        );
        this._register(this.authenticationService.onDidRegisterAuthenticationProvider(e => {
            const defaultAccountProvider = this.getDefaultAccountAuthenticationProvider();
            if (e.id !== defaultAccountProvider.id) {
                return;
            }
            this.logService.debug(
                "[DefaultAccount] Default account provider registered, updating default account"
            );
            this.updateDefaultAccount();
        }));
        this._register(this.authenticationService.onDidUnregisterAuthenticationProvider(e => {
            const defaultAccountProvider = this.getDefaultAccountAuthenticationProvider();
            if (e.id !== defaultAccountProvider.id) {
                return;
            }
            this.logService.debug(
                "[DefaultAccount] Default account provider unregistered, updating default account"
            );
            this.updateDefaultAccount();
        }));
        this._register(this.hostService.onDidChangeFocus(focused => {
            if (focused && this._defaultAccount && this._policyData && (!this._policyData.isMcpRegistryDataFetched || !this._policyData.isTokenEntitlementsDataFetched)) {
                this.accountDataPollScheduler.cancel();
                this.logService.debug("[DefaultAccount] Window focused, updating default account");
                this.refresh(true);
            }
        }));
    }
    async refresh(donotFetchEntitlements = false) {
        if (!this.initialized) {
            await this.initPromise;
            return this.defaultAccount;
        }
        this.logService.debug("[DefaultAccount] Refreshing default account");
        await this.updateDefaultAccount(false, donotFetchEntitlements);
        return this.defaultAccount;
    }
    async refetchPolicyData() {
        if (!this.hostService.hasFocus) {
            this.scheduleAccountDataPoll();
            this.logService.debug(
                "[DefaultAccount] Skipping refetching policy data because window is not focused"
            );
            return;
        }
        this.logService.debug("[DefaultAccount] Refetching policy data for current default account");
        await this.updateDefaultAccount(true);
    }
    async updateDefaultAccount(donotUseCache = false, donotFetchEntitlements = false) {
        await this.updateThrottler.trigger(() => this.doUpdateDefaultAccount(donotUseCache, donotFetchEntitlements));
    }
    async doUpdateDefaultAccount(donotUseCache, donotFetchEntitlements) {
        try {
            const defaultAccount = await this.fetchDefaultAccount(donotUseCache, donotFetchEntitlements);
            this.setDefaultAccount(defaultAccount);
            this.scheduleAccountDataPoll();
        } catch (error) {
            this.logService.error(
                "[DefaultAccount] Error while updating default account",
                getErrorMessage(error)
            );
        }
    }
    async fetchDefaultAccount(donotUseCache, donotFetchEntitlements) {
        const defaultAccountProvider = this.getDefaultAccountAuthenticationProvider();
        this.logService.debug("[DefaultAccount] Default account provider ID:", defaultAccountProvider.id);
        const declaredProvider = this.authenticationService.declaredProviders.find(provider => provider.id === defaultAccountProvider.id);
        if (!declaredProvider) {
            this.logService.info(
                `[DefaultAccount] Authentication provider is not declared.`,
                defaultAccountProvider
            );
            return null;
        }
        return await this.getDefaultAccountForAuthenticationProvider(defaultAccountProvider, donotUseCache, donotFetchEntitlements);
    }
    setDefaultAccount(account) {
        if (equals(this._defaultAccount, account)) {
            return;
        }
        this.logService.trace("[DefaultAccount] Updating default account:", account);
        if (account) {
            this._defaultAccount = account;
            this.setPolicyData(account.policyData);
            this._onDidChangeDefaultAccount.fire(this._defaultAccount.defaultAccount);
            this.accountStatusContext.set(DefaultAccountStatus.Available);
            this.logService.debug("[DefaultAccount] Account status set to Available");
        } else {
            this._defaultAccount = null;
            this.setPolicyData(null);
            this._onDidChangeDefaultAccount.fire(null);
            this.accountDataPollScheduler.cancel();
            this.accountStatusContext.set(DefaultAccountStatus.Unavailable);
            this.logService.debug("[DefaultAccount] Account status set to Unavailable");
        }
    }
    setPolicyData(accountPolicyData) {
        if (equals(this._policyData, accountPolicyData)) {
            return;
        }
        this._policyData = accountPolicyData;
        this.cachePolicyData(accountPolicyData);
        this._onDidChangePolicyData.fire(this._policyData?.policyData ?? null);
    }
    cachePolicyData(accountPolicyData) {
        if (accountPolicyData) {
            this.logService.debug(
                "[DefaultAccount] Caching policy data for account:",
                accountPolicyData.accountId
            );
            this.storageService.store(
                CACHED_POLICY_DATA_KEY,
                JSON.stringify(accountPolicyData),
                StorageScope.APPLICATION,
                StorageTarget.MACHINE
            );
        } else {
            this.logService.debug("[DefaultAccount] Removing cached policy data");
            this.storageService.remove(CACHED_POLICY_DATA_KEY, StorageScope.APPLICATION);
        }
    }
    scheduleAccountDataPoll() {
        if (!this._defaultAccount) {
            return;
        }
        this.accountDataPollScheduler.schedule(ACCOUNT_DATA_POLL_INTERVAL_MS);
    }
    extractFromToken(token) {
        const result = ( new Map());
        const firstPart = token?.split(":")[0];
        const fields = firstPart?.split(";");
        for (const field of fields) {
            const [key, value] = field.split("=");
            result.set(key, value);
        }
        this.logService.debug(
            `[DefaultAccount] extractFromToken: ${JSON.stringify(Object.fromEntries(result))}`
        );
        return result;
    }
    async getDefaultAccountForAuthenticationProvider(authenticationProvider, donotUseCache, donotFetchEntitlements) {
        try {
            this.logService.debug(
                "[DefaultAccount] Getting Default Account from authenticated sessions for provider:",
                authenticationProvider.id
            );
            const sessions = await this.findMatchingProviderSession(
                authenticationProvider.id,
                this.defaultAccountConfig.authenticationProvider.scopes
            );
            if (!sessions?.length) {
                this.logService.debug(
                    "[DefaultAccount] No matching session found for provider:",
                    authenticationProvider.id
                );
                return null;
            }
            return this.getDefaultAccountFromAuthenticatedSessions(authenticationProvider, sessions, donotUseCache, donotFetchEntitlements);
        } catch (error) {
            this.logService.error(
                "[DefaultAccount] Failed to get default account for provider:",
                authenticationProvider.id,
                getErrorMessage(error)
            );
            return null;
        }
    }
    async getDefaultAccountFromAuthenticatedSessions(authenticationProvider, sessions, donotUseCache, donotFetchEntitlements) {
        try {
            const accountId = sessions[0].account.id;
            const accountPolicyData = this._policyData?.accountId === accountId ? this._policyData : undefined;
            const [entitlementsData, tokenEntitlementsData] = await Promise.all([
                donotFetchEntitlements && accountPolicyData && this._defaultAccount ? this._defaultAccount.defaultAccount.entitlementsData : this.getEntitlements(sessions),
                this.getTokenEntitlements(sessions, donotUseCache ? undefined : accountPolicyData)
            ]);
            let isTokenEntitlementsDataFetched = false;
            let isMcpRegistryDataFetched = false;
            let policyData = accountPolicyData?.policyData ? {
                ...accountPolicyData.policyData
            } : undefined;
            if (tokenEntitlementsData) {
                isTokenEntitlementsDataFetched = true;
                policyData = policyData ?? {};
                policyData.chat_agent_enabled = tokenEntitlementsData.chat_agent_enabled;
                policyData.chat_preview_features_enabled = tokenEntitlementsData.chat_preview_features_enabled;
                policyData.mcp = tokenEntitlementsData.mcp;
                if (policyData.mcp) {
                    const mcpRegistryProvider = await this.getMcpRegistryProvider(sessions, donotUseCache ? undefined : accountPolicyData);
                    if (!isUndefined(mcpRegistryProvider)) {
                        isMcpRegistryDataFetched = true;
                        policyData.mcpRegistryUrl = mcpRegistryProvider?.url;
                        policyData.mcpAccess = mcpRegistryProvider?.registry_access;
                    }
                } else {
                    policyData.mcpRegistryUrl = undefined;
                    policyData.mcpAccess = undefined;
                }
            }
            const defaultAccount = {
                authenticationProvider,
                sessionId: sessions[0].id,
                enterprise: authenticationProvider.enterprise || sessions[0].account.label.includes("_"),
                entitlementsData
            };
            this.logService.debug(
                "[DefaultAccount] Successfully created default account for provider:",
                authenticationProvider.id
            );
            return {
                defaultAccount,
                policyData: policyData ? {
                    accountId,
                    policyData,
                    isTokenEntitlementsDataFetched,
                    isMcpRegistryDataFetched
                } : null
            };
        } catch (error) {
            this.logService.error(
                "[DefaultAccount] Failed to create default account for provider:",
                authenticationProvider.id,
                getErrorMessage(error)
            );
            return null;
        }
    }
    async findMatchingProviderSession(authProviderId, allScopes) {
        const sessions = await this.getSessions(authProviderId);
        const matchingSessions = [];
        for (const session of sessions) {
            this.logService.debug("[DefaultAccount] Checking session with scopes", session.scopes);
            for (const scopes of allScopes) {
                if (this.scopesMatch(session.scopes, scopes)) {
                    matchingSessions.push(session);
                }
            }
        }
        return matchingSessions.length > 0 ? matchingSessions : undefined;
    }
    async getSessions(authProviderId) {
        for (let attempt = 1; attempt <= 3; attempt++) {
            try {
                let preferredAccount;
                let preferredAccountName;
                for (const preferredExtension of this.defaultAccountConfig.preferredExtensions) {
                    preferredAccountName = this.authenticationExtensionsService.getAccountPreference(preferredExtension, authProviderId);
                    if (preferredAccountName) {
                        break;
                    }
                }
                for (const account of await this.authenticationService.getAccounts(authProviderId)) {
                    if (account.label === preferredAccountName) {
                        preferredAccount = account;
                        break;
                    }
                }
                return await this.authenticationService.getSessions(authProviderId, undefined, {
                    account: preferredAccount
                }, true);
            } catch (error) {
                this.logService.warn(
                    `[DefaultAccount] Attempt ${attempt} to get sessions failed:`,
                    getErrorMessage(error)
                );
                if (attempt === 3) {
                    throw error;
                }
                await timeout(500);
            }
        }
        throw ( new Error("Unable to get sessions after multiple attempts"));
    }
    scopesMatch(scopes, expectedScopes) {
        return expectedScopes.every(scope => scopes.includes(scope));
    }
    async getTokenEntitlements(sessions, accountPolicyData) {
        if (accountPolicyData?.isTokenEntitlementsDataFetched) {
            this.logService.debug("[DefaultAccount] Using last fetched token entitlements data");
            return accountPolicyData.policyData;
        }
        return await this.requestTokenEntitlements(sessions);
    }
    async requestTokenEntitlements(sessions) {
        const tokenEntitlementsUrl = this.getTokenEntitlementUrl();
        if (!tokenEntitlementsUrl) {
            this.logService.debug("[DefaultAccount] No token entitlements URL found");
            return undefined;
        }
        this.logService.debug("[DefaultAccount] Fetching token entitlements from:", tokenEntitlementsUrl);
        const response = await this.request(tokenEntitlementsUrl, "GET", undefined, sessions, CancellationToken.None);
        if (!response) {
            return undefined;
        }
        if (response.res.statusCode && response.res.statusCode !== 200) {
            this.logService.trace(
                `[DefaultAccount] unexpected status code ${response.res.statusCode} while fetching token entitlements`
            );
            return undefined;
        }
        try {
            const chatData = await asJson(response);
            if (chatData) {
                const tokenMap = this.extractFromToken(chatData.token);
                return {
                    chat_preview_features_enabled: tokenMap.get("editor_preview_features") !== "0",
                    chat_agent_enabled: tokenMap.get("agent_mode") !== "0",
                    mcp: tokenMap.get("mcp") !== "0"
                };
            }
            this.logService.error("Failed to fetch token entitlements", "No data returned");
        } catch (error) {
            this.logService.error("Failed to fetch token entitlements", getErrorMessage(error));
        }
        return undefined;
    }
    async getEntitlements(sessions) {
        const entitlementUrl = this.getEntitlementUrl();
        if (!entitlementUrl) {
            this.logService.debug("[DefaultAccount] No chat entitlements URL found");
            return undefined;
        }
        this.logService.debug("[DefaultAccount] Fetching entitlements from:", entitlementUrl);
        const response = await this.request(entitlementUrl, "GET", undefined, sessions, CancellationToken.None);
        if (!response) {
            return undefined;
        }
        if (response.res.statusCode && response.res.statusCode !== 200) {
            this.logService.trace(
                `[DefaultAccount] unexpected status code ${response.res.statusCode} while fetching entitlements`
            );
            return (
                (
                response.res.statusCode === 401 || response.res.statusCode === 404) ? null : undefined
            );
        }
        try {
            const data = await asJson(response);
            if (data) {
                return data;
            }
            this.logService.error("[DefaultAccount] Failed to fetch entitlements", "No data returned");
        } catch (error) {
            this.logService.error("[DefaultAccount] Failed to fetch entitlements", getErrorMessage(error));
        }
        return undefined;
    }
    async getMcpRegistryProvider(sessions, accountPolicyData) {
        if (accountPolicyData?.isMcpRegistryDataFetched) {
            this.logService.debug("[DefaultAccount] Using last fetched MCP registry data");
            return accountPolicyData.policyData.mcpRegistryUrl && accountPolicyData.policyData.mcpAccess ? {
                url: accountPolicyData.policyData.mcpRegistryUrl,
                registry_access: accountPolicyData.policyData.mcpAccess
            } : null;
        }
        return await this.requestMcpRegistryProvider(sessions);
    }
    async requestMcpRegistryProvider(sessions) {
        const mcpRegistryDataUrl = this.getMcpRegistryDataUrl();
        if (!mcpRegistryDataUrl) {
            this.logService.debug("[DefaultAccount] No MCP registry data URL found");
            return null;
        }
        this.logService.debug("[DefaultAccount] Fetching MCP registry data from:", mcpRegistryDataUrl);
        const response = await this.request(mcpRegistryDataUrl, "GET", undefined, sessions, CancellationToken.None);
        if (!response) {
            return undefined;
        }
        if (response.res.statusCode && response.res.statusCode !== 200) {
            if (response.res.statusCode === 401) {
                this.logService.debug(
                    "[DefaultAccount] Unauthorized (401) when fetching MCP registry data, treating as no registry available"
                );
                return null;
            }
            if (response.res.statusCode === 404) {
                this.logService.debug(
                    "[DefaultAccount] MCP registry endpoint not found (404), treating as no registry available"
                );
                return null;
            }
            if (response.res.statusCode === 429) {
                this.logService.debug(
                    `[DefaultAccount] Received 429 Too Many Requests for MCP registry data, treating as no registry available and not retrying immediately.`
                );
                return null;
            }
            this.logService.debug(
                `[DefaultAccount] unexpected status code ${response.res.statusCode} while fetching MCP registry data`
            );
            return undefined;
        }
        try {
            const data = await asJson(response);
            if (data) {
                this.logService.debug("Fetched MCP registry providers", data.mcp_registries);
                return data.mcp_registries[0] ?? null;
            }
            this.logService.debug("No MCP registry providers content found in response");
            return null;
        } catch (error) {
            this.logService.error("Failed to fetch MCP registry providers", getErrorMessage(error));
            return undefined;
        }
    }
    async request(url, type, body, sessions, token) {
        let lastResponse;
        for (const session of sessions) {
            if (token.isCancellationRequested) {
                return lastResponse;
            }
            try {
                const response = await this.requestService.request({
                    type,
                    url,
                    data: type === "POST" ? JSON.stringify(body) : undefined,
                    disableCache: true,
                    headers: {
                        "Authorization": `Bearer ${session.accessToken}`
                    }
                }, token);
                const status = response.res.statusCode;
                if (status && status !== 200) {
                    if (status === 429) {
                        this.logService.warn(`[DefaultAccount] Received 429 Too Many Requests for ${url}.`);
                        return response;
                    }
                    lastResponse = response;
                    continue;
                }
                return response;
            } catch (error) {
                if (!token.isCancellationRequested) {
                    this.logService.error(`[DefaultAccount] request: error ${error}`, url);
                }
            }
        }
        if (!lastResponse) {
            this.logService.trace("[DefaultAccount]: No response received for request", url);
            return undefined;
        }
        return lastResponse;
    }
    getEntitlementUrl() {
        if (this.getDefaultAccountAuthenticationProvider().enterprise) {
            try {
                const enterpriseUrl = this.getEnterpriseUrl();
                if (!enterpriseUrl) {
                    return undefined;
                }
                return `${enterpriseUrl.protocol}//api.${enterpriseUrl.hostname}${enterpriseUrl.port ? ":" + enterpriseUrl.port : ""}/copilot_internal/user`;
            } catch (error) {
                this.logService.error(error);
            }
        }
        return this.defaultAccountConfig.entitlementUrl;
    }
    getTokenEntitlementUrl() {
        if (this.getDefaultAccountAuthenticationProvider().enterprise) {
            try {
                const enterpriseUrl = this.getEnterpriseUrl();
                if (!enterpriseUrl) {
                    return undefined;
                }
                return `${enterpriseUrl.protocol}//api.${enterpriseUrl.hostname}${enterpriseUrl.port ? ":" + enterpriseUrl.port : ""}/copilot_internal/v2/token`;
            } catch (error) {
                this.logService.error(error);
            }
        }
        return this.defaultAccountConfig.tokenEntitlementUrl;
    }
    getMcpRegistryDataUrl() {
        if (this.getDefaultAccountAuthenticationProvider().enterprise) {
            try {
                const enterpriseUrl = this.getEnterpriseUrl();
                if (!enterpriseUrl) {
                    return undefined;
                }
                return `${enterpriseUrl.protocol}//api.${enterpriseUrl.hostname}${enterpriseUrl.port ? ":" + enterpriseUrl.port : ""}/copilot/mcp_registry`;
            } catch (error) {
                this.logService.error(error);
            }
        }
        return this.defaultAccountConfig.mcpRegistryDataUrl;
    }
    getDefaultAccountAuthenticationProvider() {
        if (this.configurationService.getValue(this.defaultAccountConfig.authenticationProvider.enterpriseProviderConfig) === this.defaultAccountConfig.authenticationProvider.enterprise.id) {
            return {
                ...this.defaultAccountConfig.authenticationProvider.enterprise,
                enterprise: true
            };
        }
        return {
            ...this.defaultAccountConfig.authenticationProvider.default,
            enterprise: false
        };
    }
    getEnterpriseUrl() {
        const value = this.configurationService.getValue(
            this.defaultAccountConfig.authenticationProvider.enterpriseProviderUriSetting
        );
        if (!isString(value)) {
            return undefined;
        }
        return ( new URL(value));
    }
    async signIn(options) {
        const authProvider = this.getDefaultAccountAuthenticationProvider();
        if (!authProvider) {
            throw ( new Error("No default account provider configured"));
        }
        const {
            additionalScopes,
            ...sessionOptions
        } = options ?? {};
        const defaultAccountScopes = this.defaultAccountConfig.authenticationProvider.scopes[0];
        const scopes = additionalScopes ? distinct([...defaultAccountScopes, ...additionalScopes]) : defaultAccountScopes;
        const session = await this.authenticationService.createSession(authProvider.id, scopes, sessionOptions);
        for (const preferredExtension of this.defaultAccountConfig.preferredExtensions) {
            this.authenticationExtensionsService.updateAccountPreference(preferredExtension, authProvider.id, session.account);
        }
        await this.updateDefaultAccount();
        return this.defaultAccount;
    }
};
DefaultAccountProvider = ( __decorate([( __param(1, IConfigurationService)), ( __param(2, IAuthenticationService)), ( __param(3, IAuthenticationExtensionsService)), ( __param(4, ITelemetryService)), ( __param(5, IExtensionService)), ( __param(6, IRequestService)), ( __param(7, ILogService)), ( __param(8, IWorkbenchEnvironmentService)), ( __param(9, IContextKeyService)), ( __param(10, IStorageService)), ( __param(11, IHostService))], DefaultAccountProvider));
let DefaultAccountProviderContribution = class DefaultAccountProviderContribution extends Disposable {
    static {
        this.ID = "workbench.contributions.defaultAccountProvider";
    }
    constructor(productService, instantiationService, defaultAccountService) {
        super();
        if (productService.defaultChatAgent != null) {
            const defaultAccountProvider = this._register(instantiationService.createInstance(
                DefaultAccountProvider,
                toDefaultAccountConfig(productService.defaultChatAgent)
            ));
            defaultAccountService.setDefaultAccountProvider(defaultAccountProvider);
        }
    }
};
DefaultAccountProviderContribution = ( __decorate([( __param(0, IProductService)), ( __param(1, IInstantiationService)), ( __param(2, IDefaultAccountService))], DefaultAccountProviderContribution));
registerWorkbenchContribution2(
    DefaultAccountProviderContribution.ID,
    DefaultAccountProviderContribution,
    WorkbenchPhase.BlockStartup
);

export { DEFAULT_ACCOUNT_SIGN_IN_COMMAND };
