/*
 * Application Insights JavaScript Web SDK - Basic, 3.2.0
 * Copyright (c) Microsoft and contributors. All rights reserved.
 */


import dynamicProto from "@microsoft/dynamicproto-js";
import { Sender } from "@microsoft/applicationinsights-channel-js";
import { DEFAULT_BREEZE_PATH, parseConnectionString } from "@microsoft/applicationinsights-common";
import { AppInsightsCore, cfgDfValidate, createDynamicConfig, onConfigChange, proxyFunctions } from "@microsoft/applicationinsights-core-js";
import { isNullOrUndefined, objDefine, throwError } from "@nevware21/ts-utils";
import { _DYN_CONNECTION_STRING, _DYN_ENDPOINT_URL, _DYN_INSTRUMENTATION_KEY, _DYN_USER_OVERRIDE_ENDPOI0 } from "./__DynamicConstants";
var defaultConfigValues = {
    diagnosticLogInterval: cfgDfValidate(_chkDiagLevel, 10000)
};
function _chkDiagLevel(value) {
    // Make sure we have a value > 0
    return value && value > 0;
}
/**
 * @export
 * @class ApplicationInsights
 */
var ApplicationInsights = /** @class */ (function () {
    /**
     * Creates an instance of ApplicationInsights.
     * @param config
     * @memberof ApplicationInsights
     */
    function ApplicationInsights(config) {
        var core = new AppInsightsCore();
        var _config;
        // initialize the queue and config in case they are undefined
        if (isNullOrUndefined(config) ||
            (isNullOrUndefined(config[_DYN_INSTRUMENTATION_KEY /* @min:%2einstrumentationKey */]) && isNullOrUndefined(config[_DYN_CONNECTION_STRING /* @min:%2econnectionString */]))) {
            throwError("Invalid input configuration");
        }
        dynamicProto(ApplicationInsights, this, function (_self) {
            // Define _self.config
            objDefine(_self, "config", {
                g: function () { return _config; }
            });
            _initialize();
            _self.initialize = _initialize;
            _self.track = _track;
            proxyFunctions(_self, core, [
                "flush",
                "pollInternalLogs",
                "stopPollingInternalLogs",
                "unload",
                "getPlugin",
                "addPlugin",
                "evtNamespace",
                "addUnloadCb",
                "onCfgChange"
            ]);
            function _initialize() {
                var cfgHandler = createDynamicConfig(config || {}, defaultConfigValues);
                _config = cfgHandler.cfg;
                core.addUnloadHook(onConfigChange(cfgHandler, function () {
                    if (_config[_DYN_CONNECTION_STRING /* @min:%2econnectionString */]) {
                        var cs = parseConnectionString(_config[_DYN_CONNECTION_STRING /* @min:%2econnectionString */]);
                        var ingest = cs.ingestionendpoint;
                        _config[_DYN_ENDPOINT_URL /* @min:%2eendpointUrl */] = _config[_DYN_USER_OVERRIDE_ENDPOI0 /* @min:%2euserOverrideEndpointUrl */] ? _config[_DYN_USER_OVERRIDE_ENDPOI0 /* @min:%2euserOverrideEndpointUrl */] : (ingest + DEFAULT_BREEZE_PATH); // only add /v2/track when from connectionstring
                        _config[_DYN_INSTRUMENTATION_KEY /* @min:%2einstrumentationKey */] = cs.instrumentationkey || _config[_DYN_INSTRUMENTATION_KEY /* @min:%2einstrumentationKey */];
                    }
                    // userOverrideEndpointUrl have the highest priority
                    _config[_DYN_ENDPOINT_URL /* @min:%2eendpointUrl */] = _config[_DYN_USER_OVERRIDE_ENDPOI0 /* @min:%2euserOverrideEndpointUrl */] ? _config[_DYN_USER_OVERRIDE_ENDPOI0 /* @min:%2euserOverrideEndpointUrl */] : _config[_DYN_ENDPOINT_URL /* @min:%2eendpointUrl */];
                }));
                // initialize core
                core.initialize(_config, [new Sender()]);
            }
        });
        function _track(item) {
            if (item) {
                // to pass sender.processTelemetry()
                item.baseData = item.baseData || {};
                item.baseType = item.baseType || "EventData";
            }
            core.track(item);
        }
    }
// Removed Stub for ApplicationInsights.prototype.initialize.
// Removed Stub for ApplicationInsights.prototype.track.
// Removed Stub for ApplicationInsights.prototype.flush.
// Removed Stub for ApplicationInsights.prototype.pollInternalLogs.
// Removed Stub for ApplicationInsights.prototype.stopPollingInternalLogs.
// Removed Stub for ApplicationInsights.prototype.unload.
// Removed Stub for ApplicationInsights.prototype.getPlugin.
// Removed Stub for ApplicationInsights.prototype.addPlugin.
// Removed Stub for ApplicationInsights.prototype.evtNamespace.
// Removed Stub for ApplicationInsights.prototype.addUnloadCb.
// Removed Stub for ApplicationInsights.prototype.onCfgChange.
    // This is a workaround for an IE bug when using dynamicProto() with classes that don't have any
    // non-dynamic functions or static properties/functions when using uglify-js to minify the resulting code.
    ApplicationInsights.__ieDyn=1;

    return ApplicationInsights;
}());
export { ApplicationInsights };
export { AppInsightsCore, arrForEach, isNullOrUndefined, throwError, proxyFunctions } from "@microsoft/applicationinsights-core-js";
export { SeverityLevel } from "@microsoft/applicationinsights-common";
export { Sender } from "@microsoft/applicationinsights-channel-js";
//# sourceMappingURL=index.js.map