import { PersistedSlice } from '../slice/PersistedSlice';
const forEachRange = (selection, callback) => {
    const slices = [];
    for (const cursor of selection) {
        const slice = callback(cursor);
        slices.push(slice);
    }
    return slices;
};
export class EditorSlices {
    txt;
    slices;
    constructor(txt, slices) {
        this.txt = txt;
        this.slices = slices;
    }
    /**
     * @todo Rename to `insMany`.
     */
    insStack(type, data, selection) {
        const { slices, txt } = this;
        selection ||= txt.editor.cursors();
        return forEachRange(selection, (range) => slices.insStack(range.range(), type, data));
    }
    insOne(type, data, selection) {
        const { slices, txt } = this;
        selection ||= txt.editor.cursors();
        return forEachRange(selection, (range) => slices.insOne(range.range(), type, data));
    }
    insErase(type, data, selection) {
        const { slices, txt } = this;
        selection ||= txt.editor.cursors();
        return forEachRange(selection, (range) => slices.insErase(range.range(), type, data));
    }
    insMarker(type, data, separator, selection) {
        const { slices, txt } = this;
        const editor = txt.editor;
        selection ||= txt.editor.cursors();
        return forEachRange(selection, (range) => {
            editor.collapseCursor(range);
            const after = range.start.clone();
            after.refAfter();
            const marker = slices.insMarkerAfter(after.id, type, data, separator);
            return marker;
        });
    }
    del(sliceOrId) {
        this.slices.del(sliceOrId instanceof PersistedSlice ? sliceOrId.id : sliceOrId);
    }
}
//# sourceMappingURL=EditorSlices.js.map