"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.DefaultEditBuilder = exports.DefaultChangeFamily = void 0;
const util_1 = require("../../util");
const modular_schema_1 = require("../modular-schema");
const defaultFieldKinds_1 = require("./defaultFieldKinds");
/**
 * Implementation of {@link ChangeFamily} based on the default set of supported field kinds.
 *
 * @sealed
 */
class DefaultChangeFamily {
    constructor(codecOptions) {
        this.modularFamily = new modular_schema_1.ModularChangeFamily(defaultFieldKinds_1.fieldKinds, codecOptions);
    }
    get rebaser() {
        return this.modularFamily.rebaser;
    }
    get codecs() {
        return this.modularFamily.codecs;
    }
    intoDelta(change) {
        return this.modularFamily.intoDelta(change);
    }
    buildEditor(changeReceiver, anchorSet) {
        return new DefaultEditBuilder(this, changeReceiver, anchorSet);
    }
}
exports.DefaultChangeFamily = DefaultChangeFamily;
/**
 * Implementation of {@link IDefaultEditBuilder} based on the default set of supported field kinds.
 * @sealed
 */
class DefaultEditBuilder {
    constructor(family, changeReceiver, anchors) {
        this.modularBuilder = new modular_schema_1.ModularEditBuilder(family, changeReceiver, anchors);
    }
    enterTransaction() {
        this.modularBuilder.enterTransaction();
    }
    exitTransaction() {
        this.modularBuilder.exitTransaction();
    }
    apply(change) {
        this.modularBuilder.apply(change);
    }
    addNodeExistsConstraint(path) {
        this.modularBuilder.addNodeExistsConstraint(path);
    }
    valueField(field) {
        return {
            set: (newContent) => {
                const id = this.modularBuilder.generateId();
                const change = (0, util_1.brand)(defaultFieldKinds_1.value.changeHandler.editor.set(newContent, id));
                this.modularBuilder.submitChange(field, defaultFieldKinds_1.value.identifier, change);
            },
        };
    }
    optionalField(field) {
        return {
            set: (newContent, wasEmpty) => {
                const id = this.modularBuilder.generateId();
                const change = (0, util_1.brand)(defaultFieldKinds_1.optional.changeHandler.editor.set(newContent, wasEmpty, id));
                this.modularBuilder.submitChange(field, defaultFieldKinds_1.optional.identifier, change);
            },
        };
    }
    move(sourceField, sourceIndex, count, destinationField, destIndex) {
        const moveId = this.modularBuilder.generateId(count);
        const changes = defaultFieldKinds_1.sequence.changeHandler.editor.move(sourceIndex, count, destIndex, moveId);
        this.modularBuilder.submitChanges([
            {
                field: sourceField,
                fieldKind: defaultFieldKinds_1.sequence.identifier,
                change: (0, util_1.brand)(changes[0]),
            },
            {
                field: destinationField,
                fieldKind: defaultFieldKinds_1.sequence.identifier,
                change: (0, util_1.brand)(changes[1]),
            },
        ], moveId);
    }
    sequenceField(field) {
        return {
            insert: (index, newContent) => {
                const content = (0, util_1.isReadonlyArray)(newContent) ? newContent : [newContent];
                if (content.length === 0) {
                    return;
                }
                const length = Array.isArray(newContent) ? newContent.length : 1;
                const firstId = this.modularBuilder.generateId(length);
                const change = (0, util_1.brand)(defaultFieldKinds_1.sequence.changeHandler.editor.insert(index, content, firstId));
                this.modularBuilder.submitChange(field, defaultFieldKinds_1.sequence.identifier, change, (0, util_1.brand)(firstId + length - 1));
            },
            delete: (index, count) => {
                if (count === 0) {
                    return;
                }
                const id = this.modularBuilder.generateId(count);
                const change = (0, util_1.brand)(defaultFieldKinds_1.sequence.changeHandler.editor.delete(index, count, id));
                this.modularBuilder.submitChange(field, defaultFieldKinds_1.sequence.identifier, change);
            },
            move: (sourceIndex, count, destIndex) => {
                const moveId = this.modularBuilder.generateId(count);
                const moves = defaultFieldKinds_1.sequence.changeHandler.editor.move(sourceIndex, count, destIndex, moveId);
                this.modularBuilder.submitChanges([
                    {
                        field,
                        fieldKind: defaultFieldKinds_1.sequence.identifier,
                        change: (0, util_1.brand)(moves[0]),
                    },
                    {
                        field,
                        fieldKind: defaultFieldKinds_1.sequence.identifier,
                        change: (0, util_1.brand)(moves[1]),
                    },
                ], moveId);
            },
        };
    }
}
exports.DefaultEditBuilder = DefaultEditBuilder;
//# sourceMappingURL=defaultChangeFamily.js.map