"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.SchemaCompatibilityTester = void 0;
const index_js_1 = require("../../feature-libraries/index.js");
const toStoredSchema_js_1 = require("../toStoredSchema.js");
const discrepancies_js_1 = require("./discrepancies.js");
/**
 * A collection of View information for schema
 * @remarks
 * This contains everything needed to determine compatibility with a given stored schema.
 */
class SchemaCompatibilityTester {
    constructor(
    /**
     * Schema for the view
     */
    viewSchema) {
        this.viewSchema = viewSchema;
    }
    /**
     * Determines the compatibility of a stored document
     * (based on its stored schema) with a viewer (based on its view schema).
     *
     * Adapters can be provided to handle differences between the two schema.
     * Adapters should only use to types in the `view` SchemaRepository.
     *
     * TODO: this API violates the parse don't validate design philosophy.
     * It should be wrapped with (or replaced by) a parse style API.
     */
    checkCompatibility(stored) {
        // The public API surface assumes defaultSchemaPolicy
        const policy = index_js_1.defaultSchemaPolicy;
        // View schema allows a subset of documents that stored schema does, and the discrepancies are allowed by policy
        // determined by the view schema (i.e. objects with extra optional fields in the stored schema have opted into allowing this.
        // In the future, this would also include things like:
        // - fields with more allowed types in the stored schema than in the view schema have out-of-schema "unknown content" adapters
        let canView = true;
        for (const _discrepancy of (0, discrepancies_js_1.getDiscrepanciesInAllowedContent)(this.viewSchema, stored)) {
            canView = false;
            break;
        }
        const wouldUpgradeTo = (0, toStoredSchema_js_1.toUpgradeSchema)(this.viewSchema.root);
        const canUpgrade = (0, index_js_1.allowsRepoSuperset)(policy, stored, wouldUpgradeTo);
        // If true, then upgrading has no effect on what can be stored in the document.
        // TODO: This should likely be changed to indicate up a schema upgrade would be a no-op, including stored schema metadata.
        const isEquivalent = canView && canUpgrade && (0, index_js_1.allowsRepoSuperset)(policy, wouldUpgradeTo, stored);
        return {
            canView,
            canUpgrade,
            isEquivalent,
        };
    }
}
exports.SchemaCompatibilityTester = SchemaCompatibilityTester;
//# sourceMappingURL=schemaCompatibilityTester.js.map