"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MainThreadTestsImpl = void 0;
const tslib_1 = require("tslib");
// eslint-disable no-console
const di_1 = require("@opensumi/di");
const ide_connection_1 = require("@opensumi/ide-connection");
const ide_core_browser_1 = require("@opensumi/ide-core-browser");
const ide_core_common_1 = require("@opensumi/ide-core-common");
const ide_testing_1 = require("@opensumi/ide-testing");
const observableValue_1 = require("@opensumi/ide-testing/lib/common/observableValue");
const test_profile_1 = require("@opensumi/ide-testing/lib/common/test-profile");
const test_result_1 = require("@opensumi/ide-testing/lib/common/test-result");
const range_1 = require("@opensumi/monaco-editor-core/esm/vs/editor/common/core/range");
const vscode_1 = require("../../../common/vscode");
const reviveDiff = (diff) => {
    var _a, _b;
    for (const entry of diff) {
        if (entry[0] === 0 /* TestDiffOpType.Add */ || entry[0] === 1 /* TestDiffOpType.Update */) {
            const item = entry[1];
            if ((_a = item.item) === null || _a === void 0 ? void 0 : _a.uri) {
                item.item.uri = ide_core_common_1.URI.revive(item.item.uri);
            }
            if ((_b = item.item) === null || _b === void 0 ? void 0 : _b.range) {
                item.item.range = range_1.Range.lift(item.item.range);
            }
        }
    }
};
let MainThreadTestsImpl = class MainThreadTestsImpl extends ide_core_common_1.Disposable {
    constructor(rpcProtocol) {
        super();
        this.testProviderRegistrations = new Map();
        this.proxy = rpcProtocol.getProxy(vscode_1.ExtHostAPIIdentifier.ExtHostTests);
    }
    $registerTestController(controllerId, label, canRefreshValue) {
        const disposable = new ide_core_common_1.DisposableStore();
        const canRefresh = disposable.add(new observableValue_1.ObservableValue(canRefreshValue));
        const controller = {
            id: controllerId,
            label,
            canRefresh,
            configureRunProfile: (id) => this.proxy.$configureRunProfile(controllerId, id),
            refreshTests: (token) => this.proxy.$refreshTests(controllerId, token),
            runTests: (req, token) => this.proxy.$runControllerTests(req, token),
            expandTest: (testId, levels) => this.proxy.$expandTest(testId, isFinite(levels) ? levels : -1),
        };
        disposable.add(ide_core_common_1.Disposable.create(() => this.testProfiles.removeProfile(controllerId)));
        disposable.add(this.testService.registerTestController(controllerId, controller));
        this.testProviderRegistrations.set(controllerId, {
            instance: controller,
            label,
            canRefresh,
            disposable,
        });
    }
    $updateController(controllerId, patch) {
        const controller = this.testProviderRegistrations.get(controllerId);
        if (!controller) {
            return;
        }
        if (patch.label !== undefined) {
            controller.label = patch.label;
        }
        if (patch.canRefresh !== undefined) {
            controller.canRefresh.value = patch.canRefresh;
        }
    }
    $unregisterTestController(controllerId) {
        this.logger.warn('test: unregisterTestController>>', controllerId);
    }
    $subscribeToDiffs() {
        this.logger.warn('test: subscribeToDiffs>>');
    }
    $unsubscribeFromDiffs() {
        this.logger.warn('test: unsubscribeFromDiffs>>');
    }
    $publishDiff(controllerId, diff) {
        reviveDiff(diff);
        this.testService.publishDiff(controllerId, diff);
    }
    $publishTestRunProfile(config) {
        const controller = this.testProviderRegistrations.get(config.controllerId);
        if (controller) {
            this.testProfiles.addProfile(controller.instance, config);
        }
    }
    $updateTestRunConfig(controllerId, configId, update) {
        this.logger.warn('Method not implemented.');
    }
    $removeTestProfile(controllerId, configId) {
        this.testProfiles.removeProfile(controllerId, configId);
    }
    async $runTests(req, token) {
        const result = await this.testService.runResolvedTests(req, token);
        return result.id;
    }
    $addTestsToRun(controllerId, runId, tests) {
        for (const test of tests) {
            test.uri = ide_core_common_1.URI.revive(test.uri);
            if (test.range) {
                test.range = range_1.Range.lift(test.range);
            }
        }
        this.withTestResult(runId, (r) => r.addTestChainToRun(controllerId, tests));
    }
    $updateTestStateInRun(runId, taskId, testId, state, duration) {
        this.withTestResult(runId, (r) => r.updateState(testId, taskId, state, duration));
    }
    $appendTestMessagesInRun(runId, taskId, testId, messages) {
        const r = this.resultService.getResult(runId);
        if (r && r instanceof test_result_1.TestResultImpl) {
            for (const message of messages) {
                if (message.location) {
                    message.location.uri = ide_core_common_1.URI.revive(message.location.uri);
                    message.location.range = range_1.Range.lift(message.location.range);
                }
                r.appendMessage(testId, taskId, message);
            }
        }
    }
    $appendOutputToRun(runId, taskId, output, locationDto, testId) {
        const location = locationDto && {
            uri: ide_core_common_1.URI.revive(locationDto.uri),
            range: range_1.Range.lift(locationDto.range),
        };
        this.withTestResult(runId, (r) => r.appendOutput(output, taskId, location, testId));
    }
    $startedTestRunTask(runId, task) {
        this.withTestResult(runId, (r) => r.addTask(task));
    }
    $finishedTestRunTask(runId, taskId) {
        this.withTestResult(runId, (r) => r.markTaskComplete(taskId));
    }
    $startedExtensionTestRun(req) {
        this.resultService.createTestResult(req);
    }
    $finishedExtensionTestRun(runId) {
        this.withTestResult(runId, (r) => r.markComplete());
    }
    $markTestRetired(testIds) {
        this.logger.warn('Method not implemented.');
    }
    withTestResult(runId, fn) {
        const r = this.resultService.getResult(runId);
        return r && r instanceof test_result_1.TestResultImpl ? fn(r) : undefined;
    }
};
exports.MainThreadTestsImpl = MainThreadTestsImpl;
tslib_1.__decorate([
    (0, di_1.Autowired)(),
    tslib_1.__metadata("design:type", ide_core_browser_1.Logger)
], MainThreadTestsImpl.prototype, "logger", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_testing_1.TestServiceToken),
    tslib_1.__metadata("design:type", Object)
], MainThreadTestsImpl.prototype, "testService", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(test_profile_1.TestProfileServiceToken),
    tslib_1.__metadata("design:type", Object)
], MainThreadTestsImpl.prototype, "testProfiles", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(test_result_1.TestResultServiceToken),
    tslib_1.__metadata("design:type", Object)
], MainThreadTestsImpl.prototype, "resultService", void 0);
exports.MainThreadTestsImpl = MainThreadTestsImpl = tslib_1.__decorate([
    (0, di_1.Injectable)({ multiple: true }),
    tslib_1.__param(0, (0, di_1.Optional)(Symbol())),
    tslib_1.__metadata("design:paramtypes", [Object])
], MainThreadTestsImpl);
//# sourceMappingURL=main.thread.tests.js.map