"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _browserDetection = require("./browser-detection");

var _uidCookieValue = require("./uid-cookie-value");

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

const encode = obj => {
  const str = JSON.stringify(obj);
  return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, (_, p1) => String.fromCharCode('0x' + p1)));
};

const send = (baseUrl, url, obj, debug = false) => {
  if (debug) {
    console.info('sending', obj);
  }

  let builtUrl = "".concat(baseUrl).concat(url, "?data=").concat(encode(obj));

  if (globalThis.navigator && globalThis.navigator.sendBeacon) {
    navigator.sendBeacon(builtUrl);
  }
};
/**
 * @param {string} baseUrl
 * @param {boolean} [debug=false] Default is `false`
 */


var _default = (baseUrl, debug = false) => {
  const envProps = (0, _browserDetection.getEnvironmentData)();
  const anonId = (0, _uidCookieValue.getOrCreateUid)();

  const engage = obj => send(baseUrl, '/engage/', obj, debug);

  const track = (event, props) => send(baseUrl, '/track/', {
    event,
    properties: _objectSpread({
      distinct_id: realId || anonId
    }, props)
  }, debug);

  let realId;
  return {
    identify: (_ref) => {
      let {
        id,
        name,
        email
      } = _ref,
          rest = _objectWithoutProperties(_ref, ["id", "name", "email"]);

      if (!realId) {
        realId = id;
        track('$identify', {
          $anon_distinct_id: anonId,
          $identified_id: id
        });
      }

      engage({
        $distinct_id: realId,
        $set: _objectSpread({
          $name: name,
          $email: email
        }, rest)
      });
    },
    track: (event, props) => track(event, _objectSpread({
      $current_url: window.location.href,
      $screen_height: window.innerHeight,
      $screen_width: window.innerWidth,
      $browser: envProps.browser,
      $browser_version: envProps.browserVersion,
      $os: envProps.os,
      $device: envProps.device,
      nativeAppVersion: envProps.nativeAppVersion,
      app_version: window.version || 0
    }, props))
  };
};

exports.default = _default;