/*
 * Application Insights JavaScript SDK - Properties Plugin, 3.4.3
 * Copyright (c) Microsoft and contributors. All rights reserved.
 */
/**
* PropertiesPlugin.ts
* @copyright Microsoft 2018
*/
var _a;
import { __extendsFn as __extends } from "@microsoft/applicationinsights-shims";
import dynamicProto from "@microsoft/dynamicproto-js";
import { BaseTelemetryPlugin, BreezeChannelIdentifier, PageViewEnvelopeType, PropertiesPluginIdentifier, _InternalLogMessage, _logInternalMessage, createProcessTelemetryContext, getNavigator, getSetValue, isNullOrUndefined, onConfigChange, utlSetStoragePrefix } from "@microsoft/applicationinsights-core-js";
import { isString, objDeepFreeze, objDefine } from "@nevware21/ts-utils";
import { TelemetryContext } from "./TelemetryContext";
import { _DYN_APPLY_APPLICATION_CO1, _DYN_APPLY_INTERNAL_CONTE5, _DYN_APPLY_LOCATION_CONTE4, _DYN_APPLY_OPERATING_SYST3, _DYN_APPLY_OPERATION_CONT2, _DYN_APPLY_SESSION_CONTEX0, _DYN_GET_SESSION_ID, _DYN_IS_NEW_USER, _DYN_IS_USER_COOKIE_SET, _DYN_SESSION, _DYN_SESSION_COOKIE_POSTF6, _DYN_SESSION_MANAGER } from "./__DynamicConstants";
var undefString;
var nullValue = null;
var _defaultConfig = objDeepFreeze((_a = {
        accountId: nullValue,
        sessionRenewalMs: 30 * 60 * 1000,
        samplingPercentage: 100,
        sessionExpirationMs: 24 * 60 * 60 * 1000,
        cookieDomain: nullValue,
        sdkExtension: nullValue,
        isBrowserLinkTrackingEnabled: false,
        appId: nullValue
    },
    _a[_DYN_GET_SESSION_ID /* @min:getSessionId */] = nullValue,
    _a.namePrefix = undefString,
    _a[_DYN_SESSION_COOKIE_POSTF6 /* @min:sessionCookiePostfix */] = undefString,
    _a.userCookiePostfix = undefString,
    _a.idLength = 22,
    _a.getNewId = nullValue,
    _a));
var PropertiesPlugin = /** @class */ (function (_super) {
    __extends(PropertiesPlugin, _super);
    function PropertiesPlugin() {
        var _this = _super.call(this) || this;
        _this.priority = 110;
        _this.identifier = PropertiesPluginIdentifier;
        var _extensionConfig;
        var _context;
        var _disableUserInitMessage;
        dynamicProto(PropertiesPlugin, _this, function (_self, _base) {
            _initDefaults();
            objDefine(_self, "context", {
                g: function () {
                    return _context;
                }
            });
            _self.initialize = function (config, core, extensions, pluginChain) {
                _base.initialize(config, core, extensions, pluginChain);
                _populateDefaults(config);
            };
            /**
             * Add Part A fields to the event
             * @param event - The event that needs to be processed
             */
            _self.processTelemetry = function (event, itemCtx) {
                if (!isNullOrUndefined(event)) {
                    itemCtx = _self._getTelCtx(itemCtx);
                    // If the envelope is PageView, reset the internal message count so that we can send internal telemetry for the new page.
                    if (event.name === PageViewEnvelopeType) {
                        itemCtx.diagLog().resetInternalMessageCount();
                    }
                    var theContext = (_context || {});
                    if (theContext[_DYN_SESSION /* @min:%2esession */]) {
                        // If customer did not provide custom session id update the session manager
                        if (!isString(_context[_DYN_SESSION /* @min:%2esession */].id) && theContext[_DYN_SESSION_MANAGER /* @min:%2esessionManager */]) {
                            theContext[_DYN_SESSION_MANAGER /* @min:%2esessionManager */].update();
                        }
                    }
                    var userCtx = theContext.user;
                    if (userCtx && !userCtx[_DYN_IS_USER_COOKIE_SET /* @min:%2eisUserCookieSet */]) {
                        userCtx.update(theContext.user.id);
                    }
                    _processTelemetryInternal(event, itemCtx);
                    if (userCtx && userCtx[_DYN_IS_NEW_USER /* @min:%2eisNewUser */]) {
                        userCtx[_DYN_IS_NEW_USER /* @min:%2eisNewUser */] = false;
                        if (!_disableUserInitMessage) {
                            var message = new _InternalLogMessage(72 /* _eInternalMessageId.SendBrowserInfoOnUserInit */, ((getNavigator() || {}).userAgent || ""));
                            _logInternalMessage(itemCtx.diagLog(), 1 /* eLoggingSeverity.CRITICAL */, message);
                        }
                    }
                    _self.processNext(event, itemCtx);
                }
            };
            _self._doTeardown = function (unloadCtx, unloadState) {
                _initDefaults();
            };
            function _initDefaults() {
                _extensionConfig = null;
                _context = null;
                _disableUserInitMessage = true;
            }
            function _populateDefaults(config) {
                var identifier = _self.identifier;
                var core = _self.core;
                // This function will be re-called whenever any referenced configuration is changed
                _self._addHook(onConfigChange(config, function () {
                    var ctx = createProcessTelemetryContext(null, config, core);
                    if (config.storagePrefix) {
                        utlSetStoragePrefix(config.storagePrefix);
                    }
                    _disableUserInitMessage = config.disableUserInitMessage === false ? false : true;
                    _extensionConfig = ctx.getExtCfg(identifier, _defaultConfig);
                    // Test hook to allow accessing the internal values -- explicitly not defined as an available property on the class
                    _self["_extConfig"] = _extensionConfig;
                }));
                // This is outside of the onConfigChange as we don't want to update (replace) these values whenever a referenced config item changes
                _context = new TelemetryContext(core, _extensionConfig, _self._unloadHooks);
                _self.context.appId = function () {
                    var breezeChannel = core.getPlugin(BreezeChannelIdentifier);
                    return breezeChannel ? breezeChannel.plugin["_appId"] : null;
                };
            }
            function _processTelemetryInternal(evt, itemCtx) {
                // Set Part A fields
                getSetValue(evt, "tags", []);
                getSetValue(evt, "ext", {});
                var ctx = _self.context;
                ctx[_DYN_APPLY_SESSION_CONTEX0 /* @min:%2eapplySessionContext */](evt, itemCtx);
                ctx[_DYN_APPLY_APPLICATION_CO1 /* @min:%2eapplyApplicationContext */](evt, itemCtx);
                ctx.applyDeviceContext(evt, itemCtx);
                ctx[_DYN_APPLY_OPERATION_CONT2 /* @min:%2eapplyOperationContext */](evt, itemCtx);
                ctx.applyUserContext(evt, itemCtx);
                ctx[_DYN_APPLY_OPERATING_SYST3 /* @min:%2eapplyOperatingSystemContxt */](evt, itemCtx);
                ctx.applyWebContext(evt, itemCtx);
                ctx[_DYN_APPLY_LOCATION_CONTE4 /* @min:%2eapplyLocationContext */](evt, itemCtx); // legacy tags
                ctx[_DYN_APPLY_INTERNAL_CONTE5 /* @min:%2eapplyInternalContext */](evt, itemCtx); // legacy tags
                ctx.cleanUp(evt, itemCtx);
            }
        });
        return _this;
    }
// Removed Stub for PropertiesPlugin.prototype.initialize.
// Removed Stub for PropertiesPlugin.prototype.processTelemetry.
    // 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.
    PropertiesPlugin.__ieDyn=1;

    return PropertiesPlugin;
}(BaseTelemetryPlugin));
export default PropertiesPlugin;
//# sourceMappingURL=PropertiesPlugin.js.map