'use strict';

var slicedToArray = require('../slicedToArray-BcL7fKuL.js');
var index = require('../index-BskY8dVw.js');
var ramda = require('ramda');
var uaParserJs = require('ua-parser-js');
require('../unsupportedIterableToArray-BoHMiKNA.js');

var getUAParser = function getUAParser() {
  return new uaParserJs.UAParser(navigator.userAgent);
};
var parseBrowser = function parseBrowser() {
  var parser = getUAParser();
  var browser = parser.getBrowser();
  return {
    name: browser.name || "Unknown",
    version: browser.version || ""
  };
};
var parseOS = function parseOS() {
  var parser = getUAParser();
  var os = parser.getOS();
  return {
    os: os.name || "Unknown",
    osVersion: os.version || ""
  };
};
var getDeviceCategory = function getDeviceCategory() {
  var parser = getUAParser();
  var _parser$getDevice = parser.getDevice(),
    type = _parser$getDevice.type;
  return type || "desktop";
};
var hasWebGL = function hasWebGL() {
  try {
    var c = document.createElement("canvas");
    return !!(window.WebGLRenderingContext && (c.getContext("webgl") || c.getContext("experimental-webgl")));
  } catch (_unused) {
    return false;
  }
};
var cookiesEnabled = function cookiesEnabled() {
  try {
    document.cookie = "ctest=1; SameSite=Lax";
    var ok = document.cookie.includes("ctest=1");
    document.cookie = "ctest=; Max-Age=0; SameSite=Lax";
    return ok;
  } catch (_unused2) {
    return false;
  }
};
var getPermission = /*#__PURE__*/function () {
  var _ref = index._asyncToGenerator(/*#__PURE__*/index._regeneratorRuntime.mark(function _callee(name) {
    var s;
    return index._regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          if (navigator.permissions) {
            _context.next = 2;
            break;
          }
          return _context.abrupt("return", "unsupported");
        case 2:
          _context.prev = 2;
          _context.next = 5;
          return navigator.permissions.query({
            name: name
          });
        case 5:
          s = _context.sent;
          return _context.abrupt("return", s.state);
        case 9:
          _context.prev = 9;
          _context.t0 = _context["catch"](2);
          return _context.abrupt("return", "unsupported");
        case 12:
        case "end":
          return _context.stop();
      }
    }, _callee, null, [[2, 9]]);
  }));
  return function getPermission(_x) {
    return _ref.apply(this, arguments);
  };
}();
var getMediaPresence = /*#__PURE__*/function () {
  var _ref2 = index._asyncToGenerator(/*#__PURE__*/index._regeneratorRuntime.mark(function _callee2() {
    var _navigator$mediaDevic;
    var list;
    return index._regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) switch (_context2.prev = _context2.next) {
        case 0:
          if ((_navigator$mediaDevic = navigator.mediaDevices) !== null && _navigator$mediaDevic !== void 0 && _navigator$mediaDevic.enumerateDevices) {
            _context2.next = 2;
            break;
          }
          return _context2.abrupt("return", {
            hasMic: null,
            hasCamera: null
          });
        case 2:
          _context2.prev = 2;
          _context2.next = 5;
          return navigator.mediaDevices.enumerateDevices();
        case 5:
          list = _context2.sent;
          return _context2.abrupt("return", {
            hasMic: list.some(function (d) {
              return d.kind === "audioinput";
            }),
            hasCamera: list.some(function (d) {
              return d.kind === "videoinput";
            })
          });
        case 9:
          _context2.prev = 9;
          _context2.t0 = _context2["catch"](2);
          return _context2.abrupt("return", {
            hasMic: null,
            hasCamera: null
          });
        case 12:
        case "end":
          return _context2.stop();
      }
    }, _callee2, null, [[2, 9]]);
  }));
  return function getMediaPresence() {
    return _ref2.apply(this, arguments);
  };
}();
var IpInfo = /*#__PURE__*/function () {
  var _ref3 = index._asyncToGenerator(/*#__PURE__*/index._regeneratorRuntime.mark(function _callee3() {
    var url, response;
    return index._regeneratorRuntime.wrap(function _callee3$(_context3) {
      while (1) switch (_context3.prev = _context3.next) {
        case 0:
          url = "https://neeto-ip-lookup-web-production.neetodeployapp.com/location";
          _context3.prev = 1;
          _context3.next = 4;
          return fetch(url, {
            headers: {
              "X-ACCESS-TOKEN": globalProps.ipLookupAccessToken
            }
          });
        case 4:
          response = _context3.sent;
          _context3.next = 7;
          return response.json();
        case 7:
          return _context3.abrupt("return", _context3.sent);
        case 10:
          _context3.prev = 10;
          _context3.t0 = _context3["catch"](1);
          return _context3.abrupt("return", null);
        case 13:
        case "end":
          return _context3.stop();
      }
    }, _callee3, null, [[1, 10]]);
  }));
  return function IpInfo() {
    return _ref3.apply(this, arguments);
  };
}();

// ---------- Main Function ----------
var deviceInformation = /*#__PURE__*/function () {
  var _ref4 = index._asyncToGenerator(/*#__PURE__*/index._regeneratorRuntime.mark(function _callee4() {
    var _parseOS, os, osVersion, browser, category, connection, _yield$Promise$all, _yield$Promise$all2, micPerm, camPerm, geoPerm, notifPerm, mediaInfo;
    return index._regeneratorRuntime.wrap(function _callee4$(_context4) {
      while (1) switch (_context4.prev = _context4.next) {
        case 0:
          _parseOS = parseOS(), os = _parseOS.os, osVersion = _parseOS.osVersion;
          browser = parseBrowser();
          category = getDeviceCategory(); // @ts-ignore
          connection = navigator.connection || null;
          _context4.next = 6;
          return Promise.all([getPermission("microphone"), getPermission("camera"), getPermission("geolocation"), getPermission("notifications"), getMediaPresence()]);
        case 6:
          _yield$Promise$all = _context4.sent;
          _yield$Promise$all2 = slicedToArray._slicedToArray(_yield$Promise$all, 5);
          micPerm = _yield$Promise$all2[0];
          camPerm = _yield$Promise$all2[1];
          geoPerm = _yield$Promise$all2[2];
          notifPerm = _yield$Promise$all2[3];
          mediaInfo = _yield$Promise$all2[4];
          _context4.t0 = {
            type: category,
            os: os,
            osVersion: osVersion,
            screen: {
              width: screen.width,
              height: screen.height,
              pixelRatio: window.devicePixelRatio || 1,
              colorDepth: screen.colorDepth
            },
            viewport: {
              width: window.innerWidth,
              height: window.innerHeight
            },
            memoryGB: typeof navigator.deviceMemory === "number" ? navigator.deviceMemory : null
          };
          _context4.t1 = {
            name: browser.name,
            version: browser.version,
            userAgent: navigator.userAgent,
            javaScriptEnabled: true,
            cookiesEnabled: cookiesEnabled(),
            websockets: "WebSocket" in window,
            webgl: hasWebGL(),
            doNotTrack: navigator.doNotTrack || null,
            mimeTypes: navigator.mimeTypes ? ramda.pluck("type", Array.from(navigator.mimeTypes)) : [],
            network: connection ? {
              effectiveType: connection.effectiveType,
              downlink: connection.downlink,
              rtt: connection.rtt,
              saveData: connection.saveData
            } : null
          };
          _context4.t2 = {
            microphone: micPerm,
            camera: camPerm,
            geolocation: geoPerm,
            notifications: notifPerm,
            media: mediaInfo
          };
          _context4.t3 = {
            timezone: Intl.DateTimeFormat().resolvedOptions().timeZone || null,
            referrer: document.referrer || ""
          };
          _context4.next = 19;
          return IpInfo();
        case 19:
          _context4.t4 = _context4.sent;
          return _context4.abrupt("return", {
            device: _context4.t0,
            browser: _context4.t1,
            permissions: _context4.t2,
            meta: _context4.t3,
            serverDerived: _context4.t4
          });
        case 21:
        case "end":
          return _context4.stop();
      }
    }, _callee4);
  }));
  return function deviceInformation() {
    return _ref4.apply(this, arguments);
  };
}();

exports.deviceInformation = deviceInformation;
//# sourceMappingURL=deviceInformation.js.map
