'use strict';

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

require('./_commonjsHelpers-06173234.js');
require('./_to-object-329a650b.js');
require('./es6.string.iterator-cc0bbaa4.js');
require('./_library-dd23b178.js');
require('./core.get-iterator-method-f62321d4.js');
require('./_object-pie-68c5bcbc.js');
require('./web.dom.iterable-a0e279c1.js');
var _typeof = require('./typeof-725500bc.js');
require('./_is-array-754e012d.js');
var React = require('react');
var React__default = _interopDefault(React);
require('prop-types');
require('react-dom');
var index$1 = require('./index-be0c82be.js');
var index$2 = require('./index-da796b8c.js');
var slicedToArray = require('./slicedToArray-8f385299.js');
var smoothscroll = require('./smoothscroll-95231a70.js');

/*
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 _useState = React.useState(0),
      _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 urlFor = function urlFor(source, client) {
    var builder = index$1.imageUrlBuilder(client);
    return builder.image(source).auto('format');
  };

  React.useEffect(function () {
    //IE11 Polyfill
    smoothscroll.smoothscroll.polyfill();
    setThumbSize();
    setTimeout(function () {
      adjustTileHeight();
    }, 300);

    window.addEventListener('resize', setThumbSize);
    window.addEventListener('scroll', adjustTileHeight);

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

  //Set tile height to avoid titles being cut off
  var adjustTileHeight = function adjustTileHeight() {
    var tileHeight = tileArray.current.map(function (tile) {
      return tile.scrollHeight;
    });
    console.log(tileArray.current);
    for (var ele in tileArray.current) {
      var eleHeight = tileHeight[ele] < 185 ? 185 : tileHeight[ele];
      tileArray.current[ele].style.height = eleHeight + 'px';
    }
  };

  var setThumbSize = function setThumbSize() {
    var thumbSize = void 0;
    var scrollerVisible = false;
    if (window.innerWidth > 1399) {
      thumbSize = Math.floor(viewableWindow.current.clientWidth / 3 - 40);
      if (props.dataset.length > 3) {
        scrollerVisible = true;
      }
    }
    if (window.innerWidth < 1399 && window.innerWidth > 592) {
      thumbSize = Math.floor(viewableWindow.current.clientWidth / 2 - 48);
      if (props.dataset.length > 2) {
        scrollerVisible = true;
      }
    }
    if (window.innerWidth < 592) {
      thumbSize = Math.floor(viewableWindow.current.clientWidth / 1 - 64);
      if (props.dataset.length > 1) {
        scrollerVisible = true;
      }
    }
    if (thumbSize !== tileSize) {
      setTileSize(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'
        });
      }
    }
  };

  var get2 = 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;

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

    var scrollDirection = void 0;

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

      if (scrollOffset + viewableWidth + 25 >= innerSlider.current.offsetWidth) {
        scrollDirection = 0;
      }
    }
    if (direction === 'prev') {
      scrollDirection = scrollOffset - numberOfSlidesToScroll;

      if (scrollOffset === 0) {
        if (window.innerWidth > 1399) {
          scrollDirection = innerSlider.current.offsetWidth - tileSize * 3 - 70;
        }
        if (window.innerWidth < 1399 && window.innerWidth > 592) {
          scrollDirection = innerSlider.current.offsetWidth - tileSize * 2 - 40;
        }
        if (window.innerWidth < 592) {
          scrollDirection = innerSlider.current.offsetWidth - tileSize * 1 - 24;
        }
      }
    }
    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.createElement(
    'div',
    { ref: viewableWindow, id: 'vid-slider', className: 'col' },
    React__default.createElement(
      'div',
      { className: 'row' },
      showScroller && showScroller && React__default.createElement(
        'div',
        {
          onClick: function onClick(e) {
            return scrollSlider(e, 'prev');
          },
          className: 'tile__slider-prev',
          style: { color: '#fff', background: 'var(--primary)', width: '35px', height: '35px', borderRadius: '50%', paddingLeft: '19px' } },
        React__default.createElement('i', { style: { border: 'solid #fff', borderWidth: '0 4px 4px 0' }, className: 'arrow left' })
      ),
      showScroller && showScroller && React__default.createElement(
        'div',
        {
          onClick: function onClick(e) {
            return scrollSlider(e, 'next');
          },
          className: 'tile__slider-next',
          style: { color: '#fff', background: 'var(--primary)', width: '35px', height: '35px', borderRadius: '50%', paddingRight: '19px' } },
        React__default.createElement('i', { style: { border: 'solid #fff', borderWidth: '0 4px 4px 0' }, className: 'arrow right' })
      ),
      React__default.createElement(
        'div',
        { ref: innerSlider, className: 'row__inner' },
        props.dataset && props.dataset.map(function (video, index) {
          // 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(video.thumbnail, props.client);
            } else if (_typeof._typeof(video.thumbnail == 'string')) {
              thumbnail = video.thumbnail;
            } else {
              thumbnail = 'https://img.youtube.com/vi/' + smoothscroll.getYoutubeId(video.url) + '/hqdefault.jpg';
            }

            return React__default.createElement(
              'div',
              { ref: function ref(ele) {
                  return tileArray.current[index] = ele;
                }, key: index, className: 'tile', style: { width: tileSize + 'px' } },
              current && current === url ? React__default.createElement(
                'div',
                { className: 'now-playing' },
                'Now Playing'
              ) : '',
              React__default.createElement(
                'a',
                { onClick: function onClick() {
                    return handleClick(url, index + 1);
                  } },
                React__default.createElement(
                  'div',
                  { className: 'tile__media' },
                  React__default.createElement(
                    index$2.LazyLoad,
                    null,
                    React__default.createElement('img', { className: 'tile__img', src: thumbnail, style: { width: tileSize + 'px' }, alt: '' })
                  )
                ),
                React__default.createElement(
                  'div',
                  { className: 'tile__details' },
                  React__default.createElement(
                    'div',
                    { className: 'tile__title' },
                    React__default.createElement(
                      'div',
                      { style: { color: variant }, className: 'tile__title_text' },
                      displayEpisodeNumber ? React__default.createElement(
                        'span',
                        null,
                        React__default.createElement(
                          'b',
                          null,
                          'EP. ',
                          index + 1,
                          ':'
                        ),
                        ' ',
                        video.title
                      ) : video.title
                    )
                  )
                ),
                React__default.createElement(
                  'div',
                  { style: { color: '#fff' }, className: 'tile__info' },
                  displayEpisodeNumber ? React__default.createElement(
                    'span',
                    null,
                    React__default.createElement(
                      'b',
                      null,
                      'EP. ',
                      index + 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(video.thumbnail, props.client);
            } else {
              _thumbnail = 'https://img.youtube.com/vi/' + smoothscroll.getYoutubeId(video.url) + '/hqdefault.jpg';
            }
            return React__default.createElement(
              'div',
              { ref: function ref(ele) {
                  return tileArray.current[index] = ele;
                }, key: index, className: 'tile', style: { width: tileSize + 'px' } },
              React__default.createElement(
                'a',
                { href: _url },
                React__default.createElement(
                  'div',
                  { className: 'tile__media' },
                  React__default.createElement(
                    index$2.LazyLoad,
                    null,
                    React__default.createElement('img', { className: 'tile__img', src: _thumbnail, alt: '', style: { width: tileSize + 'px' } })
                  )
                ),
                React__default.createElement(
                  'div',
                  { className: 'tile__details' },
                  React__default.createElement(
                    'div',
                    { className: 'tile__title' },
                    React__default.createElement(
                      'div',
                      { style: { color: '#fff' }, className: 'tile__title_text' },
                      displayEpisodeNumber ? React__default.createElement(
                        'span',
                        null,
                        React__default.createElement(
                          'b',
                          null,
                          'EP. ',
                          index + 1,
                          ':'
                        ),
                        ' ',
                        video.title
                      ) : video.title
                    )
                  )
                ),
                React__default.createElement(
                  'div',
                  { style: { color: variant }, className: 'tile__info' },
                  displayEpisodeNumber ? React__default.createElement(
                    'span',
                    null,
                    React__default.createElement(
                      'b',
                      null,
                      'EP. ',
                      index + 1,
                      ':'
                    ),
                    ' ',
                    video.title
                  ) : video.title
                )
              )
            );
          }
        })
      )
    ),
    React__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 1.5rem;\n          margin-bottom: 2rem;\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,\n        #vidHolder {\n          max-width: 805px;\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 a:hover {\n          color: #7f8c8d;\n          text-decoration: underline;\n        }\n        #vid-slider .contain {\n          width: 100%;\n        }\n        #vid-slider .row {\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: bold;\n          transition: opacity 0.25s;\n        }\n        #vid-slider .row__inner {\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: 25px 0 0 -0.25rem !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\n          margin: 0 12.5px;\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: 15px;\n        }\n        #vid-slider .tile__img {\n          height: 135px;\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        #vid-slider .tile__title {\n          position: absolute;\n          bottom: 0;\n          padding: 10px;\n          max-width: 250px;\n        }\n        #vid-slider .tile__title_text {\n          position: relative;\n          left: 0;\n          white-space: normal;\n        }\n\n        #vid-slider .tile__slider-prev,\n        #vid-slider .tile__slider-next {\n          width: 30px;\n          background: transparent;\n          border: none;\n          position: absolute;\n          top: 50%;\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            max-width: 620px;\n          }\n          #vid-slider .tile__img {\n            height: 145px;\n          }\n        }\n        @media screen and (max-width: 1200px) {\n          #vid-slider,\n          #vidHolder {\n            max-width: 580px;\n          }\n        }\n        @media screen and (max-width: 967px) {\n          #vid-slider {\n            max-width: 100%;\n          }\n          #vid-slider .tile__img {\n            height: 175px;\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            height: 118px;\n          }\n          #vid-slider .row__inner {\n            margin: 30px 0 60px;\n          }\n        }\n\n        @media screen and (max-width: 590px) {\n          #vid-slider .tile__img {\n            height: 195px;\n          }\n        }\n      '
    )
  );
};

module.exports = YoutubeGroup;
