"use strict";
var sServerApi = "https://pushdata.sendpulse.com:4434", gcmServer = "https://android.googleapis.com/gcm/send/", sFirefoxServer = "https://updates.push.services.mozilla.com/push/",
	sFirefoxServer2 = "https://updates.push.services.mozilla.com/wpush/v1/";

function endpointWorkaround(t) {
	return ~t.indexOf(gcmServer) ? t.split(gcmServer)[1] : ~t.indexOf(sFirefoxServer) ? t.split(sFirefoxServer)[1] : ~t.indexOf(sFirefoxServer2) ? t.split(sFirefoxServer2)[1] : t
}

self.addEventListener("push", function (t) {
	var c = !1;
	t.data && (c = t.data.json()), t.waitUntil(self.registration.pushManager.getSubscription().then(function (t) {
		var e = null;
		if (e = endpointWorkaround(e = "subscriptionId" in t ? t.subscriptionId : t.endpoint), c) {
			var i = Base64.decode(c.message.title), r = Base64.decode(c.message.message), o = c.message.icon, n = c.message.tag, a = Base64.decode(c.message.url),
				s = {action: "statisctic", subscriptionId: e, statisctic_action: "delivered", task_id: c.message.task_id, user_id: c.message.user_id};
			return fetch(sServerApi, {method: "post", headers: {"Content-Type": "application/json"}, body: JSON.stringify(s)}).then(function (t) {
				var e = {body: r, icon: o, tag: n, data: {redirectUrl: a}, requireInteraction: !0};
				return void 0 !== c.message.buttons && (e.actions = c.message.buttons, e.data.buttons = c.message.buttons), void 0 !== c.message.image && (e.image = c.message.image), self.registration.showNotification(i, e)
			})
		}
		return fetch(sServerApi + "/message/" + e).then(function (t) {
			if (200 !== t.status) throw new Error;
			return t.json().then(function (t) {
				if (t.error || !t.notification) throw new Error;
				if (void 0 !== t.notifications) {
					for (var e = !1, i = 0; i < t.notifications.length; i++) {
						var r = t.notifications[i].title, o = t.notifications[i].message, n = t.notifications[i].icon, a = t.notifications[i].tag, s = t.notifications[i].url;
						e = self.registration.showNotification(r, {body: o, icon: n, tag: a + i, data: {redirectUrl: s}, requireInteraction: !0})
					}
					return e
				}
				r = t.notification.title, o = t.notification.message, n = t.notification.icon, a = t.notification.tag, s = t.notification.url;
				return self.registration.showNotification(r, {body: o, icon: n, tag: a, data: {redirectUrl: s}, requireInteraction: !0})
			})
		})
	}))
}), self.addEventListener("notificationclick", function (t) {
	var r = t.notification.data.redirectUrl;
	if (void 0 !== t.action) {
		var e = t.action;
		if (void 0 !== t.notification.data.buttons) for (var i = 0; i < t.notification.data.buttons.length; i++) t.notification.data.buttons[i].action == e && (r = t.notification.data.buttons[i].url)
	}
	t.notification.close(), t.waitUntil(clients.matchAll({type: "window"}).then(function (t) {
		for (var e = 0; e < t.length; e++) {
			var i = t[e];
			if ("/" == i.url && "focus" in i) return i.focus()
		}
		if (clients.openWindow) return clients.openWindow(r)
	}))
});
var Base64 = {
	_keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", encode: function (t) {
		var e, i, r, o, n, a, s, c = "", d = 0;
		for (t = Base64._utf8_encode(t); d < t.length;) o = (e = t.charCodeAt(d++)) >> 2, n = (3 & e) << 4 | (i = t.charCodeAt(d++)) >> 4, a = (15 & i) << 2 | (r = t.charCodeAt(d++)) >> 6, s = 63 & r, isNaN(i) ? a = s = 64 : isNaN(r) && (s = 64), c = c + this._keyStr.charAt(o) + this._keyStr.charAt(n) + this._keyStr.charAt(a) + this._keyStr.charAt(s);
		return c
	}, decode: function (t) {
		var e, i, r, o, n, a, s = "", c = 0;
		for (t = t.replace(/[^A-Za-z0-9\+\/\=]/g, ""); c < t.length;) e = this._keyStr.indexOf(t.charAt(c++)) << 2 | (o = this._keyStr.indexOf(t.charAt(c++))) >> 4, i = (15 & o) << 4 | (n = this._keyStr.indexOf(t.charAt(c++))) >> 2, r = (3 & n) << 6 | (a = this._keyStr.indexOf(t.charAt(c++))), s += String.fromCharCode(e), 64 != n && (s += String.fromCharCode(i)), 64 != a && (s += String.fromCharCode(r));
		return s = Base64._utf8_decode(s)
	}, _utf8_encode: function (t) {
		t = t.replace(/\r\n/g, "\n");
		for (var e = "", i = 0; i < t.length; i++) {
			var r = t.charCodeAt(i);
			r < 128 ? e += String.fromCharCode(r) : (127 < r && r < 2048 ? e += String.fromCharCode(r >> 6 | 192) : (e += String.fromCharCode(r >> 12 | 224), e += String.fromCharCode(r >> 6 & 63 | 128)), e += String.fromCharCode(63 & r | 128))
		}
		return e
	}, _utf8_decode: function (t) {
		for (var e = "", i = 0, r = 0, o = 0, n = 0; i < t.length;) (r = t.charCodeAt(i)) < 128 ? (e += String.fromCharCode(r), i++) : 191 < r && r < 224 ? (o = t.charCodeAt(i + 1), e += String.fromCharCode((31 & r) << 6 | 63 & o), i += 2) : (o = t.charCodeAt(i + 1), n = t.charCodeAt(i + 2), e += String.fromCharCode((15 & r) << 12 | (63 & o) << 6 | 63 & n), i += 3);
		return e
	}
};