"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.activate = activate;
exports.deactivate = deactivate;
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = __importStar(require("vscode"));
const Api_1 = __importStar(require("./request/Api"));
// This method is called when your extension is activated
function activate(context) {
    console.log('AutoMindX 安装器已激活');
    /**注意 其实就是把更新当下载用，只要传入0.0.0即可*/
    // 创建状态栏项 - 下载按钮（使用明显的图标和文字）
    const statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right);
    statusBarItem.text = "$(cloud-download) 下载安装最新 AutoMind";
    statusBarItem.tooltip = "点击下载并安装最新版本的 AutoMind 插件";
    statusBarItem.command = 'automindX.install';
    statusBarItem.show();
    // 注册安装命令
    const installCommand = vscode.commands.registerCommand('automindX.install', async () => {
        await installPlugin(statusBarItem);
    });
    // 安装插件
    async function installPlugin(statusBarItem) {
        try {
            // 更新状态栏显示下载中，并禁用点击
            statusBarItem.text = "$(sync~spin) 正在下载安装...";
            statusBarItem.tooltip = "正在下载并安装 AutoMind 插件，请稍候...";
            statusBarItem.command = undefined; // 禁用点击
            // 显示进度条（支持取消）
            await vscode.window.withProgress({
                location: vscode.ProgressLocation.Notification,
                title: "安装 AutoMind 插件",
                cancellable: true
            }, async (progress, token) => {
                try {
                    // 检查是否已取消
                    if (token.isCancellationRequested) {
                        throw new Error('下载已取消');
                    }
                    await new Promise(resolve => setTimeout(resolve, 1000));
                    // 检查是否已取消
                    if (token.isCancellationRequested) {
                        throw new Error('下载已取消');
                    }
                    progress.report({ message: "正在下载插件..." });
                    // 监听取消事件
                    const cancelHandler = () => {
                        Api_1.Api.cancelDownload();
                    };
                    token.onCancellationRequested(cancelHandler);
                    // 调用API安装插件
                    await Api_1.default.checkForUpdateV2('0.0.0', context);
                    // 检查是否已取消
                    if (token.isCancellationRequested) {
                        throw new Error('下载已取消');
                    }
                    progress.report({ message: "正在安装插件..." });
                    // 等待一段时间让安装完成
                    await new Promise(resolve => setTimeout(resolve, 2000));
                    // 检查是否已取消
                    if (token.isCancellationRequested) {
                        throw new Error('下载已取消');
                    }
                    progress.report({ message: "安装完成！" });
                }
                catch (progressError) {
                    // 如果是取消操作，不显示错误消息
                    if (progressError.message === '下载已取消' || progressError.name === 'AbortError') {
                        throw new Error('下载已取消');
                    }
                    // 捕获进度条内的其他错误
                    throw progressError;
                }
            });
            // 安装完成，显示成功消息
            vscode.window.showInformationMessage('✨ AutoMind 插件安装完成！');
            // 恢复状态栏到初始状态
            statusBarItem.text = "$(cloud-download) 下载安装最新 AutoMind";
            statusBarItem.tooltip = "点击下载并安装最新版本的 AutoMind 插件";
            statusBarItem.command = 'automindX.install';
            // 提示用户重新加载窗口
            const reload = await vscode.window.showInformationMessage('✨ AutoMind 插件安装完成，请检查扩展列表AutoMind是否启用！需要重新加载窗口才能使用，是否现在重新加载？', '重新加载', '稍后');
            if (reload === '重新加载') {
                await vscode.commands.executeCommand('workbench.action.reloadWindow');
            }
        }
        catch (error) {
            // 如果是取消操作，显示提示信息而不是错误
            if (error.message === '下载已取消' || error.name === 'AbortError') {
                console.info('用户取消了下载');
                vscode.window.showInformationMessage('下载已取消');
            }
            else {
                console.error('安装插件时出错:', error);
                vscode.window.showErrorMessage('安装插件时出错: ' + error);
            }
            // 确保在任何错误情况下都恢复到初始状态
            statusBarItem.text = "$(cloud-download) 下载安装最新 AutoMind";
            statusBarItem.tooltip = "点击下载并安装最新版本的 AutoMind 插件";
            statusBarItem.command = 'automindX.install';
        }
    }
    context.subscriptions.push(installCommand, statusBarItem);
}
// This method is called when your extension is deactivated
function deactivate() {
    console.log('AutoMindX 安装器已停用');
}
//# sourceMappingURL=extension.js.map