/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
import { Disposable } from '../../../../base/common/lifecycle.js';
import { observableFromEvent, observableValue } from '../../../../base/common/observable.js';
import { mapObservableArrayCached } from '../../../../base/common/observableInternal/utils.js';
import { DiffEditorOptions } from '../diffEditor/diffEditorOptions.js';
import { DiffEditorViewModel } from '../diffEditor/diffEditorViewModel.js';
export class MultiDiffEditorViewModel extends Disposable {
    get contextKeys() {
        return this.model.contextKeys;
    }
    constructor(model, _instantiationService) {
        super();
        this.model = model;
        this._instantiationService = _instantiationService;
        this._documents = observableFromEvent(this.model.onDidChange, /** @description MultiDiffEditorViewModel.documents */ () => this.model.documents);
        this.items = mapObservableArrayCached(this, this._documents, (d, store) => store.add(new DocumentDiffItemViewModel(d, this._instantiationService)))
            .recomputeInitiallyAndOnChange(this._store);
        this.activeDiffItem = observableValue(this, undefined);
    }
}
export class DocumentDiffItemViewModel extends Disposable {
    constructor(entry, _instantiationService) {
        super();
        this.entry = entry;
        this._instantiationService = _instantiationService;
        this.collapsed = observableValue(this, false);
        this.lastTemplateData = observableValue(this, { contentHeight: 500, selections: undefined, });
        function updateOptions(options) {
            return {
                ...options,
                hideUnchangedRegions: {
                    enabled: true,
                },
            };
        }
        const options = new DiffEditorOptions(updateOptions(this.entry.value.options || {}));
        if (this.entry.value.onOptionsDidChange) {
            this._register(this.entry.value.onOptionsDidChange(() => {
                options.updateOptions(updateOptions(this.entry.value.options || {}));
            }));
        }
        this.diffEditorViewModel = this._register(this._instantiationService.createInstance(DiffEditorViewModel, {
            original: entry.value.original,
            modified: entry.value.modified,
        }, options));
    }
    getKey() {
        return JSON.stringify([
            this.diffEditorViewModel.model.original.uri.toString(),
            this.diffEditorViewModel.model.modified.uri.toString()
        ]);
    }
}
