import { _ as _asyncToGenerator, a as _regeneratorRuntime } from '../../index-CoBEoJgD.js';

// THIS FILE HAS BEEN ADDED BY NEETO NOTIFICATIONS ENGINE
// AND IS USED FOR BROWSER NOTIFICATIONS
// CONTACT NEETO NOTIFICATIONS TEAM BEFORE MODIFYING THIS FILE

function urlB64ToUint8Array(base64String) {
  var padding = "=".repeat((4 - base64String.length % 4) % 4);
  var base64 = (base64String + padding).replace(/-/g, "+").replace(/_/g, "/");
  var rawData = window.atob(base64);
  var outputArray = new Uint8Array(rawData.length);
  for (var i = 0; i < rawData.length; ++i) {
    outputArray[i] = rawData.charCodeAt(i);
  }
  return outputArray;
}
var subscribeBrowserNotifications = /*#__PURE__*/function () {
  var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(vapidPublicKey) {
    var subscription, permission, sw;
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          subscription = {};
          _context.prev = 1;
          _context.next = 4;
          return Notification.requestPermission();
        case 4:
          permission = _context.sent;
          if (!(permission !== "denied")) {
            _context.next = 15;
            break;
          }
          _context.next = 8;
          return navigator.serviceWorker.register("/serviceworker.js");
        case 8:
          _context.next = 10;
          return navigator.serviceWorker.ready;
        case 10:
          sw = _context.sent;
          _context.next = 13;
          return sw.pushManager.subscribe({
            userVisibleOnly: true,
            applicationServerKey: urlB64ToUint8Array(vapidPublicKey)
          });
        case 13:
          subscription = _context.sent;
          return _context.abrupt("return", subscription);
        case 15:
          logger.error("Notification Permission Denied");
          _context.next = 21;
          break;
        case 18:
          _context.prev = 18;
          _context.t0 = _context["catch"](1);
          logger.error(_context.t0);
        case 21:
          return _context.abrupt("return", null);
        case 22:
        case "end":
          return _context.stop();
      }
    }, _callee, null, [[1, 18]]);
  }));
  return function subscribeBrowserNotifications(_x) {
    return _ref.apply(this, arguments);
  };
}();
var unsubscribeBrowserNotifications = /*#__PURE__*/function () {
  var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
    var sw, subscription, res;
    return _regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) switch (_context2.prev = _context2.next) {
        case 0:
          _context2.prev = 0;
          _context2.next = 3;
          return navigator.serviceWorker.ready;
        case 3:
          sw = _context2.sent;
          _context2.next = 6;
          return sw.pushManager.getSubscription();
        case 6:
          subscription = _context2.sent;
          if (!subscription) {
            _context2.next = 12;
            break;
          }
          _context2.next = 10;
          return subscription.unsubscribe();
        case 10:
          res = _context2.sent;
          logger.error(res);
        case 12:
          _context2.next = 17;
          break;
        case 14:
          _context2.prev = 14;
          _context2.t0 = _context2["catch"](0);
          logger.error(_context2.t0);
        case 17:
        case "end":
          return _context2.stop();
      }
    }, _callee2, null, [[0, 14]]);
  }));
  return function unsubscribeBrowserNotifications() {
    return _ref2.apply(this, arguments);
  };
}();
var registerServiceWorker = /*#__PURE__*/function () {
  var _ref3 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee3(vapidPublicKey) {
    var res;
    return _regeneratorRuntime.wrap(function _callee3$(_context3) {
      while (1) switch (_context3.prev = _context3.next) {
        case 0:
          if (!(navigator.serviceWorker && vapidPublicKey)) {
            _context3.next = 5;
            break;
          }
          _context3.next = 3;
          return subscribeBrowserNotifications(vapidPublicKey);
        case 3:
          res = _context3.sent;
          return _context3.abrupt("return", res);
        case 5:
          logger.error("Service worker is not supported in this Browser");
          return _context3.abrupt("return", null);
        case 7:
        case "end":
          return _context3.stop();
      }
    }, _callee3);
  }));
  return function registerServiceWorker(_x2) {
    return _ref3.apply(this, arguments);
  };
}();

export { registerServiceWorker, unsubscribeBrowserNotifications };
//# sourceMappingURL=registerServiceWorker.js.map
