"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 = exports.DefaultChangeFamily = void 0;
const util_1 = require("../util");
const modular_schema_1 = require("./modular-schema");
const defaultFieldKinds_1 = require("./defaultFieldKinds");
const defaultFieldKinds = new Map([defaultFieldKinds_1.value, defaultFieldKinds_1.optional, defaultFieldKinds_1.sequence, defaultFieldKinds_1.forbidden].map((f) => [f.identifier, f]));
/**
 * Implementation of {@link ChangeFamily} based on the default set of supported field kinds.
 *
 * @sealed
 */
class DefaultChangeFamily {
    constructor() {
        this.modularFamily = new modular_schema_1.ModularChangeFamily(defaultFieldKinds);
    }
    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;
exports.defaultChangeFamily = new 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);
    }
    setValue(path, value) {
        this.modularBuilder.setValue(path, value);
    }
    addValueConstraint(path, value) {
        this.modularBuilder.addValueConstraint(path, value);
    }
    valueField(field) {
        return {
            set: (newContent) => {
                const change = (0, util_1.brand)(defaultFieldKinds_1.value.changeHandler.editor.set(newContent));
                this.modularBuilder.submitChange(field, defaultFieldKinds_1.value.identifier, change);
            },
        };
    }
    optionalField(field) {
        return {
            set: (newContent, wasEmpty) => {
                const change = (0, util_1.brand)(defaultFieldKinds_1.optional.changeHandler.editor.set(newContent, wasEmpty));
                this.modularBuilder.submitChange(field, defaultFieldKinds_1.optional.identifier, change);
            },
        };
    }
    move(sourceField, sourceIndex, count, destinationField, destIndex) {
        const changes = defaultFieldKinds_1.sequence.changeHandler.editor.move(sourceIndex, count, destIndex, this.modularBuilder.generateId());
        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]),
            },
        ], (0, util_1.brand)(0));
    }
    sequenceField(field) {
        return {
            insert: (index, newContent) => {
                const change = (0, util_1.brand)(defaultFieldKinds_1.sequence.changeHandler.editor.insert(index, newContent, this.modularBuilder.generateId(Array.isArray(newContent) ? newContent.length : 1)));
                this.modularBuilder.submitChange(field, defaultFieldKinds_1.sequence.identifier, change);
            },
            delete: (index, count) => {
                const change = (0, util_1.brand)(defaultFieldKinds_1.sequence.changeHandler.editor.delete(index, count));
                this.modularBuilder.submitChange(field, defaultFieldKinds_1.sequence.identifier, change);
            },
            move: (sourceIndex, count, destIndex) => {
                const moves = defaultFieldKinds_1.sequence.changeHandler.editor.move(sourceIndex, count, destIndex, this.modularBuilder.generateId());
                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]),
                    },
                ], (0, util_1.brand)(0));
            },
            revive: (index, count, detachedBy, reviver, detachIndex, isIntention) => {
                const change = (0, util_1.brand)(defaultFieldKinds_1.sequence.changeHandler.editor.revive(index, count, detachedBy, reviver, detachIndex, isIntention));
                this.modularBuilder.submitChange(field, defaultFieldKinds_1.sequence.identifier, change);
            },
        };
    }
}
exports.DefaultEditBuilder = DefaultEditBuilder;
//# sourceMappingURL=defaultChangeFamily.js.map