import { _ as _JSON$stringify } from './stringify-1e2a5946.js';
import { _ as _asyncToGenerator, r as regenerator } from './asyncToGenerator-1ebeb135.js';
import Segment from './Segment.js';
import './_commonjsHelpers-0c4b6f40.js';
import './core.get-iterator-method-95db387d.js';
import './web.dom.iterable-3fffc027.js';
import './_library-528f1934.js';
import './_set-species-7d3317b7.js';
import './_iter-detect-2f89237f.js';

require("babel-polyfill");
// server key for beam 
// fnADs4Gtx6ACEgKgwlNo-eNSIrdwykH9Zj74GsDI

var beam_domain = 'https://beam.mjhlifesciences.com';

function getCookie(cname) {
  var name = cname + "=";
  var decodedCookie = decodeURIComponent(document.cookie);
  var ca = decodedCookie.split(';');
  for (var i = 0; i < ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0) == ' ') {
      c = c.substring(1);
    }
    if (c.indexOf(name) == 0) {
      return c.substring(name.length, c.length);
    }
  }
  return "";
}

var beam = {
  proc_url: function proc_url() {
    var $_GET = {};
    document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function () {
      function decode(s) {
        return decodeURIComponent(s.split("+").join(" "));
      }
      $_GET[decode(arguments[1])] = decode(arguments[2]);
    });

    return $_GET;
  },
  proc_eKey: function proc_eKey($_GET) {
    var _this = this;

    return _asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee() {
      var eKey;
      return regenerator.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              eKey = "N/A";


              if ($_GET["eKey"]) {
                eKey = $_GET["eKey"];
              } else if (typeof window.eKey !== 'undefined') {
                eKey = window.eKey;
              }

              if (!eKey || eKey === 'N/A') {
                eKey = getCookie('eKey');
              }

              return _context.abrupt('return', eKey);

            case 4:
            case 'end':
              return _context.stop();
          }
        }
      }, _callee, _this);
    }))();
  },
  proc_source: function proc_source() {
    var referrer = "N/A";

    if (encodeURIComponent(document.referrer)) {
      referrer = encodeURIComponent(document.referrer);
    }

    return referrer;
  },
  proc_meta: function proc_meta() {
    var data = {};

    //keyword processing
    var element = document.querySelector('meta[name="keywords"]');

    data.keywords = "N/A";

    if (element && element.getAttribute("content")) {
      data.keywords = element.getAttribute("content");
    }

    //title processing
    data.title = "N/A";
    if (document.title !== "undefined") {
      data.title = document.title;
    }

    return data;
  },
  proc_host: function proc_host() {
    var data = {};
    data.domain = window.location.hostname;
    data.uri = window.location.pathname;
    data.host = window.location.host;
    data.protocol = window.location.protocol;

    if (window.location.search) {
      data.uri = data.uri + window.location.search;
    }

    return data;
  },
  get_beam: function get_beam(deviceType) {
    var _this2 = this;

    return _asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee2() {
      var beam;
      return regenerator.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              beam = {};
              _context2.next = 3;
              return _this2.proc_eKey(_this2.proc_url());

            case 3:
              beam.eKey = _context2.sent;

              beam.source = _this2.proc_source();
              beam.meta = _this2.proc_meta();
              beam.host = _this2.proc_host();
              beam.device = deviceType;
              return _context2.abrupt('return', beam);

            case 9:
            case 'end':
              return _context2.stop();
          }
        }
      }, _callee2, _this2);
    }))();
  },


  /* for client use */
  report_beam_event: function report_beam_event(action, deviceType) {
    var _this3 = this;

    return _asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee3() {
      var beam, response, res;
      return regenerator.wrap(function _callee3$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              _context3.next = 2;
              return _this3.get_beam(deviceType);

            case 2:
              beam = _context3.sent;


              console.log('Call Beam', action, beam);

              _context3.next = 6;
              return fetch(beam_domain + '/api/beam/event', {
                headers: {
                  'Content-Type': 'application/json',
                  'Accept': 'application/json'
                },
                method: 'post',
                credentials: "include",
                body: _JSON$stringify({
                  event: 'Beam',
                  action: action,
                  beam: _JSON$stringify(beam)
                })
              });

            case 6:
              response = _context3.sent;
              _context3.next = 9;
              return response.json();

            case 9:
              res = _context3.sent;

              if (res.error) {
                console.error('Beam reporting error:', res.error);
              }

              // report segment when eKey is present 
              if (beam.eKey) {
                Segment.identify(beam.eKey);
              }

            case 12:
            case 'end':
              return _context3.stop();
          }
        }
      }, _callee3, _this3);
    }))();
  },
  report_beam_ad: function report_beam_ad(action, deviceType, cID, lID, campID, adSize, slotId) {
    var adType = arguments.length > 7 && arguments[7] !== undefined ? arguments[7] : 'DFP';

    var _this4 = this;

    var adDurationBrightCove = arguments.length > 8 && arguments[8] !== undefined ? arguments[8] : '';
    var adIDBrightCove = arguments.length > 9 && arguments[9] !== undefined ? arguments[9] : '';
    return _asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee4() {
      var beam, data, response, res;
      return regenerator.wrap(function _callee4$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              _context4.next = 2;
              return _this4.get_beam(deviceType);

            case 2:
              beam = _context4.sent;
              data = {};

              data.campaignId = campID;
              data.lineItemId = lID;
              data.creativeId = cID;
              data.size = adSize;
              data.slotId = slotId;

              data.adType = adType;
              data.adDurationBrightCove = adDurationBrightCove;
              data.adIDBrightCove = adIDBrightCove;

              console.log('Call ' + adType, action, data);

              _context4.next = 15;
              return fetch(beam_domain + '/api/beam/ad', {
                headers: {
                  'Content-Type': 'application/json',
                  'Accept': 'application/json'
                },
                method: 'post',
                credentials: "include",
                body: _JSON$stringify({
                  event: 'DFP',
                  action: action,
                  beam: _JSON$stringify(beam),
                  data: _JSON$stringify(data)
                })
              });

            case 15:
              response = _context4.sent;
              _context4.next = 18;
              return response.json();

            case 18:
              res = _context4.sent;

              if (res.error) {
                console.error('DFP reporting error:', res.error);
              }

            case 20:
            case 'end':
              return _context4.stop();
          }
        }
      }, _callee4, _this4);
    }))();
  },
  report_lasso: function report_lasso(domain, url, action, mjhPubID, mcode, mid) {
    var _this5 = this;

    return _asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee5() {
      var data, response, res;
      return regenerator.wrap(function _callee5$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              data = {};

              data.mjhPubID = mjhPubID;
              data.mcode = mcode;
              data.mid = mid;

              console.log('Call Lasso', action, data);

              _context5.next = 7;
              return fetch(beam_domain + '/api/beam/lasso', {
                headers: {
                  'Content-Type': 'application/json',
                  'Accept': 'application/json'
                },
                method: 'post',
                credentials: "include",
                body: _JSON$stringify({
                  event: 'Lasso',
                  action: action,
                  domain: domain,
                  url: url,
                  data: _JSON$stringify(data)
                })
              });

            case 7:
              response = _context5.sent;
              _context5.next = 10;
              return response.json();

            case 10:
              res = _context5.sent;

              if (res.error) {
                console.error('Lasso reporting error:', res.error);
              }

            case 12:
            case 'end':
              return _context5.stop();
          }
        }
      }, _callee5, _this5);
    }))();
  },
  report_beam_video: function report_beam_video(action, deviceType, duration, current, segment) {
    var videoType = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 'Youtube';
    var brightCovePlayerID = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : '';

    var _this6 = this;

    var brightCoveAccountID = arguments.length > 7 && arguments[7] !== undefined ? arguments[7] : '';
    var brightCoveVideoID = arguments.length > 8 && arguments[8] !== undefined ? arguments[8] : '';
    return _asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee6() {
      var beam, data, response, res;
      return regenerator.wrap(function _callee6$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              _context6.next = 2;
              return _this6.get_beam(deviceType);

            case 2:
              beam = _context6.sent;
              data = {};

              data.duration = duration;
              data.queue = Math.floor(duration / 10);
              data.segment = segment;
              data.current = current;

              data.videoType = videoType;
              data.brightCovePlayerID = brightCovePlayerID;
              data.brightCoveAccountID = brightCoveAccountID;
              data.brightCoveVideoID = brightCoveVideoID;

              console.log('Call ' + videoType, action, data);

              _context6.next = 15;
              return fetch(beam_domain + '/api/beam/video', {
                headers: {
                  'Content-Type': 'application/json',
                  'Accept': 'application/json'
                },
                method: 'post',
                credentials: "include",
                body: _JSON$stringify({
                  event: 'Video',
                  action: action,
                  beam: _JSON$stringify(beam),
                  data: _JSON$stringify(data)
                })
              });

            case 15:
              response = _context6.sent;
              _context6.next = 18;
              return response.json();

            case 18:
              res = _context6.sent;

              if (res.error) {
                console.error('Video reporting error:', res.error);
              }

            case 20:
            case 'end':
              return _context6.stop();
          }
        }
      }, _callee6, _this6);
    }))();
  }
};

export { beam as default };
