import './_commonjsHelpers-0c4b6f40.js';
import './core.get-iterator-method-86d4db69.js';
import './_library-528f1934.js';
import './_iter-detect-130ee549.js';
import './web.dom.iterable-f8f967cd.js';
import React, { useState, useRef, useEffect } from 'react';
import { _ as _slicedToArray } from './slicedToArray-67a08c9c.js';
import { m as main_40 } from './main-43ca42fe.js';
import { a as _asyncToGenerator, r as regenerator } from './asyncToGenerator-7bf8bc4d.js';
import './iconBase-602d52fe.js';
import { F as FaWindowClose } from './index.esm-d1125763.js';

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 = _ref.right,
      _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 = useState(width),
      _useState2 = _slicedToArray(_useState, 2),
      currentWidth = _useState2[0],
      setWidth = _useState2[1];

  var _useState3 = useState(height),
      _useState4 = _slicedToArray(_useState3, 2),
      currentHeight = _useState4[0],
      setHeight = _useState4[1];

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

  var ustreamContainer = useRef(null);
  var clickableLink = 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();
  }
  useEffect(function () {
    if (!main_40) {
      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) {}
    }
  }, []);

  useEffect(function () {
    if (isMuted && !main_40) {
      var loadUstreamListener = function () {
        var _ref2 = _asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee() {
          var UstreamEmbed, viewer, reporting;
          return regenerator.wrap(function _callee$(_context) {
            while (1) {
              switch (_context.prev = _context.next) {
                case 0:
                  _context.next = 2;
                  return import('./ustream-api-4c4fe0ba.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_40) {
    return null;
  }

  return React.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.createElement(
      'a',
      { ref: clickableLink, target: '_blank', href: '' },
      React.createElement(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.createElement('img', { src: frameUrl, style: { width: '100%' } }),
      React.createElement(
        'div',
        { style: { position: 'absolute', top: '48px', left: '26px', width: '272px' } },
        React.createElement(
          'div',
          { style: { position: 'relative', width: '100%', paddingTop: '56.25%', overflow: 'hidden', borderRadius: '10px' } },
          React.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.memo(Ustream);

export default Ustream$1;
