
import { ExtensionIdentifier } from '../../../../../platform/extensions/common/extensions.js';
import product from '../../../../../platform/product/common/product.js';
import { localize } from '../../../../../nls.js';
import { EnablementState } from '../../../../services/extensionManagement/common/extensionManagement.js';

const defaultChat = {
    chatExtensionId: product.defaultChatAgent?.chatExtensionId ?? "",
    chatRefreshTokenCommand: product.defaultChatAgent?.chatRefreshTokenCommand ?? "",
    providerExtensionId: product.defaultChatAgent?.providerExtensionId ?? ""
};
var ChatSetupAnonymous;
(function(ChatSetupAnonymous) {
    ChatSetupAnonymous[ChatSetupAnonymous["Disabled"] = 0] = "Disabled";
    ChatSetupAnonymous[ChatSetupAnonymous["EnabledWithDialog"] = 1] = "EnabledWithDialog";
    ChatSetupAnonymous[ChatSetupAnonymous["EnabledWithoutDialog"] = 2] = "EnabledWithoutDialog";
})(ChatSetupAnonymous || (ChatSetupAnonymous = {}));
var ChatSetupStep;
(function(ChatSetupStep) {
    ChatSetupStep[ChatSetupStep["Initial"] = 1] = "Initial";
    ChatSetupStep[ChatSetupStep["SigningIn"] = 2] = "SigningIn";
    ChatSetupStep[ChatSetupStep["Installing"] = 3] = "Installing";
})(ChatSetupStep || (ChatSetupStep = {}));
var ChatSetupStrategy;
(function(ChatSetupStrategy) {
    ChatSetupStrategy[ChatSetupStrategy["Canceled"] = 0] = "Canceled";
    ChatSetupStrategy[ChatSetupStrategy["DefaultSetup"] = 1] = "DefaultSetup";
    ChatSetupStrategy[ChatSetupStrategy["SetupWithoutEnterpriseProvider"] = 2] = "SetupWithoutEnterpriseProvider";
    ChatSetupStrategy[ChatSetupStrategy["SetupWithEnterpriseProvider"] = 3] = "SetupWithEnterpriseProvider";
    ChatSetupStrategy[ChatSetupStrategy["SetupWithGoogleProvider"] = 4] = "SetupWithGoogleProvider";
    ChatSetupStrategy[ChatSetupStrategy["SetupWithAppleProvider"] = 5] = "SetupWithAppleProvider";
})(ChatSetupStrategy || (ChatSetupStrategy = {}));
function refreshTokens(commandService) {
    commandService.executeCommand(defaultChat.chatRefreshTokenCommand).catch(() => {});
}
function buildUpgradeUrlWithRedirect(baseUpgradeUrl, urlProtocol, quality) {
    const vscodeUri = `${urlProtocol}://${defaultChat.chatExtensionId}/upgrade-success`;
    const redirectHost = quality === "stable" ? "vscode.dev" : "insiders.vscode.dev";
    const returnTo = `https://${redirectHost}/redirect?url=${encodeURIComponent(vscodeUri)}`;
    const separator = baseUpgradeUrl.includes("?") ? "&" : "?";
    return `${baseUpgradeUrl}${separator}return_to=${encodeURIComponent(returnTo)}`;
}
async function maybeEnableAuthExtension(extensionsWorkbenchService, logService) {
    if (!defaultChat.providerExtensionId) {
        return false;
    }
    const providerExtension = extensionsWorkbenchService.local.find(
        e => ExtensionIdentifier.equals(e.identifier.id, defaultChat.providerExtensionId)
    );
    if (!providerExtension) {
        return false;
    }
    if (providerExtension.enablementState === EnablementState.DisabledGlobally || providerExtension.enablementState === EnablementState.DisabledWorkspace) {
        logService.info(
            `[chat setup] auth provider extension '${defaultChat.providerExtensionId}' is disabled, re-enabling it`
        );
        try {
            await extensionsWorkbenchService.setEnablement([providerExtension], EnablementState.EnabledGlobally);
            await extensionsWorkbenchService.updateRunningExtensions(( localize(6720, "Enabling GitHub Authentication")));
            return true;
        } catch (error) {
            logService.error(
                `[chat setup] failed to re-enable auth provider extension '${defaultChat.providerExtensionId}'`,
                error
            );
            return false;
        }
    }
    return false;
}

export { ChatSetupAnonymous, ChatSetupStep, ChatSetupStrategy, buildUpgradeUrlWithRedirect, maybeEnableAuthExtension, refreshTokens };
