"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var sp_core_library_1 = require("@microsoft/sp-core-library");
var ComponentStore_1 = tslib_1.__importDefault(require("../stores/ComponentStore"));
var ManifestStore_1 = tslib_1.__importDefault(require("../stores/ManifestStore"));
var componentConstants_1 = require("./componentConstants");
var ComponentOverrider = /** @class */ (function () {
    function ComponentOverrider() {
    }
    /**
     * Given a component id and a component module, it sets the component in the loader, therefore being
     * available without the need of loading it separately.
     * Should only be used when initializing the loader.
     *
     * @param componentId - Id of the component to override. There should be only one version of the component.
     * @param componentModule - Component module.
     */
    ComponentOverrider.overrideComponent = function (componentId, componentModule, serviceScope, moduleLoaderServiceKey) {
        sp_core_library_1.Validate.isNonemptyString(componentId, 'componentId');
        sp_core_library_1.Validate.isNotNullOrUndefined(componentModule, 'componentModule');
        // Needed for React16 rollout. Should be removed with version aware assembly manifests.
        var version = ComponentOverrider.getReactVersionIfNecessary(componentId);
        var manifest = ManifestStore_1.default.instance.tryGetManifest(componentId, version);
        // VSO bug 295355 makes it possible that manifest for odspUtilitiesBundle may not exist.
        if (!manifest) {
            return;
        }
        serviceScope.consume(moduleLoaderServiceKey).ensure(manifest, componentModule);
        ComponentStore_1.default.instance.storeLoadedComponent(manifest.id, manifest.version, componentModule);
    };
    /**
     * This is necessary because assemblies don't advertise the component version, and it may be the case where the page
     * requires both React 15 and React 16 manifests, but assemblies never have React 15 anymore.  When assemblies have
     * a deeper understanding of the versions that are included in them, this can be removed.
     */
    ComponentOverrider.getReactVersionIfNecessary = function (componentId) {
        if (componentId === componentConstants_1.reactComponentId || componentId === componentConstants_1.reactDomComponentId) {
            return componentConstants_1.react16Version;
        }
        else {
            return undefined;
        }
    };
    return ComponentOverrider;
}());
exports.default = ComponentOverrider;
//# sourceMappingURL=ComponentOverrider.js.map