"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.FieldKinds = exports.fieldKinds = exports.fieldKindConfigurations = exports.forbidden = exports.identifier = exports.nodeKey = exports.sequence = exports.required = exports.valueChangeHandler = exports.valueFieldEditor = exports.optional = exports.noChangeHandler = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
const index_js_1 = require("../../core/index.js");
const index_js_2 = require("../modular-schema/index.js");
const index_js_3 = require("../optional-field/index.js");
const index_js_4 = require("../sequence-field/index.js");
const noChangeCodecs_js_1 = require("./noChangeCodecs.js");
/**
 * ChangeHandler that only handles no-op / identity changes.
 */
exports.noChangeHandler = {
    rebaser: (0, index_js_2.referenceFreeFieldChangeRebaser)({
        compose: (change1, change2) => 0,
        invert: (changes) => 0,
        rebase: (change, over) => 0,
    }),
    codecsFactory: () => noChangeCodecs_js_1.noChangeCodecFamily,
    editor: { buildChildChanges: () => (0, internal_1.fail)(0xb0d /* Child changes not supported */) },
    intoDelta: (change, deltaFromChild) => ({}),
    relevantRemovedRoots: (change) => [],
    isEmpty: (change) => true,
    getNestedChanges: (change) => [],
    createEmpty: () => 0,
    getCrossFieldKeys: () => [],
};
const optionalIdentifier = "Optional";
/**
 * 0 or 1 items.
 */
exports.optional = new index_js_2.FieldKindWithEditor(optionalIdentifier, index_js_1.Multiplicity.Optional, index_js_3.optionalChangeHandler, (types, other) => (other.kind === exports.sequence.identifier || other.kind === optionalIdentifier) &&
    (0, index_js_2.allowsTreeSchemaIdentifierSuperset)(types, other.types), new Set([]));
exports.valueFieldEditor = {
    ...index_js_3.optionalFieldEditor,
    set: (ids) => index_js_3.optionalFieldEditor.set(false, ids),
};
exports.valueChangeHandler = {
    ...exports.optional.changeHandler,
    editor: exports.valueFieldEditor,
};
const requiredIdentifier = "Value";
/**
 * Exactly one item.
 */
exports.required = new index_js_2.FieldKindWithEditor(requiredIdentifier, index_js_1.Multiplicity.Single, exports.valueChangeHandler, (types, other) => (other.kind === exports.sequence.identifier ||
    other.kind === requiredIdentifier ||
    other.kind === exports.optional.identifier ||
    other.kind === exports.nodeKey.identifier) &&
    (0, index_js_2.allowsTreeSchemaIdentifierSuperset)(types, other.types), new Set());
const sequenceIdentifier = "Sequence";
/**
 * 0 or more items.
 */
exports.sequence = new index_js_2.FieldKindWithEditor(sequenceIdentifier, index_js_1.Multiplicity.Sequence, index_js_4.sequenceFieldChangeHandler, (types, other) => other.kind === sequenceIdentifier &&
    (0, index_js_2.allowsTreeSchemaIdentifierSuperset)(types, other.types), 
// TODO: add normalizer/importers for handling ops from other kinds.
new Set([]));
const nodeKeyIdentifier = "NodeKey";
/**
 * Exactly one identifier.
 */
exports.nodeKey = new index_js_2.FieldKindWithEditor(nodeKeyIdentifier, index_js_1.Multiplicity.Single, exports.noChangeHandler, (types, other) => (other.kind === exports.sequence.identifier ||
    other.kind === requiredIdentifier ||
    other.kind === exports.optional.identifier ||
    other.kind === nodeKeyIdentifier) &&
    (0, index_js_2.allowsTreeSchemaIdentifierSuperset)(types, other.types), new Set());
const identifierFieldIdentifier = "Identifier";
/**
 * Exactly one identifier.
 */
exports.identifier = new index_js_2.FieldKindWithEditor(identifierFieldIdentifier, index_js_1.Multiplicity.Single, exports.noChangeHandler, (types, other) => (other.kind === exports.sequence.identifier ||
    other.kind === requiredIdentifier ||
    other.kind === exports.optional.identifier ||
    other.kind === identifierFieldIdentifier) &&
    (0, index_js_2.allowsTreeSchemaIdentifierSuperset)(types, other.types), new Set());
/**
 * Exactly 0 items.
 *
 * Using Forbidden makes what types are listed for allowed in a field irrelevant
 * since the field will never have values in it.
 *
 * Using Forbidden is equivalent to picking a kind that permits empty (like sequence or optional)
 * and having no allowed types (or only never types).
 * Because of this, its possible to express everything constraint wise without Forbidden,
 * but using Forbidden can be more semantically clear than optional with no allowed types.
 *
 * For view schema, this can be useful if you need to:
 * - run a specific out of schema handler when a field is present,
 * but otherwise are ignoring or tolerating (ex: via extra fields) unmentioned fields.
 * - prevent a specific field from being used as an extra field
 * (perhaps for some past of future compatibility reason)
 * - keep a field in a schema for metadata purposes
 * (ex: for improved error messaging, error handling or documentation)
 * that is not used in this specific version of the schema (ex: to document what it was or will be used for).
 *
 * For stored schema, this can be useful if you need to:
 * - have a field which can have its schema updated to Optional or Sequence of any type.
 * - to exclude a field from extra fields
 * - for the schema system to use as a default for fields which aren't declared
 * (ex: when updating a field that did not exist into one that does)
 *
 * See {@link emptyField} for a constant, reusable field using Forbidden.
 */
exports.forbidden = new index_js_2.FieldKindWithEditor(index_js_1.forbiddenFieldKindIdentifier, index_js_1.Multiplicity.Forbidden, exports.noChangeHandler, 
// All multiplicities other than Value support empty.
(types, other) => exports.fieldKinds.get(other.kind)?.multiplicity !== index_js_1.Multiplicity.Single, new Set());
exports.fieldKindConfigurations = new Map([
    [
        1,
        new Map([
            [exports.nodeKey.identifier, { kind: exports.nodeKey, formatVersion: 1 }],
            [exports.required.identifier, { kind: exports.required, formatVersion: 1 }],
            [exports.optional.identifier, { kind: exports.optional, formatVersion: 1 }],
            [exports.sequence.identifier, { kind: exports.sequence, formatVersion: 1 }],
            [exports.forbidden.identifier, { kind: exports.forbidden, formatVersion: 1 }],
            [exports.identifier.identifier, { kind: exports.identifier, formatVersion: 1 }],
        ]),
    ],
    [
        2,
        new Map([
            [exports.nodeKey.identifier, { kind: exports.nodeKey, formatVersion: 1 }],
            [exports.required.identifier, { kind: exports.required, formatVersion: 2 }],
            [exports.optional.identifier, { kind: exports.optional, formatVersion: 2 }],
            [exports.sequence.identifier, { kind: exports.sequence, formatVersion: 1 }],
            [exports.forbidden.identifier, { kind: exports.forbidden, formatVersion: 1 }],
            [exports.identifier.identifier, { kind: exports.identifier, formatVersion: 1 }],
        ]),
    ],
    [
        3,
        new Map([
            [exports.nodeKey.identifier, { kind: exports.nodeKey, formatVersion: 1 }],
            [exports.required.identifier, { kind: exports.required, formatVersion: 2 }],
            [exports.optional.identifier, { kind: exports.optional, formatVersion: 2 }],
            [exports.sequence.identifier, { kind: exports.sequence, formatVersion: 2 }],
            [exports.forbidden.identifier, { kind: exports.forbidden, formatVersion: 1 }],
            [exports.identifier.identifier, { kind: exports.identifier, formatVersion: 1 }],
        ]),
    ],
    [
        4,
        new Map([
            [exports.nodeKey.identifier, { kind: exports.nodeKey, formatVersion: 1 }],
            [exports.required.identifier, { kind: exports.required, formatVersion: 2 }],
            [exports.optional.identifier, { kind: exports.optional, formatVersion: 2 }],
            [exports.sequence.identifier, { kind: exports.sequence, formatVersion: 3 }],
            [exports.forbidden.identifier, { kind: exports.forbidden, formatVersion: 1 }],
            [exports.identifier.identifier, { kind: exports.identifier, formatVersion: 1 }],
        ]),
    ],
]);
/**
 * All supported field kinds.
 *
 * @privateRemarks
 * Before making a SharedTree format change which impacts which set of field kinds are allowed,
 * code which uses this should be audited for compatibility considerations.
 */
exports.fieldKinds = new Map([exports.required, exports.optional, exports.sequence, exports.nodeKey, exports.identifier, exports.forbidden].map((s) => [s.identifier, s]));
/**
 * Default FieldKinds with their editor types erased.
 */
exports.FieldKinds = { required: exports.required, optional: exports.optional, sequence: exports.sequence, identifier: exports.identifier, forbidden: exports.forbidden };
//# sourceMappingURL=defaultFieldKinds.js.map