var esign = esign || {}; esign.ebugs = {}; esign.ebugs.key ="sRwHj26iTkzytqGEm8IdXcNhea1lBvgF"; esign.ebugs.url = "https://ebugs.esign.eu/report-clientside"; esign.ebugs.environment="production";
var ErrorLogging = {

	notify: function(err, lineno, colno, source, trace){

		var scope = this;

		var clientInfo = this.clientInfo();

		var fileRegex = new RegExp('.*\/([^\/]+)$');
		var file = source.replace(fileRegex, '$1');

		var data = {
				key: esign.ebugs.key,
				message: err,
				line: file + ':' + lineno + ',' + colno,
				url: clientInfo.location.href,
				level: 'JavaScript',
				trace: JSON.stringify(trace),
				data: JSON.stringify({
				file: {
					url: source
				},
				location: clientInfo.location,
				navigator: clientInfo.navigator,
				viewport: clientInfo.viewport
			})
		};

		if (typeof esign.ebugs.environment !== typeof undefined) data.environment = esign.ebugs.environment;
        
        fetch(esign.ebugs.url,
        {
            method: 'POST',
            body: JSON.stringify(data),
            headers: {
                'Content-Type': 'application/json',
                'Accept': 'application/json'
            },
        })
            .then(function(res) {
                if (!res.ok && res.status != 422) {
                    throw Error(res.statusText);
                }
                return res.json()
            })
            .then(function(data) {
                console.log(data.message)
            })
            .catch(function(err) {
                scope.notificationFailed(err)
            })

	},

	notificationFailed: function(err) {
		var scope = this;
		console.log(
			'%c\nAn error occured. An attempt to notify Esign\'s developers failed.\n' +
			'Please email following message to < info@esign.eu > :\n\n\n' +
			err + '\n' +
			scope.clientInfoString() + '\n' +
			'Thank you for helping us out. :)\n' +
			'Esign\n'
			, 'background-color: red; color: white;'
		)
	},

	clientInfo: function(){
		var info = {};

		var extract = {
			navigator: [
				'appCodeName',
				'appName',
				'appVersion',
				'cookieEnabled',
				'doNotTrack',
				'hardwareConcurrency',
				'language',
				'onLine',
				'platform',
				'product',
				'productSub',
				'userAgent',
				'vendor'
			],
			location: [
				'hash',
				'host',
				'hostname',
				'href',
				'origin',
				'pathname',
				'port',
				'protocol'
			]
		};

		var i;
		for (var key in extract) {
			if (extract.hasOwnProperty(key)) {
				info[key] = {};
				for (i = 0; i < extract[key].length; i++) {
					info[key][extract[key][i]] = window[key][extract[key][i]]
				}
			}
		}

		info.viewport = {
			width: window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth,
			height: window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight,
			pixelRatio: window.devicePixelRatio
		};

		return info;
	},

	clientInfoString: function(){
		var clientInfo = this.clientInfo();

		var subkey;
		var string = '';
		for (var key in clientInfo) {
			if (clientInfo.hasOwnProperty(key)) {
				string += key.toUpperCase() + '\n';
				for (subkey in clientInfo[key]) {
					if (clientInfo[key].hasOwnProperty(subkey)) {
						string += '- ' + subkey + ': "' + clientInfo[key][subkey] + '"\n';
					}
				}
				string += '\n';
			}
		}

		return string;
	}

};

window.onerror = function (message, source, lineno, colno, error) {
	ErrorLogging.notify(message, lineno, colno, source, error.stack);
	return true;
};
