"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MainThreadProgress = void 0;
const tslib_1 = require("tslib");
const di_1 = require("@opensumi/di");
const ide_connection_1 = require("@opensumi/ide-connection");
const progress_1 = require("@opensumi/ide-core-browser/lib/progress");
const ide_core_common_1 = require("@opensumi/ide-core-common");
const vscode_1 = require("../../../common/vscode");
let MainThreadProgress = class MainThreadProgress {
    constructor(rpcProtocol) {
        this.rpcProtocol = rpcProtocol;
        this.progress = new Map();
        this.proxy = this.rpcProtocol.getProxy(vscode_1.ExtHostAPIIdentifier.ExtHostProgress);
    }
    $startProgress(handle, options, extension) {
        const task = this.createTask(handle);
        if (options.location === ide_core_common_1.ProgressLocation.Notification && extension) {
            const notificationOptions = {
                ...options,
                location: ide_core_common_1.ProgressLocation.Notification,
            };
            options = notificationOptions;
        }
        this.progressService.withProgress(options, task, () => this.proxy.$acceptProgressCanceled(handle));
    }
    $progressReport(handle, message) {
        const entry = this.progress.get(handle);
        if (entry) {
            entry.progress.report(message);
        }
    }
    $progressEnd(handle) {
        const entry = this.progress.get(handle);
        if (entry) {
            entry.resolve();
            this.progress.delete(handle);
        }
    }
    dispose() {
        this.progress.forEach((handle) => handle.resolve());
        this.progress.clear();
    }
    createTask(handle) {
        return (progress) => new Promise((resolve) => {
            this.progress.set(handle, { resolve, progress });
        });
    }
};
exports.MainThreadProgress = MainThreadProgress;
tslib_1.__decorate([
    (0, di_1.Autowired)(progress_1.IProgressService),
    tslib_1.__metadata("design:type", Object)
], MainThreadProgress.prototype, "progressService", void 0);
exports.MainThreadProgress = MainThreadProgress = tslib_1.__decorate([
    (0, di_1.Injectable)({ multiple: true }),
    tslib_1.__param(0, (0, di_1.Optional)(ide_connection_1.IRPCProtocol)),
    tslib_1.__metadata("design:paramtypes", [Object])
], MainThreadProgress);
//# sourceMappingURL=main.thread.api.progress.js.map