"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.normalizeField = exports.isNeverTreeRecursive = exports.isNeverTree = exports.isNeverFieldRecursive = exports.isNeverField = exports.allowsRepoSuperset = exports.allowsTreeSchemaIdentifierSuperset = exports.allowsFieldSuperset = exports.allowsValueSuperset = exports.allowsTreeSuperset = void 0;
const common_utils_1 = require("@fluidframework/common-utils");
const util_1 = require("../../util");
const core_1 = require("../../core");
const fieldKind_1 = require("./fieldKind");
/**
 * @returns true iff `superset` is a superset of `original`.
 *
 * This does not require a strict (aka proper) superset: equivalent schema will return true.
 *
 * `undefined` TreeStoredSchema means the schema is not present (and thus treated as a NeverTree).
 */
function allowsTreeSuperset(policy, originalData, original, superset) {
    if (isNeverTree(policy, originalData, original)) {
        return true;
    }
    if (isNeverTree(policy, originalData, superset)) {
        return false;
    }
    (0, common_utils_1.assert)(original !== undefined, 0x716 /* only never trees have undefined schema */);
    (0, common_utils_1.assert)(superset !== undefined, 0x717 /* only never trees have undefined schema */);
    if (!allowsValueSuperset(original.leafValue, superset.leafValue)) {
        return false;
    }
    if (!allowsFieldSuperset(policy, originalData, normalizeField(original.mapFields), normalizeField(superset.mapFields))) {
        return false;
    }
    if (!(0, util_1.compareSets)({
        a: original.structFields,
        b: superset.structFields,
        aExtra: (originalField) => allowsFieldSuperset(policy, originalData, original.structFields.get(originalField) ?? (0, util_1.fail)("missing expected field"), normalizeField(superset.mapFields)),
        bExtra: (supersetField) => allowsFieldSuperset(policy, originalData, normalizeField(original.mapFields), superset.structFields.get(supersetField) ?? (0, util_1.fail)("missing expected field")),
        same: (sameField) => allowsFieldSuperset(policy, originalData, original.structFields.get(sameField) ?? (0, util_1.fail)("missing expected field"), superset.structFields.get(sameField) ?? (0, util_1.fail)("missing expected field")),
    })) {
        return false;
    }
    return true;
}
exports.allowsTreeSuperset = allowsTreeSuperset;
/**
 * @returns true iff `superset` is a superset of `original`.
 *
 * This does not require a strict (aka proper) superset: equivalent schema will return true.
 */
function allowsValueSuperset(original, superset) {
    if (original === superset) {
        return true;
    }
    if (original === undefined) {
        return false;
    }
    return superset === core_1.ValueSchema.Serializable;
}
exports.allowsValueSuperset = allowsValueSuperset;
/**
 * @returns true iff `superset` is a superset of `original`.
 *
 * This does not require a strict (aka proper) superset: equivalent schema will return true.
 */
function allowsFieldSuperset(policy, originalData, original, superset) {
    return (policy.fieldKinds.get(original.kind.identifier) ?? (0, util_1.fail)("missing kind")).allowsFieldSuperset(policy, originalData, original.types, superset);
}
exports.allowsFieldSuperset = allowsFieldSuperset;
/**
 * @returns true iff `superset` is a superset of `original`.
 *
 * This does not require a strict (aka proper) superset: equivalent schema will return true.
 */
function allowsTreeSchemaIdentifierSuperset(original, superset) {
    if (superset === undefined) {
        return true;
    }
    if (original === undefined) {
        return false;
    }
    for (const originalType of original) {
        if (!superset.has(originalType)) {
            return false;
        }
    }
    return true;
}
exports.allowsTreeSchemaIdentifierSuperset = allowsTreeSchemaIdentifierSuperset;
/**
 * @returns true iff `superset` is a superset of `original`.
 *
 * This does not require a strict (aka proper) superset: equivalent schema will return true.
 *
 * A version of this that assumes a specific root field could be slightly more permissive in some simple cases,
 * however if any extra fields and fields with unconstrained types are reachable,
 * it would have to compare everything anyway.
 */
function allowsRepoSuperset(policy, original, superset) {
    {
        // TODO: I think its ok to use the field from superset here, but I should confirm it is, and document why.
        if (!allowsFieldSuperset(policy, original, original.rootFieldSchema, superset.rootFieldSchema)) {
            return false;
        }
    }
    for (const [key, schema] of original.treeSchema) {
        // TODO: I think its ok to use the tree from superset here, but I should confirm it is, and document why.
        if (!allowsTreeSuperset(policy, original, schema, superset.treeSchema.get(key))) {
            return false;
        }
    }
    return true;
}
exports.allowsRepoSuperset = allowsRepoSuperset;
/**
 * @alpha
 */
function isNeverField(policy, originalData, field) {
    return isNeverFieldRecursive(policy, originalData, field, new Set());
}
exports.isNeverField = isNeverField;
function isNeverFieldRecursive(policy, originalData, field, parentTypeStack) {
    if ((policy.fieldKinds.get(field.kind.identifier) ?? (0, util_1.fail)("missing field kind"))
        .multiplicity === fieldKind_1.Multiplicity.Value &&
        field.types !== undefined) {
        for (const type of field.types) {
            if (!isNeverTreeRecursive(policy, originalData, originalData.treeSchema.get(type), parentTypeStack)) {
                return false;
            }
        }
        // This field requires at least one child, and there are no types permitted in it that can exist,
        // so this is a never field (field which no sequence of children content could ever be in schema for)
        return true;
    }
    return false;
}
exports.isNeverFieldRecursive = isNeverFieldRecursive;
/**
 * Returns true iff there are no possible trees that could meet this schema.
 * Trees which are infinite (like endless linked lists) are considered impossible.
 *
 * `undefined` means the schema is not present and thus a NeverTree.
 */
function isNeverTree(policy, originalData, tree) {
    return isNeverTreeRecursive(policy, originalData, tree, new Set());
}
exports.isNeverTree = isNeverTree;
/**
 * Returns true iff there are no possible trees that could meet this schema.
 * Trees which are infinite (like endless linked lists) are considered impossible.
 *
 * `undefined` means the schema is not present and thus a NeverTree.
 */
function isNeverTreeRecursive(policy, originalData, tree, parentTypeStack) {
    if (tree === undefined) {
        return true;
    }
    if (parentTypeStack.has(tree)) {
        return true;
    }
    try {
        parentTypeStack.add(tree);
        if ((policy.fieldKinds.get(normalizeField(tree.mapFields).kind.identifier) ??
            (0, util_1.fail)("missing field kind")).multiplicity === fieldKind_1.Multiplicity.Value) {
            return true;
        }
        for (const field of tree.structFields.values()) {
            // TODO: this can recurse infinitely for schema that include themselves in a value field.
            // This breaks even if there are other allowed types.
            // Such schema should either be rejected (as an error here) or considered never (and thus detected by this).
            // This can be done by passing a set/stack of current types recursively here.
            if (isNeverFieldRecursive(policy, originalData, field, parentTypeStack)) {
                return true;
            }
        }
        return false;
    }
    finally {
        parentTypeStack.delete(tree);
    }
}
exports.isNeverTreeRecursive = isNeverTreeRecursive;
function normalizeField(schema) {
    return schema ?? core_1.storedEmptyFieldSchema;
}
exports.normalizeField = normalizeField;
//# sourceMappingURL=comparison.js.map