import { __decorate, __param } from '../../../../../../../external/tslib/tslib.es6.js';
import { Disposable } from '../../../../base/common/lifecycle.js';
import '../../../../base/common/arrays.js';
import '../../../../base/common/observableInternal/derived.js';
import '../../../../base/common/observableInternal/autorun.js';
import { observableFromEvent, ValueWithChangeEventFromObservable } from '../../../../base/common/observableInternal/utils.js';
import { waitForState } from '../../../../base/common/observableInternal/promise.js';
import { URI } from '../../../../base/common/uri.js';
import { localize2WithPath } from '../../../../nls.js';
import { Action2 } from '../../../../platform/actions/common/actions.js';
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
import { MultiDiffEditorItem } from './multiDiffSourceResolverService.js';
import { IMultiDiffSourceResolverService } from './multiDiffSourceResolverService.service.js';
import { ISCMService } from '../../scm/common/scm.service.js';
import { IEditorService } from '../../../services/editor/common/editorService.service.js';
var ScmMultiDiffSourceResolver_1;
const _moduleId = "vs/workbench/contrib/multiDiffEditor/browser/scmMultiDiffSourceResolver";
let ScmMultiDiffSourceResolver = class ScmMultiDiffSourceResolver {
    static { ScmMultiDiffSourceResolver_1 = this; }
    static { this._scheme = 'scm-multi-diff-source'; }
    static getMultiDiffSourceUri(repositoryUri, groupId) {
        return (
             (URI.from({
                scheme: ScmMultiDiffSourceResolver_1._scheme,
                query: JSON.stringify({ repositoryUri, groupId }),
            }))
        );
    }
    static parseUri(uri) {
        if (uri.scheme !== ScmMultiDiffSourceResolver_1._scheme) {
            return undefined;
        }
        let query;
        try {
            query = JSON.parse(uri.query);
        }
        catch (e) {
            return undefined;
        }
        if (typeof query !== 'object' || query === null) {
            return undefined;
        }
        const { repositoryUri, groupId } = query;
        if (typeof repositoryUri !== 'string' || typeof groupId !== 'string') {
            return undefined;
        }
        return { repositoryUri: ( (URI.parse(repositoryUri))), groupId };
    }
    constructor(_scmService) {
        this._scmService = _scmService;
    }
    canHandleUri(uri) {
        return ScmMultiDiffSourceResolver_1.parseUri(uri) !== undefined;
    }
    async resolveDiffSource(uri) {
        const { repositoryUri, groupId } = ScmMultiDiffSourceResolver_1.parseUri(uri);
        const repository = await waitForState(observableFromEvent(this, this._scmService.onDidAddRepository, () => [...this._scmService.repositories].find(r => r.provider.rootUri?.toString() === ( (repositoryUri.toString())))));
        const group = await waitForState(observableFromEvent(this, repository.provider.onDidChangeResourceGroups, () => repository.provider.groups.find(g => g.id === groupId)));
        return (
             (new ScmResolvedMultiDiffSource(group, repository))
        );
    }
};
ScmMultiDiffSourceResolver = ScmMultiDiffSourceResolver_1 = ( (__decorate([
    ( (__param(0, ISCMService)))
], ScmMultiDiffSourceResolver)));
class ScmResolvedMultiDiffSource {
    constructor(_group, _repository) {
        this._group = _group;
        this._repository = _repository;
        this._resources = observableFromEvent(this._group.onDidChangeResources, () => (  (this._group.resources.map(
            e => ( (new MultiDiffEditorItem(e.multiDiffEditorOriginalUri, e.multiDiffEditorModifiedUri)))
        ))));
        this.resources = ( (new ValueWithChangeEventFromObservable(this._resources)));
        this.contextKeys = {
            scmResourceGroup: this._group.id,
            scmProvider: this._repository.provider.contextValue,
        };
    }
}
let ScmMultiDiffSourceResolverContribution = class ScmMultiDiffSourceResolverContribution extends Disposable {
    static { this.ID = 'workbench.contrib.scmMultiDiffSourceResolver'; }
    constructor(instantiationService, multiDiffSourceResolverService) {
        super();
        this._register(multiDiffSourceResolverService.registerResolver(instantiationService.createInstance(ScmMultiDiffSourceResolver)));
    }
};
ScmMultiDiffSourceResolverContribution = ( (__decorate([
    ( (__param(0, IInstantiationService))),
    ( (__param(1, IMultiDiffSourceResolverService)))
], ScmMultiDiffSourceResolverContribution)));
class OpenScmGroupAction extends Action2 {
    static async openMultiFileDiffEditor(editorService, label, repositoryRootUri, resourceGroupId, options) {
        if (!repositoryRootUri) {
            return;
        }
        const multiDiffSource = ScmMultiDiffSourceResolver.getMultiDiffSourceUri(( (repositoryRootUri.toString())), resourceGroupId);
        return await editorService.openEditor({ label, multiDiffSource, options });
    }
    constructor() {
        super({
            id: '_workbench.openScmMultiDiffEditor',
            title: ( localize2WithPath(_moduleId, 0, 'View Changes')),
            f1: false
        });
    }
    async run(accessor, options) {
        const editorService = accessor.get(IEditorService);
        await OpenScmGroupAction.openMultiFileDiffEditor(editorService, options.title, URI.revive(options.repositoryUri), options.resourceGroupId);
    }
}
export { OpenScmGroupAction, ScmMultiDiffSourceResolver, ScmMultiDiffSourceResolverContribution };
