/*
 * Application Insights JavaScript SDK - Web, 3.3.3-nightly3.2409-08
 * Copyright (c) Microsoft and contributors. All rights reserved.
 */


import { strUndefined } from "@microsoft/applicationinsights-core-js";
import { getInst } from "@nevware21/ts-utils";
import { ApplicationInsightsContainer } from "./ApplicationInsightsContainer";
import { _DYN_VERSION } from "./__DynamicConstants";
// ----------------------------------------------------------------------------------------------------
// Exports available from the Cdn bundles
// ----------------------------------------------------------------------------------------------------
export { AppInsightsSku as ApplicationInsights } from "./AISku";
export { LoggingSeverity, PerfEvent, PerfManager, doPerf, newId, newGuid, random32, randomValue, generateW3CId, findW3cTraceParent, findMetaTag, mergeEvtNamespace, eventOn, eventOff, addEventHandler, removeEventHandler, isBeaconsSupported } from "@microsoft/applicationinsights-core-js";
export { RequestHeaders, DisabledPropertyName, DEFAULT_BREEZE_ENDPOINT, SeverityLevel, DistributedTracingModes, PropertiesPluginIdentifier, BreezeChannelIdentifier, AnalyticsPluginIdentifier } from "@microsoft/applicationinsights-common";
// ----------------------------------------------------------------------------------------------------
// End of Exports available from the Cdn bundles
// ----------------------------------------------------------------------------------------------------
function _logWarn(aiName, message) {
    // TODO: Find better place to warn to console when SDK initialization fails
    var _console = getInst("console");
    if (_console && _console.warn) {
        _console.warn("Failed to initialize AppInsights JS SDK for instance " + (aiName || "<unknown>") + " - " + message);
    }
}
// should be global function that should load as soon as SDK loads
try {
    // E2E sku on load initializes core and pipeline using snippet as input for configuration
    // tslint:disable-next-line: no-var-keyword
    var aiName;
    if (typeof window !== strUndefined) {
        var _window = window;
        aiName = _window["appInsightsSDK"] || "appInsights";
        if (document.currentScript) {
            aiName = document.currentScript.getAttribute("data-ai-name") || aiName;
        }
        if (typeof JSON !== strUndefined) {
            // get snippet or initialize to an empty object
            if (_window[aiName] !== undefined) {
                // this is the typical case for browser+snippet
                var snippet = _window[aiName] || { version: 2.0 };
                // overwrite snippet with full appInsights
                // only initiaize if required and detected snippet version is >= 2 or not defined
                if ((snippet[_DYN_VERSION /* @min:%2eversion */] >= 2 && _window[aiName].initialize) || snippet[_DYN_VERSION /* @min:%2eversion */] === undefined) {
                    ApplicationInsightsContainer.getAppInsights(snippet, snippet[_DYN_VERSION /* @min:%2eversion */]);
                }
            }
        }
        else {
            _logWarn(aiName, "Missing JSON - you must supply a JSON polyfill!");
        }
    }
    else {
        _logWarn(aiName, "Missing window");
    }
    // Hack: If legacy SDK exists, skip this step (Microsoft.ApplicationInsights exists).
    // else write what was there for v2 SDK prior to rollup bundle output name change.
    // e.g Microsoft.ApplicationInsights.ApplicationInsights, Microsoft.ApplicationInsights.Telemetry
    // @todo uncomment once integration tests for this can be added
    // if (typeof window !== strUndefined && window && ((window as any).Microsoft && !(window as any).Microsoft.ApplicationInsights)) {
    //     (window as any).Microsoft = (window as any).Microsoft || {};
    //     (window as any).Microsoft.ApplicationInsights = {
    //         ApplicationInsights, Telemetry
    //     };
    // }
}
catch (e) {
    _logWarn(aiName, e.message);
}
//# sourceMappingURL=Init.js.map