"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateDatastoreCompatibility = exports.validateLoaderCompatibility = exports.dataStoreSupportRequirements = exports.runtimeCompatDetailsForDataStore = exports.loaderSupportRequirements = exports.runtimeCompatDetailsForLoader = exports.runtimeCoreCompatDetails = void 0;
const client_utils_1 = require("@fluid-internal/client-utils");
const internal_1 = require("@fluidframework/runtime-definitions/internal");
const internal_2 = require("@fluidframework/telemetry-utils/internal");
const packageVersion_js_1 = require("./packageVersion.js");
/**
 * The core compatibility details of the Runtime layer that is the same across all layer boundaries.
 * @internal
 */
exports.runtimeCoreCompatDetails = {
    /**
     * The package version of the Runtime layer.
     */
    pkgVersion: packageVersion_js_1.pkgVersion,
    /**
     * The current generation of the Runtime layer.
     */
    generation: 1,
};
/**
 * Runtime's compatibility details that is exposed to the Loader layer.
 * @internal
 */
exports.runtimeCompatDetailsForLoader = {
    ...exports.runtimeCoreCompatDetails,
    /**
     * The features supported by the Runtime layer across the Runtime / Loader boundary.
     */
    supportedFeatures: new Set(),
};
/**
 * The requirements that the Loader layer must meet to be compatible with this Runtime.
 * @internal
 */
exports.loaderSupportRequirements = {
    /**
     * Minimum generation that Loader must be at to be compatible with Runtime. Note that 0 is used here so
     * that Loader layers before the introduction of the layer compatibility enforcement are compatible.
     */
    minSupportedGeneration: 0,
    /**
     * The features that the Loader must support to be compatible with Runtime.
     */
    requiredFeatures: [],
};
/**
 * Runtime's compatibility details that is exposed to the DataStore layer.
 * @internal
 */
exports.runtimeCompatDetailsForDataStore = {
    ...exports.runtimeCoreCompatDetails,
    /**
     * The features supported by the Runtime layer across the Runtime / DataStore boundary.
     */
    supportedFeatures: new Set([internal_1.encodeHandlesInContainerRuntime]),
};
/**
 * The requirements that the DataStore layer must meet to be compatible with this Runtime.
 * @internal
 */
exports.dataStoreSupportRequirements = {
    /**
     * Minimum generation that DataStore must be at to be compatible with Runtime. Note that 0 is used here so
     * that DataStore layers before the introduction of the layer compatibility enforcement are compatible.
     */
    minSupportedGeneration: 0,
    /**
     * The features that the DataStore must support to be compatible with Runtime.
     */
    requiredFeatures: [],
};
/**
 * Validates that the Loader layer is compatible with this Runtime.
 * @internal
 */
function validateLoaderCompatibility(maybeloaderCompatDetailsForRuntime, disposeFn) {
    const layerCheckResult = (0, client_utils_1.checkLayerCompatibility)(exports.loaderSupportRequirements, maybeloaderCompatDetailsForRuntime);
    if (!layerCheckResult.isCompatible) {
        const error = new internal_2.UsageError("Runtime is not compatible with Loader", {
            errorDetails: JSON.stringify({
                runtimeVersion: exports.runtimeCoreCompatDetails.pkgVersion,
                loaderVersion: maybeloaderCompatDetailsForRuntime?.pkgVersion,
                runtimeGeneration: exports.runtimeCoreCompatDetails.generation,
                loaderGeneration: maybeloaderCompatDetailsForRuntime?.generation,
                minSupportedGeneration: exports.loaderSupportRequirements.minSupportedGeneration,
                isGenerationCompatible: layerCheckResult.isGenerationCompatible,
                unsupportedFeatures: layerCheckResult.unsupportedFeatures,
            }),
        });
        disposeFn(error);
        throw error;
    }
}
exports.validateLoaderCompatibility = validateLoaderCompatibility;
/**
 * Validates that the DataStore layer is compatible with this Runtime.
 * @internal
 */
function validateDatastoreCompatibility(maybeDataStoreCompatDetails, disposeFn) {
    const layerCheckResult = (0, client_utils_1.checkLayerCompatibility)(exports.dataStoreSupportRequirements, maybeDataStoreCompatDetails);
    if (!layerCheckResult.isCompatible) {
        const error = new internal_2.UsageError("Runtime is not compatible with DataStore", {
            errorDetails: JSON.stringify({
                runtimeVersion: exports.runtimeCoreCompatDetails.pkgVersion,
                dataStoreVersion: maybeDataStoreCompatDetails?.pkgVersion,
                runtimeGeneration: exports.runtimeCoreCompatDetails.generation,
                dataStoreGeneration: maybeDataStoreCompatDetails?.generation,
                minSupportedGeneration: exports.dataStoreSupportRequirements.minSupportedGeneration,
                isGenerationCompatible: layerCheckResult.isGenerationCompatible,
                unsupportedFeatures: layerCheckResult.unsupportedFeatures,
            }),
        });
        disposeFn();
        throw error;
    }
}
exports.validateDatastoreCompatibility = validateDatastoreCompatibility;
//# sourceMappingURL=runtimeLayerCompatState.js.map