'use strict';

var asyncToGenerator = require('./asyncToGenerator-65df6bf8.js');
var slicedToArray = require('./slicedToArray-067dd3bc.js');
var React = require('react');
var index_esm = require('./index.esm-d51c9ac6.js');
var main = require('./main-ae8472f3.js');
require('./_commonjsHelpers-06173234.js');
require('./core.get-iterator-method-5643aa10.js');
require('./web.dom.iterable-d98303e0.js');
require('./_library-dd23b178.js');
require('./_set-species-0c7864a2.js');
require('./_iter-detect-b1df62f0.js');
require('./iconBase-be4097c0.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);

var _this = undefined;

var Ustream = function Ustream(_ref) {
  var _ref$width = _ref.width,
      width = _ref$width === undefined ? '320px' : _ref$width,
      _ref$height = _ref.height,
      height = _ref$height === undefined ? 'auto' : _ref$height,
      _ref$close = _ref.close,
      close = _ref$close === undefined ? false : _ref$close;
      _ref.right;
      var _ref$src = _ref.src,
      src = _ref$src === undefined ? 'https://video.ibm.com/embed/23627444?volume=0&autoplay=true' : _ref$src,
      _ref$logEvent = _ref.logEvent,
      logEvent = _ref$logEvent === undefined ? function () {} : _ref$logEvent;

  var _useState = React.useState(width),
      _useState2 = slicedToArray._slicedToArray(_useState, 2),
      currentWidth = _useState2[0];
      _useState2[1];

  var _useState3 = React.useState(height),
      _useState4 = slicedToArray._slicedToArray(_useState3, 2);
      _useState4[0];
      _useState4[1];

  var _useState5 = React.useState(true),
      _useState6 = slicedToArray._slicedToArray(_useState5, 2),
      isMuted = _useState6[0],
      setIsMuted = _useState6[1];

  var ustreamContainer = React.useRef(null);
  var clickableLink = React.useRef(null);
  var frameUrl = 'https://mjh-web.s3.amazonaws.com/_media/mwn_frame_v2.jpg';

  function handleClose(ev) {
    ev.preventDefault();
    ev.stopPropagation();
    ustreamContainer.current.remove();
  }
  React.useEffect(function () {
    if (!main.main_43) {
      try {
        var container = document.getElementById('ustream-container');
        var ustream = document.getElementById('ustream-iframe');
        ustream.setAttribute('src', src);
        var referrerUrl = window && window.location.hostname.replace('www.', '').replace('.com', '').replace('.net', '').replace('.vercel.app', '') || ';';
        var mwnUrl = 'https://www.medicalworldnews.com/?utm_source=' + referrerUrl + '&utm_medium=web&utm_campaign=mwn&ultv=true';
        clickableLink.current.href = mwnUrl;
        if (close) {
          var closeBtn = document.querySelector('.ustream-close');
          closeBtn.addEventListener('click', function () {
            container.remove();
          });
        }
      } catch (err) {}
    }
  }, []);

  React.useEffect(function () {
    if (isMuted && !main.main_43) {
      var loadUstreamListener = function () {
        var _ref2 = asyncToGenerator._asyncToGenerator( /*#__PURE__*/asyncToGenerator.regenerator.mark(function _callee() {
          var UstreamEmbed, viewer, reporting;
          return asyncToGenerator.regenerator.wrap(function _callee$(_context) {
            while (1) {
              switch (_context.prev = _context.next) {
                case 0:
                  _context.next = 2;
                  return Promise.resolve().then(function () { return require('./ustream-api-3819e4aa.js'); });

                case 2:
                  UstreamEmbed = _context.sent;
                  viewer = UstreamEmbed('ustream-iframe');
                  reporting = setInterval(function () {
                    viewer.getProperty('volume', function (content) {
                      if (content > 0) {
                        logEvent({ category: 'Ustream Unmute', action: 'Unmute Ustream Widget', label: 'Unmute Ustream Widget' });
                        setIsMuted(false);
                        clearInterval(reporting);
                      }
                    });
                  }, 2000);

                case 5:
                case 'end':
                  return _context.stop();
              }
            }
          }, _callee, _this);
        }));

        return function loadUstreamListener() {
          return _ref2.apply(this, arguments);
        };
      }();
      loadUstreamListener();
    }
  }, [isMuted]);
  //return nothing if mobile
  if (main.main_43) {
    return null;
  }

  return React__default["default"].createElement(
    'div',
    {
      id: 'ustream-container',
      ref: ustreamContainer,
      style: { width: '' + currentWidth, position: 'fixed', bottom: '2rem', left: '2rem', zIndex: '999999', boxShadow: '2px 4px 8px rgba(0,0,0,.35)', borderRadius: '5px', overflow: 'hidden' } },
    React__default["default"].createElement(
      'a',
      { ref: clickableLink, target: '_blank', href: '' },
      React__default["default"].createElement(index_esm.FaWindowClose, { style: { position: 'absolute', top: '0', right: '0', width: '22px', height: '20px', color: '#fff', background: '#000', padding: '2px' }, onClick: function onClick(e) {
          return handleClose(e);
        } }),
      React__default["default"].createElement('img', { src: frameUrl, style: { width: '100%' } }),
      React__default["default"].createElement(
        'div',
        { style: { position: 'absolute', top: '48px', left: '26px', width: '272px' } },
        React__default["default"].createElement(
          'div',
          { style: { position: 'relative', width: '100%', paddingTop: '56.25%', overflow: 'hidden', borderRadius: '10px' } },
          React__default["default"].createElement('iframe', {
            id: 'ustream-iframe',
            webkitallowfullscreen: 'true',
            allowFullScreen: true,
            frameBorder: 'no',
            style: { position: 'absolute', top: '0', bottom: '0', left: '0', right: '0', width: '100%', height: '100%', pointerEvents: 'none', borderRadius: '10px' }
          })
        )
      )
    )
  );
};

var Ustream$1 = React__default["default"].memo(Ustream);

module.exports = Ustream$1;
