
import { __decorate, __param } from '../../../../../../external/tslib/tslib.es6.js';
import { localize } from '../../../nls.js';
import { SideBySideEditorInput, AbstractSideBySideEditorInputSerializer } from './sideBySideEditorInput.js';
import { EditorInputCapabilities, Verbosity, BINARY_DIFF_EDITOR_ID, TEXT_DIFF_EDITOR_ID, isResourceDiffEditorInput } from '../editor.js';
import { BaseTextEditorModel } from './textEditorModel.js';
import { DiffEditorModel } from './diffEditorModel.js';
import { TextDiffEditorModel } from './textDiffEditorModel.js';
import { IEditorService } from '../../services/editor/common/editorService.service.js';
import { shorten } from '../../../base/common/labels.js';
import { isResolvedEditorModel } from '../../../platform/editor/common/editor.js';

var DiffEditorInput_1;
let DiffEditorInput = class DiffEditorInput extends SideBySideEditorInput {
    static {
        DiffEditorInput_1 = this;
    }
    static {
        this.ID = "workbench.editors.diffEditorInput";
    }
    get typeId() {
        return DiffEditorInput_1.ID;
    }
    get editorId() {
        return this.modified.editorId === this.original.editorId ? this.modified.editorId : undefined;
    }
    get capabilities() {
        let capabilities = super.capabilities;
        if (this.labels.forceDescription) {
            capabilities |= EditorInputCapabilities.ForceDescription;
        }
        return capabilities;
    }
    constructor(
        preferredName,
        preferredDescription,
        original,
        modified,
        forceOpenAsBinary,
        editorService
    ) {
        super(preferredName, preferredDescription, original, modified, editorService);
        this.original = original;
        this.modified = modified;
        this.forceOpenAsBinary = forceOpenAsBinary;
        this.cachedModel = undefined;
        this.labels = this.computeLabels();
    }
    computeLabels() {
        let name;
        let forceDescription = false;
        if (this.preferredName) {
            name = this.preferredName;
        } else {
            const originalName = this.original.getName();
            const modifiedName = this.modified.getName();
            name = ( localize(4161, "{0} ↔ {1}", originalName, modifiedName));
            forceDescription = originalName === modifiedName;
        }
        let shortDescription;
        let mediumDescription;
        let longDescription;
        if (this.preferredDescription) {
            shortDescription = this.preferredDescription;
            mediumDescription = this.preferredDescription;
            longDescription = this.preferredDescription;
        } else {
            shortDescription = this.computeLabel(
                this.original.getDescription(Verbosity.SHORT),
                this.modified.getDescription(Verbosity.SHORT)
            );
            longDescription = this.computeLabel(
                this.original.getDescription(Verbosity.LONG),
                this.modified.getDescription(Verbosity.LONG)
            );
            const originalMediumDescription = this.original.getDescription(Verbosity.MEDIUM);
            const modifiedMediumDescription = this.modified.getDescription(Verbosity.MEDIUM);
            if (
            (typeof originalMediumDescription === "string" && typeof modifiedMediumDescription === "string") && (originalMediumDescription || modifiedMediumDescription)) {
                const [shortenedOriginalMediumDescription, shortenedModifiedMediumDescription] = shorten([originalMediumDescription, modifiedMediumDescription]);
                mediumDescription = this.computeLabel(shortenedOriginalMediumDescription, shortenedModifiedMediumDescription);
            }
        }
        let shortTitle = this.computeLabel(
            this.original.getTitle(Verbosity.SHORT) ?? this.original.getName(),
            this.modified.getTitle(Verbosity.SHORT) ?? this.modified.getName(),
            " ↔ "
        );
        let mediumTitle = this.computeLabel(
            this.original.getTitle(Verbosity.MEDIUM) ?? this.original.getName(),
            this.modified.getTitle(Verbosity.MEDIUM) ?? this.modified.getName(),
            " ↔ "
        );
        let longTitle = this.computeLabel(
            this.original.getTitle(Verbosity.LONG) ?? this.original.getName(),
            this.modified.getTitle(Verbosity.LONG) ?? this.modified.getName(),
            " ↔ "
        );
        const preferredTitle = this.getPreferredTitle();
        if (preferredTitle) {
            shortTitle = `${preferredTitle} (${shortTitle})`;
            mediumTitle = `${preferredTitle} (${mediumTitle})`;
            longTitle = `${preferredTitle} (${longTitle})`;
        }
        return {
            name,
            shortDescription,
            mediumDescription,
            longDescription,
            forceDescription,
            shortTitle,
            mediumTitle,
            longTitle
        };
    }
    computeLabel(originalLabel, modifiedLabel, separator = " - ") {
        if (!originalLabel || !modifiedLabel) {
            return undefined;
        }
        if (originalLabel === modifiedLabel) {
            return modifiedLabel;
        }
        return `${originalLabel}${separator}${modifiedLabel}`;
    }
    getName() {
        return this.labels.name;
    }
    getDescription(verbosity = Verbosity.MEDIUM) {
        switch (verbosity) {
        case Verbosity.SHORT:
            return this.labels.shortDescription;
        case Verbosity.LONG:
            return this.labels.longDescription;
        case Verbosity.MEDIUM:
        default:
            return this.labels.mediumDescription;
        }
    }
    getTitle(verbosity) {
        switch (verbosity) {
        case Verbosity.SHORT:
            return this.labels.shortTitle;
        case Verbosity.LONG:
            return this.labels.longTitle;
        default:
        case Verbosity.MEDIUM:
            return this.labels.mediumTitle;
        }
    }
    async resolve() {
        const resolvedModel = await this.createModel();
        this.cachedModel?.dispose();
        this.cachedModel = resolvedModel;
        return this.cachedModel;
    }
    prefersEditorPane(editorPanes) {
        if (this.forceOpenAsBinary) {
            return editorPanes.find(editorPane => editorPane.typeId === BINARY_DIFF_EDITOR_ID);
        }
        return editorPanes.find(editorPane => editorPane.typeId === TEXT_DIFF_EDITOR_ID);
    }
    async createModel() {
        const [originalEditorModel, modifiedEditorModel] = await Promise.all([this.original.resolve(), this.modified.resolve()]);
        if (modifiedEditorModel instanceof BaseTextEditorModel && originalEditorModel instanceof BaseTextEditorModel) {
            return ( new TextDiffEditorModel(originalEditorModel, modifiedEditorModel));
        }
        return ( new DiffEditorModel(
            isResolvedEditorModel(originalEditorModel) ? originalEditorModel : undefined,
            isResolvedEditorModel(modifiedEditorModel) ? modifiedEditorModel : undefined
        ));
    }
    toUntyped(options) {
        const untyped = super.toUntyped(options);
        if (untyped) {
            return {
                ...untyped,
                modified: untyped.primary,
                original: untyped.secondary
            };
        }
        return undefined;
    }
    matches(otherInput) {
        if (this === otherInput) {
            return true;
        }
        if (otherInput instanceof DiffEditorInput_1) {
            return this.modified.matches(otherInput.modified) && this.original.matches(otherInput.original) && otherInput.forceOpenAsBinary === this.forceOpenAsBinary;
        }
        if (isResourceDiffEditorInput(otherInput)) {
            return this.modified.matches(otherInput.modified) && this.original.matches(otherInput.original);
        }
        return false;
    }
    dispose() {
        if (this.cachedModel) {
            this.cachedModel.dispose();
            this.cachedModel = undefined;
        }
        super.dispose();
    }
};
DiffEditorInput = DiffEditorInput_1 = ( __decorate([( __param(5, IEditorService))], DiffEditorInput));
class DiffEditorInputSerializer extends AbstractSideBySideEditorInputSerializer {
    createEditorInput(instantiationService, name, description, secondaryInput, primaryInput) {
        return instantiationService.createInstance(
            DiffEditorInput,
            name,
            description,
            secondaryInput,
            primaryInput,
            undefined
        );
    }
}

export { DiffEditorInput, DiffEditorInputSerializer };
