"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.MarkListFactory = void 0;
const utils_1 = require("./utils");
/**
 * Helper class for constructing an offset list of marks that...
 * - Does not insert offsets if there is no content after them
 * - Does not insert 0-sized offsets
 * - Merges runs of offsets together
 * - Merges marks together
 */
class MarkListFactory {
    constructor() {
        this.offset = 0;
        this.list = [];
    }
    push(...marks) {
        for (const item of marks) {
            this.pushContent(item);
        }
    }
    pushOffset(offset) {
        this.offset += offset;
    }
    pushContent(mark) {
        if ((0, utils_1.isNoopMark)(mark) && mark.changes === undefined) {
            // A noop targeting an empty cell can be omitted from the final mark list
            if (mark.cellId === undefined) {
                this.pushOffset(mark.count);
            }
        }
        else {
            if (this.offset > 0) {
                this.list.push({ count: this.offset });
                this.offset = 0;
            }
            const prev = this.list[this.list.length - 1];
            if (prev !== undefined && prev.type === mark.type) {
                const merged = (0, utils_1.tryMergeMarks)(prev, mark);
                if (merged !== undefined) {
                    this.list.splice(this.list.length - 1, 1, merged);
                    return;
                }
            }
            this.list.push(mark);
        }
    }
}
exports.MarkListFactory = MarkListFactory;
//# sourceMappingURL=markListFactory.js.map