"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.allowsRepoSuperset = exports.allowsTreeSchemaIdentifierSuperset = exports.allowsFieldSuperset = exports.allowsValueSuperset = exports.allowsTreeSuperset = void 0;
const core_utils_1 = require("@fluidframework/core-utils");
const index_js_1 = require("../../util/index.js");
const index_js_2 = require("../../core/index.js");
const fieldKindWithEditor_js_1 = require("./fieldKindWithEditor.js");
const isNeverTree_js_1 = require("./isNeverTree.js");
/**
 * @returns true iff `superset` is a superset of `original`.
 *
 * This does not require a strict (aka proper) superset: equivalent schema will return true.
 *
 * `undefined` TreeNodeStoredSchema means the schema is not present (and thus treated as a NeverTree).
 */
function allowsTreeSuperset(policy, originalData, original, superset) {
    if ((0, isNeverTree_js_1.isNeverTree)(policy, originalData, original)) {
        return true;
    }
    if ((0, isNeverTree_js_1.isNeverTree)(policy, originalData, superset)) {
        return false;
    }
    (0, core_utils_1.assert)(original !== undefined, 0x716 /* only never trees have undefined schema */);
    (0, core_utils_1.assert)(superset !== undefined, 0x717 /* only never trees have undefined schema */);
    if (original instanceof index_js_2.LeafNodeStoredSchema) {
        if (superset instanceof index_js_2.LeafNodeStoredSchema) {
            return allowsValueSuperset(original.leafValue, superset.leafValue);
        }
        return false;
    }
    (0, core_utils_1.assert)(original instanceof index_js_2.MapNodeStoredSchema || original instanceof index_js_2.ObjectNodeStoredSchema, 0x893 /* unsupported node kind */);
    (0, core_utils_1.assert)(superset instanceof index_js_2.MapNodeStoredSchema || superset instanceof index_js_2.ObjectNodeStoredSchema, 0x894 /* unsupported node kind */);
    if (original instanceof index_js_2.MapNodeStoredSchema) {
        if (superset instanceof index_js_2.MapNodeStoredSchema) {
            return allowsFieldSuperset(policy, originalData, normalizeField(original.mapFields), normalizeField(superset.mapFields));
        }
        return false;
    }
    (0, core_utils_1.assert)(original instanceof index_js_2.ObjectNodeStoredSchema, 0x895 /* unsupported node kind */);
    if (superset instanceof index_js_2.MapNodeStoredSchema) {
        for (const [_key, field] of original.objectNodeFields) {
            if (!allowsFieldSuperset(policy, originalData, normalizeField(field), normalizeField(superset.mapFields))) {
                return false;
            }
        }
        return true;
    }
    (0, core_utils_1.assert)(superset instanceof index_js_2.ObjectNodeStoredSchema, 0x896 /* unsupported node kind */);
    return (0, index_js_1.compareSets)({
        a: original.objectNodeFields,
        b: superset.objectNodeFields,
        aExtra: (originalField) => allowsFieldSuperset(policy, originalData, original.objectNodeFields.get(originalField) ?? (0, index_js_1.fail)("missing expected field"), normalizeField(undefined)),
        bExtra: (supersetField) => allowsFieldSuperset(policy, originalData, normalizeField(undefined), superset.objectNodeFields.get(supersetField) ?? (0, index_js_1.fail)("missing expected field")),
        same: (sameField) => allowsFieldSuperset(policy, originalData, original.objectNodeFields.get(sameField) ?? (0, index_js_1.fail)("missing expected field"), superset.objectNodeFields.get(sameField) ?? (0, index_js_1.fail)("missing expected field")),
    });
}
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) {
    return original === superset;
}
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 (0, fieldKindWithEditor_js_1.withEditor)(policy.fieldKinds.get(original.kind.identifier) ?? (0, index_js_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.nodeSchema) {
        // 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.nodeSchema.get(key))) {
            return false;
        }
    }
    return true;
}
exports.allowsRepoSuperset = allowsRepoSuperset;
function normalizeField(schema) {
    return schema ?? index_js_2.storedEmptyFieldSchema;
}
exports.normalizeField = normalizeField;
//# sourceMappingURL=comparison.js.map