import { _ as _slicedToArray } from './slicedToArray-6ebcbfea.js';
import React__default, { useRef, useState, useEffect } from 'react';
import { g as getYoutubeId } from './index-c7e2ac95.js';
import { L as LazyLoad } from './index-755f2cc2.js';
import { s as smoothscroll } from './smoothscroll-fa4e3f8c.js';
import urlFor from './urlFor.js';
import { m as main_43, a as main_49, b as main_42 } from './main-1894c974.js';

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

var SeriesSlider = function SeriesSlider(_ref) {
  var dataset = _ref.dataset,
      _ref$dark = _ref.dark,
      dark = _ref$dark === undefined ? false : _ref$dark,
      client = _ref.client,
      _ref$displayEpisodeNu = _ref.displayEpisodeNumber,
      displayEpisodeNumber = _ref$displayEpisodeNu === undefined ? false : _ref$displayEpisodeNu,
      _ref$nowPlaying = _ref.nowPlaying,
      nowPlaying = _ref$nowPlaying === undefined ? null : _ref$nowPlaying;

  // Props should be an array of objects containing Thumbnail, title, and URL
  if (!dataset) return null;
  dataset = dataset.sort(function (a, b) {
    return new Date(a.published) - new Date(b.published);
  });
  var innerSlider = useRef(null);
  var tileArray = useRef([]);
  var viewableWindow = useRef(null);
  var viewableArea = useRef(null);

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

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

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

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

  useEffect(function () {
    //IE11 Polyfill
    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 (dataset.length > 5) {
        scrollerVisible = true;
      }
    }
    if (viewableWindow.current.offsetWidth < 1399 && viewableWindow.current.offsetWidth > 992) {
      thumbSize = Math.floor((viewableArea.current.clientWidth - tileDividerWidth * 5) / 6);
      if (dataset.length > 5) {
        scrollerVisible = true;
      }
    }
    if (viewableWindow.current.offsetWidth < 992 && viewableWindow.current.offsetWidth > 768) {
      thumbSize = Math.floor((viewableArea.current.clientWidth - tileDividerWidth * 3) / 4);
      if (dataset.length > 4) {
        scrollerVisible = true;
      }
    }
    if (viewableWindow.current.offsetWidth < 768 && viewableWindow.current.offsetWidth > 604) {
      thumbSize = Math.floor((viewableArea.current.clientWidth - tileDividerWidth * 2) / 3);
      if (dataset.length > 3) {
        scrollerVisible = true;
      }
    }
    if (viewableWindow.current.offsetWidth < 604) {
      thumbSize = Math.floor((viewableArea.current.clientWidth - tileDividerWidth * 2) / 2);
      if (dataset.length > 3) {
        scrollerVisible = true;
      }
    }

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

    if (thumbSize !== tileSize) {
      if (thumbSize > 372) thumbSize = 372;
      setTileSize(thumbSize);
      setTileHeight(Math.round(0.578 * 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'
        });
      }
    }
  };
  //scrolls to active video
  //   useEffect(() => {
  //     let activeVideo = document.querySelector('.tile__media.active')
  //     if(activeVideo){
  //  setTimeout(() => {
  //      activeVideo.scrollIntoView({
  //        behavior: 'smooth',
  //        block: 'nearest',
  //        inline: 'nearest'
  //      })
  //  },1000)

  //     }
  //   },[])

  var variant = 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 + 8); //width of the tile plus margin

    var scrollDirection = void 0;

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

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

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

  return React__default.createElement(
    'div',
    { ref: viewableWindow, className: 'col vid-slider' },
    React__default.createElement(
      'div',
      { ref: viewableArea, className: 'row', style: { display: 'flex', alignItems: 'center' } },
      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' },
        dataset && dataset.map(function (article, index) {
          // If there is a current prop, means this will overlay a currently playing article
          // slider by itself
          var url = '/view/' + article.url.current;
          var thumbnail = void 0;
          if (article.thumbnail && article.thumbnail.asset) {
            thumbnail = urlFor({ source: article.thumbnail, client: client });
          } else {
            thumbnail = 'https://img.youtube.com/vi/' + getYoutubeId(article.url) + '/hqdefault.jpg';
          }
          return React__default.createElement(
            'div',
            { ref: function ref(ele) {
                return tileArray.current[index] = ele;
              }, key: index, style: { width: tileSize + 'px' }, className: 'tile' },
            React__default.createElement(
              'a',
              { href: url },
              React__default.createElement(
                'div',
                { className: nowPlaying == article.title ? 'tile__media active' : 'tile__media', style: { width: tileSize + 'px', height: tileHeight + 'px' } },
                React__default.createElement(
                  LazyLoad,
                  null,
                  React__default.createElement('img', { className: 'tile__img', src: thumbnail, style: { width: tileSize + 'px', height: tileHeight + 'px' }, alt: '' })
                ),
                nowPlaying == article.title && React__default.createElement(
                  'span',
                  { style: { position: 'absolute', padding: '0 .25rem', top: '0', right: '0', background: 'rgba(0,0,0,.75)', color: '#eee', fontWeight: 'normal', fontSize: '14px' } },
                  'Now Viewing'
                )
              ),
              React__default.createElement(
                'div',
                { style: { color: variant }, className: 'tile__title' },
                displayEpisodeNumber ? React__default.createElement(
                  'span',
                  null,
                  'EP. ',
                  index + 1,
                  ': ',
                  article.title
                ) : article.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 {\n          margin: 0 -14px;\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          max-width: 800px;\n          margin-left: auto;\n          margin-right: auto;\n        }\n        .vid-slider.col {\n          padding: 0 1.15rem 0 2.75rem;\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          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: ' + (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          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          border-radius: 3px;\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          box-shadow: 1px 2px 4px rgba(0, 0, 0, 0.5);\n          border-radius: 3px;\n        }\n\n        .vid-slider .tile__title {\n          white-space: normal !important;\n          max-width: ' + tileSize + 'px;\n          text-align: center;\n          color: ' + (dark ? 'white' : '#383838') + ';\n          opacity: 1;\n          font-size: ' + (main_42 ? '0.75rem' : '0.9rem') + ';\n          font-weight: normal;\n          transition: opacity 0.25s;\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          transform: translateY(-50%);\n          z-index: 99;\n          cursor: pointer;\n          color: ' + (dark ? 'white' : 'black') + ';\n          display: flex;\n          justify-content: center;\n          align-items: center;\n          font-size: 2rem;\n          margin-left: 10px;\n          margin-right: 10px;\n          box-shadow: 1px 3px 7px rgba(0, 0, 0, 0.5);\n        }\n        .vid-slider .tile__slider-prev {\n          left: -.9rem;\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          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 ' + (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        .vidHolder {\n          max-width: 805px;\n        }\n        @media screen and (max-width: 1399px) {\n          .vid-slider {\n            max-width: 615px;\n            margin: 0 auto;\n          }\n          .vid-slider .tile__img {\n          }\n        }\n        @media screen and (max-width: 1200px) {\n          .vid-slider {\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          }\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      '
    )
  );
};

export { SeriesSlider as S };
