/// <reference path="./IPlugin.ts" />
/// <reference path="./IEmosGlobalConfig.ts" />
/// <reference path="./EmosConfig.ts" />
/// <reference path="../lib-definitions/emos.d.ts" />
var econda;
(function (econda) {
    var tracking;
    (function (tracking) {
        var EmosConfig = econda.tracking.EmosConfig;
        /**
        * Plug-in manager acts as an defined interface to emos3 tracking library.
        *
        * <h3>Example plug-in:</h3>
        *
        *     var myPlugin = {
        *	      onRequest: function(emosProperties) { console.log(emosProperties); }
        *     };
        *     econda.tracking.PluginManager.registerPlugin(myPlugin);
        *
        * @class econda.tracking.PluginManager
        */
        var PluginManager = (function () {
            function PluginManager() {
            }
            /**
             * Register plugin
             * @static
             * @param {Object} plugin Plug-in to add
             */
            PluginManager.registerPlugin = function (plugin) {
                if (PluginManager._plugins.indexOf(plugin) === -1) {
                    PluginManager._plugins.push(plugin);
                }
            };
            PluginManager.unregisterPlugin = function (plugin) {
                (typeof plugin === 'function')
                    ? this.removeAllPlugInsOfType(plugin)
                    : this.removePlugInByInstance(plugin);
            };
            PluginManager.removeAllPlugInsOfType = function (type) {
                for (var n = 0; n < this._plugins.length; n++) {
                    if (this._plugins[n] instanceof type) {
                        this._plugins.splice(n--, 1);
                    }
                }
            };
            PluginManager.removePlugInByInstance = function (plugin) {
                for (var n = 0; n < this._plugins.length; n++) {
                    if (this._plugins[n] === plugin) {
                        this._plugins.splice(n, 1);
                    }
                }
            };
            /**
             * Remove all registered plug-ins
             * @static
             */
            PluginManager.clearAll = function () {
                PluginManager._plugins = [];
            };
            /**
             * Returns an array of all registered plug-ins.
             * @static
             * @return {Object[]}
             */
            PluginManager.getRegisteredPlugins = function () {
                return PluginManager._plugins;
            };
            /**
             * Internal function. That's the function we register as emos3 plugin.
             * @private
             * @static
             */
            PluginManager.handleTrackingRequestEvent = function (properties, globalConfigProperties, config) {
                if (properties === void 0) { properties = {}; }
                if (config === void 0) { config = {}; }
                var globalConfig = new EmosConfig(globalConfigProperties);
                if (typeof config['cb'] != 'object' || config['cb'] === null) {
                    config.cb = [];
                }
                config.cb.push(function () {
                    PluginManager.handleTrackingAfterRequestCallback(properties, globalConfig, config);
                });
                for (var n = 0; n < PluginManager._plugins.length; n++) {
                    var plugin = PluginManager._plugins[n];
                    if (typeof plugin.onRequest === 'function') {
                        plugin.onRequest(properties, globalConfig, config);
                    }
                }
            };
            /**
             * Internal function. That's the function we pass as a callback to emos3.
             * @private
             * @static
             */
            PluginManager.handleTrackingAfterRequestCallback = function (properties, globalConfig, config) {
                if (properties === void 0) { properties = {}; }
                if (config === void 0) { config = {}; }
                for (var n = 0; n < PluginManager._plugins.length; n++) {
                    var plugin = PluginManager._plugins[n];
                    if (typeof plugin.onAfterRequest === 'function') {
                        plugin.onAfterRequest(properties, globalConfig, config);
                    }
                }
            };
            /**
             * Register plugin manager as emos3 plugin
             * @static
             */
            PluginManager.registerManager = function () {
                var emos3 = window['emos3'];
                if (typeof emos3 !== 'object' || emos3 === null) {
                    window.emos3 = {};
                    emos3 = window['emos3'];
                }
                if (typeof emos3['plugins'] !== 'object' || emos3.plugins === null) {
                    emos3['plugins'] = [];
                }
                // check if already registered
                for (var i = 0; i < emos3.plugins.length; i++) {
                    if (emos3.plugins[i] === PluginManager.handleTrackingRequestEvent) {
                        return;
                    }
                }
                // add manager as emos3 plugin
                emos3.plugins.push(PluginManager.handleTrackingRequestEvent);
            };
            /**
             * Array of registered plugins
             * @property {Object[]} _plugins
             * @static
             * @private
             */
            PluginManager._plugins = [];
            return PluginManager;
        }());
        tracking.PluginManager = PluginManager;
        econda.tracking.PluginManager.registerManager();
    })(tracking = econda.tracking || (econda.tracking = {}));
})(econda || (econda = {}));