/// <reference path="./IPlugin.ts" />
/// <reference path="./PluginManager.ts" />
/// <reference path="./EmosConfig.ts" />
/// <reference path="../debug.ts" />
/// <reference path="../cookie/Store.ts" />
/// <reference path="./VisitorId.ts" />
/// <reference path="../net/Uri.ts" />
var econda;
(function (econda) {
    var tracking;
    (function (tracking) {
        var CookieStore = econda.cookie.Store;
        var VisitorId = econda.tracking.VisitorId;
        /**
         * Tracking may use thrid party cookie to store the visitor id. This third party id will be the "real" visitor id and NOT the visitor id we see
         * in first party cookie. This class requests the visitor id stored in third party cookie and update the visitor id in first party cookie.
         *
         * @class econda.tracking.ThirdPartyVisitorIdPlugIn
         */
        var ThirdPartyVisitorIdPlugIn = (function () {
            function ThirdPartyVisitorIdPlugIn() {
            }
            ThirdPartyVisitorIdPlugIn.prototype.onAfterRequest = function (requestProperties, config) {
                var _this = this;
                if (config.isTrackThirdParty()
                    && !config.doNotTrack()
                    && !config.isSyncCacheId()
                    && config.VCL > 0) {
                    this.requestThirdPartyVisitorId(function (thirdPartyVisitorId) {
                        // if content and not "null" - there could be a newline at the end...
                        if (typeof thirdPartyVisitorId === 'string' && thirdPartyVisitorId.length > 6) {
                            VisitorId.update(thirdPartyVisitorId, {
                                domain: config.getCookieDomain(),
                                expires: config.getClientCookieLifetime()
                            });
                        }
                        _this.disableAndMarkSessionUpdated();
                    });
                }
                else {
                    econda.tracking.PluginManager.unregisterPlugin(ThirdPartyVisitorIdPlugIn);
                }
            };
            ThirdPartyVisitorIdPlugIn.prototype.disableAndMarkSessionUpdated = function () {
                econda.tracking.PluginManager.unregisterPlugin(ThirdPartyVisitorIdPlugIn);
                CookieStore.set({
                    name: 'ec_vid_updated',
                    value: '1'
                });
            };
            ThirdPartyVisitorIdPlugIn.prototype.requestThirdPartyVisitorId = function (callback) {
                try {
                    var xhr = new XMLHttpRequest();
                    xhr.withCredentials = true;
                    xhr.open('GET', this.buildServiceURL(), true);
                    xhr.onreadystatechange = function (_) {
                        if (xhr.status == 200) {
                            callback(xhr.responseText);
                        }
                    };
                    xhr.send();
                }
                catch (e) { }
            };
            /**
             * @return {string} Service URL with locations protocol.
             */
            ThirdPartyVisitorIdPlugIn.prototype.buildServiceURL = function () {
                var serviceURL = econda.net.Uri.detectProtocol() + '://cross.econda-monitor.de/vi';
                return serviceURL;
            };
            return ThirdPartyVisitorIdPlugIn;
        }());
        tracking.ThirdPartyVisitorIdPlugIn = ThirdPartyVisitorIdPlugIn;
        if (CookieStore.getValue('ec_vid_updated') !== '1') {
            econda.tracking.PluginManager.registerPlugin(new ThirdPartyVisitorIdPlugIn());
        }
    })(tracking = econda.tracking || (econda.tracking = {}));
})(econda || (econda = {}));