'use strict';

var slicedToArray = require('./slicedToArray-067dd3bc.js');
var React = require('react');
var index = require('./index-187c967e.js');
var index$1 = require('./index-65621b6c.js');
var smoothscroll = require('./smoothscroll-99df7f93.js');
var urlFor = require('./urlFor.js');
var main = require('./main-812888f1.js');
require('./_commonjsHelpers-06173234.js');
require('./web.dom.iterable-d98303e0.js');
require('./core.get-iterator-method-5643aa10.js');
require('react-dom');
require('prop-types');
require('./extends-be0d85bc.js');
require('./index-d48b231c.js');

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

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

/*
GROQ query -
    accepts an array of title, URL
`https://img.youtube.com/vi/${getYouTubeId(vid.url)}/hqdefault.jpg`
*/

var YoutubeGroup = function YoutubeGroup(props) {
  // Props should be an array of objects containing Thumbnail, title, and URL
  var innerSlider = React.useRef(null);
  var tileArray = React.useRef([]);
  var viewableWindow = React.useRef(null);
  var viewableArea = React.useRef(null);

  var _useState = React.useState(200),
      _useState2 = slicedToArray._slicedToArray(_useState, 2),
      tileSize = _useState2[0],
      setTileSize = _useState2[1];

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

  var _useState5 = React.useState(0),
      _useState6 = slicedToArray._slicedToArray(_useState5, 2),
      scrollPosition = _useState6[0],
      setScrollPosition = _useState6[1];

  var _useState7 = React.useState(0),
      _useState8 = slicedToArray._slicedToArray(_useState7, 2),
      tileHeight = _useState8[0],
      setTileHeight = _useState8[1];

  React.useEffect(function () {
    // IE11 Polyfill
    smoothscroll.smoothscroll.polyfill();
    if (viewableWindow.current) {
      setThumbSize();
    }

    window.addEventListener('resize', setThumbSize);

    return function () {
      window.removeEventListener('resize', setThumbSize);
    };
  }, []);

  var setThumbSize = function setThumbSize() {
    var thumbSize = void 0;
    var scrollerVisible = false;
    var tileDividerWidth = parseInt(getComputedStyle(document.documentElement).fontSize) / 2 || 8;

    if (viewableWindow.current.offsetWidth > 1399) {
      thumbSize = Math.floor((viewableArea.current.clientWidth - tileDividerWidth * 5) / 6);
      if (props.dataset.length > 5) {
        scrollerVisible = true;
      }
    }
    if (viewableWindow.current.offsetWidth < 1399 && viewableWindow.current.offsetWidth > 992) {
      thumbSize = Math.floor((viewableArea.current.clientWidth - tileDividerWidth * 5) / 6);
      if (props.dataset.length > 5) {
        scrollerVisible = true;
      }
    }
    if (viewableWindow.current.offsetWidth < 992 && viewableWindow.current.offsetWidth > 768) {
      thumbSize = Math.floor((viewableArea.current.clientWidth - tileDividerWidth * 3) / 4);
      if (props.dataset.length > 4) {
        scrollerVisible = true;
      }
    }
    if (viewableWindow.current.offsetWidth < 768 && viewableWindow.current.offsetWidth > 604) {
      thumbSize = Math.floor((viewableArea.current.clientWidth - tileDividerWidth * 2) / 3);
      if (props.dataset.length > 3) {
        scrollerVisible = true;
      }
    }
    if (viewableWindow.current.offsetWidth < 604) {
      thumbSize = Math.floor((viewableArea.current.clientWidth - tileDividerWidth * 2) / 3);
      if (props.dataset.length > 3) {
        scrollerVisible = true;
      }
    }

    if (main.main_43) {
      thumbSize = Math.floor((viewableArea.current.clientWidth - tileDividerWidth) / 2);
      if (props.dataset.length > 2) {
        scrollerVisible = true;
      }
    }
    if (main.main_49) {
      thumbSize = Math.floor((viewableArea.current.clientWidth - tileDividerWidth * 2) / 3);
      if (props.dataset.length > 3) {
        scrollerVisible = true;
      }
    }

    if (thumbSize !== tileSize) {
      if (thumbSize > 372) thumbSize = 372;
      setTileSize(thumbSize);
      setTileHeight(Math.round(0.5625 * thumbSize));
      setScrollPosition(0);
      setShowScroller(scrollerVisible);
      var videoRows = document.querySelectorAll('#vid-slider .row');
      for (var i = 0; i < videoRows.length; i++) {
        videoRows[i].scrollTo({
          left: 0,
          behavior: 'smooth'
        });
      }
    }
  };

  props.get2 ? '/' + props.get2 + '/' : '/view/';

  var variant = props.dark ? 'white' : '#383838';

  function scrollSlider(e, direction) {
    var slide = e.target.parentElement;
    var scrollOffset = scrollPosition;
    var viewableWidth = slide.clientWidth;
    var innerSliderScrollWidth = innerSlider.current.scrollWidth;

    // calculate viewable size / slide
    var numberOfSlidesToScroll = Math.floor(tileArray.current[0].clientWidth + 8); // width of the tile plus margin

    var scrollDirection = void 0;

    if (direction === 'next') {
      scrollDirection = scrollOffset + numberOfSlidesToScroll;

      if (scrollOffset + viewableWidth + 8 >= innerSliderScrollWidth) {
        scrollDirection = 0;
      }
    }
    if (direction === 'prev') {
      scrollDirection = scrollOffset - numberOfSlidesToScroll;
      if (scrollOffset <= 0) {
        if (viewableWindow.current.offsetWidth > 1399) {
          scrollDirection = innerSliderScrollWidth - numberOfSlidesToScroll * 5;
        }
        if (viewableWindow.current.offsetWidth < 1399 && innerSliderScrollWidth > 992) {
          scrollDirection = innerSliderScrollWidth - numberOfSlidesToScroll * 5;
        }
        if (viewableWindow.current.offsetWidth < 992 && innerSliderScrollWidth > 768) {
          scrollDirection = innerSliderScrollWidth - numberOfSlidesToScroll * 3;
        }
        if (viewableWindow.current.offsetWidth < 768 && innerSliderScrollWidth > 604) {
          scrollDirection = innerSliderScrollWidth - numberOfSlidesToScroll * 2;
        }
        if (viewableWindow.current.offsetWidth < 604) {
          scrollDirection = innerSliderScrollWidth - numberOfSlidesToScroll * 2;
        }
        if (main.main_49) {
          scrollDirection = innerSliderScrollWidth - numberOfSlidesToScroll * 2;
        }
        if (main.main_43) {
          scrollDirection = innerSliderScrollWidth - numberOfSlidesToScroll * 1;
        }
      }
    }
    slide.scrollTo({
      left: scrollDirection,
      behavior: 'smooth'
    });
    setScrollPosition(scrollDirection);
  }

  function handleClick(url, vidIndex) {
    props.setState(url, vidIndex);
  }

  var _props$current = props.current,
      current = _props$current === undefined ? '' : _props$current,
      _props$displayEpisode = props.displayEpisodeNumber,
      displayEpisodeNumber = _props$displayEpisode === undefined ? false : _props$displayEpisode;


  return React__default["default"].createElement(
    'div',
    { ref: viewableWindow, id: 'vid-slider', style: { maxWidth: '100%' }, className: 'col' },
    React__default["default"].createElement(
      'div',
      { ref: viewableArea, className: 'row', style: { display: 'flex', alignItems: 'center' } },
      showScroller && showScroller && React__default["default"].createElement(
        'div',
        {
          onClick: function onClick(e) {
            return scrollSlider(e, 'prev');
          },
          className: 'tile__slider-prev',
          style: {
            color: '#fff',
            width: '35px',
            height: '35px',
            borderRadius: '50%',
            paddingLeft: '19px'
          } },
        React__default["default"].createElement('i', { style: { border: 'solid #fff', borderWidth: '0 4px 4px 0', marginLeft: '3px' }, className: 'arrow left' })
      ),
      showScroller && showScroller && React__default["default"].createElement(
        'div',
        {
          onClick: function onClick(e) {
            return scrollSlider(e, 'next');
          },
          className: 'tile__slider-next',
          style: {
            color: '#fff',
            width: '35px',
            height: '35px',
            borderRadius: '50%',
            paddingRight: '19px'
          } },
        React__default["default"].createElement('i', { style: { border: 'solid #fff', borderWidth: '0 4px 4px 0', marginRight: '3px' }, className: 'arrow right' })
      ),
      React__default["default"].createElement(
        'div',
        { ref: innerSlider, className: 'row__inner' },
        props.dataset && props.dataset.map(function (video, index$2) {
          // If there is a current prop, means this will overlay a currently playing video
          if (current !== '') {
            var url = video.url;
            var thumbnail = void 0;
            if (video.thumbnail && video.thumbnail.asset) {
              thumbnail = urlFor({ source: video.thumbnail, client: props.client });
            } else if (typeof video.thumbnail === 'string') {
              thumbnail = video.thumbnail;
            } else {
              thumbnail = 'https://img.youtube.com/vi/' + index.getYoutubeId(video.url) + '/hqdefault.jpg';
            }

            return React__default["default"].createElement(
              'div',
              { ref: function ref(ele) {
                  return tileArray.current[index$2] = ele;
                }, key: index$2, className: 'tile', style: { flex: '0 0 ' + tileSize + 'px', width: tileSize + 'px' } },
              current && current === url ? React__default["default"].createElement(
                'div',
                { className: 'now-playing' },
                'Now Playing'
              ) : '',
              React__default["default"].createElement(
                'a',
                { onClick: function onClick() {
                    return handleClick(url, index$2 + 1);
                  } },
                React__default["default"].createElement(
                  'div',
                  { className: 'tile__media', style: { width: tileSize + 'px', height: '' + (tileHeight !== 0 ? tileHeight + 'px' : '100px') } },
                  React__default["default"].createElement(
                    index$1.LazyLoad,
                    null,
                    React__default["default"].createElement('img', { className: 'tile__img', style: { width: tileSize + 'px', height: '' + (tileHeight !== 0 ? tileHeight + 'px' : '100px') }, src: thumbnail, alt: '' })
                  ),
                  React__default["default"].createElement(
                    'noscript',
                    null,
                    React__default["default"].createElement('img', { className: 'tile__img', src: thumbnail, style: { width: tileSize + 'px', height: '' + (tileHeight !== 0 ? tileHeight + 'px' : '100%') }, alt: '' })
                  )
                ),
                React__default["default"].createElement(
                  'div',
                  { style: { color: variant }, className: 'tile__title' },
                  displayEpisodeNumber ? React__default["default"].createElement(
                    'span',
                    null,
                    React__default["default"].createElement(
                      'b',
                      { style: { marginLeft: '.25rem', display: 'inline-block' } },
                      'EP. ',
                      index$2 + 1,
                      ':'
                    ),
                    ' ',
                    video.title
                  ) : video.title
                )
              )
            );
          } else {
            // slider by itself
            var _url = '' + video.link;
            var _thumbnail = void 0;
            if (video.thumbnail && video.thumbnail.asset) {
              _thumbnail = urlFor({ source: video.thumbnail, client: props.client });
            } else {
              _thumbnail = 'https://img.youtube.com/vi/' + index.getYoutubeId(video.url) + '/hqdefault.jpg';
            }
            return React__default["default"].createElement(
              'div',
              { ref: function ref(ele) {
                  return tileArray.current[index$2] = ele;
                }, key: index$2, style: { flex: '0 0 ' + tileSize + 'px', width: tileSize + 'px' }, className: 'tile' },
              React__default["default"].createElement(
                'a',
                { href: _url },
                React__default["default"].createElement(
                  'div',
                  { className: 'tile__media', style: { width: tileSize + 'px', height: '' + (tileHeight !== 0 ? tileHeight + 'px' : '100%') } },
                  React__default["default"].createElement(
                    index$1.LazyLoad,
                    null,
                    React__default["default"].createElement('img', { className: 'tile__img', src: _thumbnail, style: { width: tileSize + 'px', height: tileHeight + 'px' }, alt: '' })
                  ),
                  React__default["default"].createElement(
                    'noscript',
                    null,
                    React__default["default"].createElement('img', { className: 'tile__img', src: _thumbnail, style: { width: tileSize + 'px', height: '' + (tileHeight !== 0 ? tileHeight + 'px' : '100%') }, alt: '' })
                  )
                ),
                React__default["default"].createElement(
                  'div',
                  { style: { color: variant }, className: 'tile__title' },
                  displayEpisodeNumber ? React__default["default"].createElement(
                    'span',
                    null,
                    React__default["default"].createElement(
                      'b',
                      { style: { marginLeft: '.25rem', display: 'inline-block' } },
                      'EP. ',
                      index$2 + 1,
                      ':'
                    ),
                    ' ',
                    video.title
                  ) : video.title
                )
              )
            );
          }
        })
      )
    ),
    React__default["default"].createElement(
      'noscript',
      null,
      React__default["default"].createElement(
        'style',
        { jsx: 'true' },
        '\n          #vid-slider .row__inner {\n            flex-wrap: wrap;\n            flex-basis: 100%;\n          }\n        '
      )
    ),
    React__default["default"].createElement(
      'style',
      { jsx: 'true' },
      '\n        #vid-slider * {\n          box-sizing: border-box;\n          -ms-overflow-style: none;\n        }\n        #vid-slider::-webkit-scrollbar-thumb {\n          display: none;\n        }\n        #vid-slider::-webkit-scrollbar {\n          display: none;\n        }\n        #vid-slider .row::-webkit-scrollbar {\n          display: none;\n        }\n        #vid-slider .row::-webkit-scrollbar-thumb {\n          display: none;\n        }\n        #vid-slider {\n          position: relative;\n          -ms-overflow-style: none;\n          padding: 0 2.5rem;\n          margin-bottom: 2rem;\n        }\n        #vid-slider.col {\n          padding: 0 2.5rem;\n        }\n        #vid-slider .now-playing {\n          position: absolute;\n          top: 0.5rem;\n          left: 1rem;\n          color: white;\n          background: rgba(0, 0, 0, 0.75);\n        }\n        #vid-slider h1,\n        #vid-slider p {\n          text-align: center;\n          width: 100%;\n          max-width: 500px;\n          margin: auto;\n        }\n        #vid-slider a:link,\n        #vid-slider a:hover,\n        #vid-slider a:active,\n        #vid-slider a:visited {\n          transition: color 150ms;\n          color: #95a5a6;\n          text-decoration: none;\n        }\n        #vid-slider .contain {\n          width: 100%;\n        }\n        #vid-slider .row {\n          flex-wrap: nowrap;\n          overflow-x: hidden;\n          width: 100%;\n          -ms-overflow-style: none;\n          overflow-y: hidden !important;\n        }\n        #vid-slider .tile__info {\n          white-space: normal !important;\n          position: absolute;\n          width: 100%;\n          text-align: center;\n          color: ' + (props.dark ? 'white' : '#383838') + ';\n          opacity: 1;\n          font-size: 0.9rem;\n          font-weight: normal;\n          transition: opacity 0.25s;\n        }\n        #vid-slider .row__inner {\n          display: flex;\n          transition: 450ms -webkit-transform;\n          transition: 450ms transform;\n          transition: 450ms transform, 450ms -webkit-transform;\n          font-size: 0;\n          white-space: nowrap;\n          margin-top: 1.5rem !important;\n          padding-bottom: 10px;\n          padding-left: 0;\n        }\n        #vid-slider .tile {\n          position: relative;\n          display: inline-block;\n          height: 100%;\n          margin-right: 0.5rem;\n          font-size: 20px;\n          cursor: pointer;\n          transition: 450ms all;\n          -webkit-transform-origin: center left;\n          transform-origin: center left;\n        }\n        #vid-slider .tile:first-child {\n          margin-left: 0;\n        }\n        #vid-slider .tile__img {\n          height: auto;\n\n          -o-object-fit: cover;\n          object-fit: cover;\n          object-position: center;\n        }\n        #vid-slider .tile__details {\n          position: absolute;\n          bottom: 0;\n          left: 0;\n          right: 0;\n          top: 0;\n          font-size: 10px;\n          opacity: 0;\n          background: linear-gradient(to top, rgba(0, 0, 0, 0.9) 0%, rgba(0, 0, 0, 0) 100%);\n          transition: 450ms opacity;\n        }\n        #vid-slider .tile__details:after,\n        #vid-slider .tile__details:before {\n          content: \'\';\n\n          position: absolute;\n          top: 50%;\n          left: 50%;\n          display: #000;\n        }\n        #vid-slider .tile__details:after {\n          margin-top: -25px;\n          margin-left: -25px;\n          width: 50px;\n          height: 50px;\n          border: 3px solid #ecf0f1;\n          line-height: 50px;\n          text-align: center;\n          border-radius: 100%;\n          background: rgba(0, 0, 0, 0.5);\n          z-index: 1;\n        }\n        #vid-slider .tile__details:before {\n          content: \'\';\n          background: url("data:image/svg+xml;utf8,<svg xmlns=\'http://www.w3.org/2000/svg\' viewBox=\'0 0 64 64\' width=\'64\' height=\'64\' fill=\'white\'><path d=\'M26 46 L46 32 L26 18 Z\'/></svg>")\n            no-repeat center center;\n          left: 0;\n          width: 100%;\n          height: 3rem;\n          font-size: 30px;\n          margin-right: 12px;\n          margin-top: -24px;\n          text-align: center;\n          z-index: 2;\n          color: white;\n        }\n\n        .tile__media {\n          overflow: hidden;\n          display: flex;\n          justify-content: center;\n          align-items: flex-start;\n        }\n\n        #vid-slider .tile__title {\n          white-space: normal !important;\n          max-width: ' + tileSize + 'px;\n          text-align: center;\n          color: ' + (props.dark ? 'white' : '#383838') + ';\n          opacity: 1;\n          font-size: ' + (main.main_42 ? '0.75rem' : '0.9rem') + ';\n          font-weight: normal;\n          transition: opacity 0.25s;\n          -webkit-font-smoothing: antialiased;\n        }\n\n        #vid-slider .tile__slider-prev,\n        #vid-slider .tile__slider-next {\n          width: 30px;\n          background: black;\n          background: var(--primary);\n          border: none;\n          position: absolute;\n          transform: translateY(-50%);\n          z-index: 99;\n          cursor: pointer;\n          color: ' + (props.dark ? 'white' : 'black') + ';\n          display: flex;\n          justify-content: center;\n          align-items: center;\n          font-size: 2rem;\n        }\n        #vid-slider .tile__slider-prev {\n          left: -0.5rem;\n          padding: 1rem;\n        }\n        #vid-slider .tile__slider-next {\n          right: -0.5rem;\n          padding: 1rem;\n        }\n        #vid-slider,\n        #vid-slider .row {\n          margin: 0;\n          padding-right: 0 1.2rem;\n        }\n\n        #vid-slider .row::-webkit-scrollbar-thumb {\n          background-color: transparent !important;\n        }\n        #vid-slider i {\n          border: solid ' + (props.dark ? 'white' : 'black') + ';\n          border-width: 0 4px 4px 0;\n          display: inline-block;\n          padding: 4px;\n          pointer-events: none;\n        }\n        #vid-slider .right {\n          transform: rotate(-45deg);\n          -webkit-transform: rotate(-45deg);\n        }\n        #vid-slider .left {\n          transform: rotate(135deg);\n          -webkit-transform: rotate(135deg);\n        }\n        @media screen and (max-width: 1399px) {\n          #vid-slider,\n          #vidHolder {\n          }\n          #vid-slider .tile__img {\n          }\n        }\n        @media screen and (max-width: 1200px) {\n          #vid-slider,\n          #vidHolder {\n          }\n        }\n        @media screen and (max-width: 967px) {\n          #vid-slider {\n            max-width: 100%;\n          }\n          #vid-slider .tile__img {\n          }\n        }\n\n        @media screen and (max-width: 768px) {\n          #vidHolder {\n            position: unset;\n            width: 100%;\n          }\n\n          #vid-slider .tile__img {\n          }\n          #vid-slider .row__inner {\n            margin-top: 1.5rem !important;\n          }\n        }\n\n        @media screen and (max-width: 590px) {\n          #vid-slider .tile__img {\n          }\n        }\n      '
    )
  );
};

module.exports = YoutubeGroup;
