import { _ as _asyncToGenerator, a as _regeneratorRuntime } from '../../index-CoBEoJgD.js';
import devicesApi from './devices.js';
import { registerServiceWorker, unsubscribeBrowserNotifications } from './registerServiceWorker.js';
import 'axios';

var createBrowserSubscription = /*#__PURE__*/function () {
  var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(subscription) {
    var payload;
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          if (!subscription) {
            _context.next = 13;
            break;
          }
          payload = {
            device: {
              subscription: subscription,
              platform: "browser"
            }
          };
          _context.prev = 2;
          _context.next = 5;
          return devicesApi.create(payload);
        case 5:
          localStorage.setItem("deviceId", JSON.stringify(subscription.toJSON().keys.auth));
          _context.next = 11;
          break;
        case 8:
          _context.prev = 8;
          _context.t0 = _context["catch"](2);
          logger.error("Device registration Failed: ", _context.t0);
        case 11:
          _context.next = 14;
          break;
        case 13:
          logger.error("Something went wrong while registering device for browser subscription");
        case 14:
        case "end":
          return _context.stop();
      }
    }, _callee, null, [[2, 8]]);
  }));
  return function createBrowserSubscription(_x) {
    return _ref.apply(this, arguments);
  };
}();
var destroyBrowserSubscription = /*#__PURE__*/function () {
  var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
    var deviceId;
    return _regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) switch (_context2.prev = _context2.next) {
        case 0:
          _context2.prev = 0;
          _context2.next = 3;
          return unsubscribeBrowserNotifications();
        case 3:
          deviceId = JSON.parse(localStorage.getItem("deviceId"));
          _context2.next = 6;
          return devicesApi.destroy(deviceId);
        case 6:
          localStorage.removeItem("deviceId");
          _context2.next = 12;
          break;
        case 9:
          _context2.prev = 9;
          _context2.t0 = _context2["catch"](0);
          logger.error("Something went wrong while unsubscribing browser notification:", _context2.t0);
        case 12:
        case "end":
          return _context2.stop();
      }
    }, _callee2, null, [[0, 9]]);
  }));
  return function destroyBrowserSubscription() {
    return _ref2.apply(this, arguments);
  };
}();
var registerBrowserNotifications = /*#__PURE__*/function () {
  var _ref3 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
    var vapidPublicKey,
      response,
      _args3 = arguments;
    return _regeneratorRuntime.wrap(function _callee3$(_context3) {
      while (1) switch (_context3.prev = _context3.next) {
        case 0:
          vapidPublicKey = _args3.length > 0 && _args3[0] !== undefined ? _args3[0] : process.env.VAPID_PUBLIC_KEY;
          _context3.next = 3;
          return registerServiceWorker(vapidPublicKey);
        case 3:
          response = _context3.sent;
          _context3.next = 6;
          return createBrowserSubscription(response);
        case 6:
        case "end":
          return _context3.stop();
      }
    }, _callee3);
  }));
  return function registerBrowserNotifications() {
    return _ref3.apply(this, arguments);
  };
}();

export { destroyBrowserSubscription, registerBrowserNotifications };
//# sourceMappingURL=pushHelper.js.map
