"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var sp_core_library_1 = require("@microsoft/sp-core-library");
var sp_diagnostics_1 = require("@microsoft/sp-diagnostics");
var sp_page_context_1 = require("@microsoft/sp-page-context");
var sp_http_1 = require("@microsoft/sp-http");
var DynamicDataProvider_1 = (0, tslib_1.__importDefault)(require("./DynamicDataProvider"));
var DynamicDataSourceManager_1 = (0, tslib_1.__importDefault)(require("./DynamicDataSourceManager"));
/**
 * The base class for context objects for client-side components.
 *
 * @remarks
 * A "context" object is a collection of well-known services and other
 * objects that are likely to be needed by business logic that interacts with a
 * associated component.  Each component type has its own specialized subclass
 * of BaseComponentContext, e.g. WebPartContext for web parts, ExtensionContext for
 * client-side extensions, and so on.
 *
 * @privateRemarks
 *
 * NOTE: The context object is a convenience that avoids the overhead of
 * properly factoring the dependencies for every class in a project.
 * If lots of members are added to the context without discipline, it can
 * easily devolve into a poor engineering practice (where "everything depends
 * on everything else").  Think carefully before adding more objects to the context object.
 * Is it really a core scenario?  Is it unnecessarily coupling your package to another
 * package?  Is there a better place to put the object?
 *
 * Only add data properties or well-defined classes with ubiquitous applicability.
 * Never add loose functions, callbacks, or settable properties to the context.
 *
 * @public
 */
var BaseComponentContext = /** @class */ (function () {
    /** @internal */
    function BaseComponentContext(parameters) {
        var _this = this;
        this._isDisposed = false;
        this._isServedFromLocalhost = false;
        sp_core_library_1.Validate.isNotNullOrUndefined(parameters, 'parameters');
        sp_core_library_1.Validate.isNotNullOrUndefined(parameters.manifest, 'manifest');
        this.manifest = parameters.manifest;
        var loaderConfig = this.manifest.loaderConfig;
        if (loaderConfig && loaderConfig.internalModuleBaseUrls && loaderConfig.internalModuleBaseUrls.length) {
            this._isServedFromLocalhost = /^http(s)?\:\/\/(localhost|127.0.0.1)/gim.test(loaderConfig.internalModuleBaseUrls[0]);
        }
        sp_core_library_1.Validate.isNotNullOrUndefined(parameters.parentServiceScope, 'parentServiceScope');
        this._serviceScope = parameters.parentServiceScope.startNewChild();
        // TODO: VSO:507790 Reconcile this with ClientSideWebPartManager._createWebPartTag()
        sp_core_library_1.Validate.isNotNullOrUndefined(parameters.loggingTag, 'loggingTag');
        this.serviceScope.provide(sp_diagnostics_1._logSourceServiceKey, sp_diagnostics_1._LogSource.create(parameters.loggingTag));
        sp_core_library_1.Validate.isNonemptyString(parameters.instanceId, 'instanceId');
        this._instanceId = parameters.instanceId;
        this._httpClient = this.serviceScope.createDefaultAndProvide(sp_http_1.HttpClient.serviceKey);
        this._spHttpClient = this.serviceScope.createDefaultAndProvide(sp_http_1.SPHttpClient.serviceKey);
        this._msGraphClientFactory = this.serviceScope.createDefaultAndProvide(sp_http_1.MSGraphClientFactory.serviceKey);
        this._aadClientFactory = this.serviceScope.createDefaultAndProvide(sp_http_1.AadHttpClientFactory.serviceKey);
        this._aadTokenFactory = this.serviceScope.createDefaultAndProvide(sp_http_1.AadTokenProviderFactory.serviceKey);
        this.serviceScope.provide(sp_core_library_1._ClientManifestData.serviceKey, new sp_core_library_1._ClientManifestData(this.serviceScope, this.manifest));
        // Consume services
        this.serviceScope.whenFinished(function () {
            _this._pageContext = _this.serviceScope.consume(sp_page_context_1.PageContext.serviceKey);
        });
        this._dynamicDataProvider = new DynamicDataProvider_1.default();
        this._dynamicDataSourceManager = new DynamicDataSourceManager_1.default();
    }
    Object.defineProperty(BaseComponentContext.prototype, "isDisposed", {
        /**
         * Returns true if the context is disposed.
         */
        get: function () {
            return this._isDisposed;
        },
        enumerable: false,
        configurable: true
    });
    /**
     * Disposes the base component context.
     */
    BaseComponentContext.prototype.dispose = function () {
        if (!this._isDisposed) {
            if (this._dynamicDataProvider) {
                this._dynamicDataProvider.dispose();
            }
            if (this._dynamicDataSourceManager) {
                this._dynamicDataSourceManager.dispose();
            }
            delete this._serviceScope;
            delete this._pageContext;
            delete this._dynamicDataProvider;
            delete this._dynamicDataSourceManager;
            this._isDisposed = true;
        }
    };
    Object.defineProperty(BaseComponentContext.prototype, "serviceScope", {
        /**
         * The associated {@link @microsoft/sp-core-library#ServiceScope} for this component.
         * @remarks
         * A child service scope is created for each client-side component.
         */
        get: function () {
            return this._serviceScope;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(BaseComponentContext.prototype, "instanceId", {
        /**
         * {@inheritDoc BaseComponent.instanceId}
         */
        get: function () {
            return this._instanceId;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(BaseComponentContext.prototype, "pageContext", {
        /**
         * The page context provides standard definitions for common SharePoint objects
         * that need to be shared between the client-side application, web parts, and other
         * components.
         */
        get: function () {
            return this._pageContext;
        },
        enumerable: false,
        configurable: true
    });
    /**
     * Creates a Dynamic Data Provider for the component.
     *
     * @param component - Component tied to the Dynamic Data Provider
     * @param serviceScope - ServiceScope to consume the Dynamic Data Manager from. Must be finished.
     * @internal
     */
    BaseComponentContext.prototype._initializeDynamicData = function (component, serviceScope) {
        this._dynamicDataProvider._initialize(component, serviceScope);
        this._dynamicDataSourceManager._initialize(component, serviceScope);
    };
    Object.defineProperty(BaseComponentContext.prototype, "dynamicDataProvider", {
        /**
         * Returns the Dynamic Data Provider associated with the component.
         */
        get: function () {
            return this._dynamicDataProvider;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(BaseComponentContext.prototype, "dynamicDataSourceManager", {
        /**
         * Returns the Dynamic Data Source Manager associated with the component.
         */
        get: function () {
            return this._dynamicDataSourceManager;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(BaseComponentContext.prototype, "aadHttpClientFactory", {
        /**
         * The instance of AadHttpClientFactory created for this instance of component
         */
        get: function () {
            return this._aadClientFactory;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(BaseComponentContext.prototype, "aadTokenProviderFactory", {
        /**
         * The instance of AadTokenProviderFactory created for this instance of component
         */
        get: function () {
            return this._aadTokenFactory;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(BaseComponentContext.prototype, "msGraphClientFactory", {
        /**
         * The instance of MSGraphClientFactory created for this instance of component
         */
        get: function () {
            return this._msGraphClientFactory;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(BaseComponentContext.prototype, "httpClient", {
        /**
         * The instance of HttpClient created for this instance of component
         */
        get: function () {
            return this._httpClient;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(BaseComponentContext.prototype, "spHttpClient", {
        /**
         * The instance of SpHttpClient created for this instance of component
         */
        get: function () {
            return this._spHttpClient;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(BaseComponentContext.prototype, "isServedFromLocalhost", {
        /**
         * Returns true if the solution is being served from localhost
         */
        get: function () {
            return this._isServedFromLocalhost;
        },
        enumerable: false,
        configurable: true
    });
    return BaseComponentContext;
}());
exports.default = BaseComponentContext;
//# sourceMappingURL=BaseComponentContext.js.map