import { _ as _asyncToGenerator, r as regenerator } from './asyncToGenerator-958ef5fc.js';
import './_commonjsHelpers-0c4b6f40.js';
import './core.get-iterator-method-ea258bb1.js';
import './web.dom.iterable-ab4dea5c.js';
import './_library-528f1934.js';
import './_iter-detect-98db3b07.js';

require('babel-polyfill');

var Segment = {
  identify: function identify(eKey) {
    if (!eKey) {
      throw new Error('eKey is not present');
    }

    if (typeof analytics === 'undefined') {
      console.error('Segment not initialized', analytics);
      return;
    }

    var buff = Buffer.from(eKey, 'base64');
    var email = buff.toString('ascii');

    analytics.identify(eKey, {
      email: email
    });
  },
  identifyWithEmail: function identifyWithEmail(email) {
    if (typeof analytics === 'undefined') {
      console.error('Segment not initialized', analytics);
      return;
    }

    var buff = Buffer.from(email, 'ascii');
    var eKey = buff.toString('base64');

    analytics.identify(eKey, {
      email: email
    });
  },
  getURL: function getURL(url) {
    var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;

    if (!data || !data.needSegmentSupport) {
      return url;
    }

    if (typeof analytics === 'undefined') {
      console.error('segment not defined');
      return url;
    }

    if (!analytics || typeof analytics.user !== 'function') {
      console.error('segment not defined: user function');
      return url;
    }

    var user = analytics.user();
    if (!user) {
      console.log('segment user not defined');
      return url;
    }

    var anonymouseId = user.anonymousId();
    var eKey = user._getId();
    if (eKey) {
      url += (url.includes('?') ? '&' : '?') + 'ekey=' + eKey;
    } else {
      url += (url.includes('?') ? '&' : '?') + 'anonymousid=' + anonymouseId;
    }

    return url;
  },
  getIDs: function getIDs() {
    var _this = this;

    return _asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee() {
      return regenerator.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              return _context.abrupt('return', {});

            case 1:
            case 'end':
              return _context.stop();
          }
        }
      }, _callee, _this);
    }))();
  },
  getMeta: function getMeta(metaName) {
    var metas = document.getElementsByTagName('meta');
    for (var i = 0; i < metas.length; i++) {
      if (metas[i].getAttribute('name') === metaName) {
        var attributeContent = metas[i].getAttribute('content');
        return attributeContent.split(',');
      }
    }

    return '';
  },
  getOsano: function getOsano() {
    var osano = {};
    if (window && window.Osano && window.Osano.cm) {
      osano.analytics = window.Osano.cm.analytics;
      osano.locale = window.Osano.cm.locale;
      osano.marketing = window.Osano.cm.marketing;
      osano.mode = window.Osano.cm.mode;
      osano.optOut = window.Osano.cm.optOut;
      osano.personalization = window.Osano.cm.personalization;
    }
    return osano;
  },
  pageview: function pageview(domain) {
    var _this2 = this;

    var properties = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
    return _asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee2() {
      var urlParams, eKey, result, beam_id, session_id;
      return regenerator.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              if (!(typeof analytics === 'undefined')) {
                _context2.next = 3;
                break;
              }

              console.error('Segment not initialized', window);
              return _context2.abrupt('return');

            case 3:
              if (!(window && window.location && window.location.search)) {
                _context2.next = 9;
                break;
              }

              urlParams = new URLSearchParams(window.location.search);
              eKey = urlParams.get('eKey');

              if (!eKey) {
                _context2.next = 9;
                break;
              }

              _context2.next = 9;
              return _this2.identify(eKey);

            case 9:
              _context2.next = 11;
              return _this2.getIDs();

            case 11:
              result = _context2.sent;
              beam_id = result.beam_id ? result.beam_id : '';
              session_id = result.session_id ? result.session_id : '';

              analytics.track('pageviews', {
                beamid: beam_id,
                sessionid: session_id,
                category: properties.category ? properties.category : '',
                content_placement: properties.content_placement && properties.content_placement.length > 0 ? properties.content_placement : '',
                document_group: properties.document_group && properties.document_group.length > 0 ? properties.document_group : '',
                domain: domain,
                keywords: _this2.getMeta('keywords'),
                osano: _this2.getOsano()
              });

            case 15:
            case 'end':
              return _context2.stop();
          }
        }
      }, _callee2, _this2);
    }))();
  },
  report_video_event: function report_video_event(action, deviceType, duration, current, segmentData) {
    var videoType = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 'Youtube';
    var brightCovePlayerID = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : '';

    var _this3 = 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 _callee3() {
      var result, beam_id, session_id, domain, data;
      return regenerator.wrap(function _callee3$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              if (!(typeof analytics === 'undefined')) {
                _context3.next = 3;
                break;
              }

              console.error('Segment not initialized');
              return _context3.abrupt('return');

            case 3:
              _context3.next = 5;
              return _this3.getIDs();

            case 5:
              result = _context3.sent;
              beam_id = result.beam_id ? result.beam_id : '';
              session_id = result.session_id ? result.session_id : '';
              domain = typeof window !== 'undefined' && window.location ? window.location.hostname : '';
              data = {
                beamid: beam_id,
                sessionid: session_id,
                domain: domain,
                Devicetype: deviceType,
                videoid: brightCoveVideoID,
                title: '',
                category: '',
                videoplayer: videoType,
                totalduration: duration,
                currentduration: current,
                segment: segmentData,
                queue: Math.floor(duration / 10),
                brightCovePlayerID: brightCovePlayerID,
                brightCoveAccountID: brightCoveAccountID,
                keywords: _this3.getMeta('keywords'),
                osano: _this3.getOsano()
              };


              console.log('Segment ' + videoType, action, data);
              analytics.track(action, data);

            case 12:
            case 'end':
              return _context3.stop();
          }
        }
      }, _callee3, _this3);
    }))();
  },
  report_ad_event: function report_ad_event(action, deviceType, duration) {
    var adType = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'brightcove';
    var adId = arguments[4];

    var _this4 = this;

    var brightCovePlayerID = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : '';
    var brightCoveAccountID = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : '';
    return _asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee4() {
      var result, beam_id, session_id, domain, data;
      return regenerator.wrap(function _callee4$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              if (!(typeof analytics === 'undefined')) {
                _context4.next = 3;
                break;
              }

              console.error('Segment not initialized');
              return _context4.abrupt('return');

            case 3:
              _context4.next = 5;
              return _this4.getIDs();

            case 5:
              result = _context4.sent;
              beam_id = result.beam_id ? result.beam_id : '';
              session_id = result.session_id ? result.session_id : '';
              domain = typeof window !== 'undefined' && window.location ? window.location.hostname : '';
              data = {
                beamid: beam_id,
                sessionid: session_id,
                domain: domain,
                Devicetype: deviceType,
                title: '',
                category: '',
                adType: adType,
                totalduration: duration,
                adId: adId,
                brightCovePlayerID: brightCovePlayerID,
                brightCoveAccountID: brightCoveAccountID,
                keywords: _this4.getMeta('keywords'),
                osano: _this4.getOsano()
              };


              console.log('Segment ' + adType, action, data);
              analytics.track(action, data);

            case 12:
            case 'end':
              return _context4.stop();
          }
        }
      }, _callee4, _this4);
    }))();
  },
  adClicked: function adClicked(domain, advertiserid, campaignid, creativeid, itemid, size, isemptyline, sourceAgnosticCreativeId, sourceAgnosticlineId) {
    var _this5 = this;

    return _asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee5() {
      var result, beam_id, session_id;
      return regenerator.wrap(function _callee5$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              if (!(typeof analytics === 'undefined')) {
                _context5.next = 3;
                break;
              }

              console.error('Segment not initialized');
              return _context5.abrupt('return');

            case 3:
              _context5.next = 5;
              return _this5.getIDs();

            case 5:
              result = _context5.sent;
              beam_id = result.beam_id ? result.beam_id : '';
              session_id = result.session_id ? result.session_id : '';

              analytics.track('Ad Clicked', {
                beamid: beam_id,
                sessionid: session_id,
                advertiserid: advertiserid,
                campaignid: campaignid,
                creativeid: creativeid,
                itemid: itemid,
                size: size,
                isemptyline: isemptyline,
                sourceAgnosticCreativeId: sourceAgnosticCreativeId,
                sourceAgnosticlineId: sourceAgnosticlineId,
                domain: domain,
                keywords: _this5.getMeta('keywords'),
                osano: _this5.getOsano()
              });

            case 9:
            case 'end':
              return _context5.stop();
          }
        }
      }, _callee5, _this5);
    }))();
  },
  adViewed: function adViewed(domain, advertiserid, campaignid, creativeid, itemid, size, isemptyline, sourceAgnosticCreativeId, sourceAgnosticlineId) {
    var _this6 = this;

    return _asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee6() {
      var result, beam_id, session_id;
      return regenerator.wrap(function _callee6$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              if (!(typeof analytics === 'undefined')) {
                _context6.next = 3;
                break;
              }

              console.error('Segment not initialized');
              return _context6.abrupt('return');

            case 3:
              _context6.next = 5;
              return _this6.getIDs();

            case 5:
              result = _context6.sent;
              beam_id = result.beam_id ? result.beam_id : '';
              session_id = result.session_id ? result.session_id : '';

              analytics.track('Impression Viewed', {
                beamid: beam_id,
                sessionid: session_id,
                advertiserid: advertiserid,
                campaignid: campaignid,
                creativeid: creativeid,
                itemid: itemid,
                size: size,
                isemptyline: isemptyline,
                sourceAgnosticCreativeId: sourceAgnosticCreativeId,
                sourceAgnosticlineId: sourceAgnosticlineId,
                domain: domain,
                keywords: _this6.getMeta('keywords'),
                osano: _this6.getOsano()
              });

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

export { Segment as default };
