"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TextEditorEdit = void 0;
const ext_types_1 = require("../../../../common/vscode/ext-types");
class TextEditorEdit {
    constructor(document, options) {
        this._document = document;
        this._documentVersionId = document.version;
        this._collectedEdits = [];
        this._setEndOfLine = undefined;
        this._undoStopBefore = options.undoStopBefore;
        this._undoStopAfter = options.undoStopAfter;
    }
    finalize() {
        return {
            documentVersionId: this._documentVersionId,
            edits: this._collectedEdits,
            setEndOfLine: this._setEndOfLine,
            undoStopBefore: this._undoStopBefore,
            undoStopAfter: this._undoStopAfter,
        };
    }
    replace(location, value) {
        let range = null;
        if (location instanceof ext_types_1.Position) {
            range = new ext_types_1.Range(location, location);
        }
        else if (location instanceof ext_types_1.Range) {
            range = location;
        }
        else {
            throw new Error('Unrecognized location');
        }
        this._pushEdit(range, value, false);
    }
    insert(location, value) {
        this._pushEdit(new ext_types_1.Range(location, location), value, true);
    }
    delete(location) {
        let range = null;
        if (location instanceof ext_types_1.Range) {
            range = location;
        }
        else {
            throw new Error('Unrecognized location');
        }
        this._pushEdit(range, null, true);
    }
    _pushEdit(range, text, forceMoveMarkers) {
        const validRange = this._document.validateRange(range);
        this._collectedEdits.push({
            range: validRange,
            text,
            forceMoveMarkers,
        });
    }
    setEndOfLine(endOfLine) {
        if (endOfLine !== ext_types_1.EndOfLine.LF && endOfLine !== ext_types_1.EndOfLine.CRLF) {
            throw new Error('illegalArgument endOfLine');
        }
        this._setEndOfLine = endOfLine;
    }
}
exports.TextEditorEdit = TextEditorEdit;
//# sourceMappingURL=edit.builder.js.map