import { jsx as ___EmotionJSX } from "@emotion/core";

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

import React from 'react';
import PropTypes from 'prop-types';
import Slider from 'react-slick';
import Arrow from './arrow';
import { OuterContainer, InnerContainer, Card } from './style';
var BASE_SLIDER_CONFIG = {
  focusOnSelect: true,
  pauseOnHover: true,
  slidesToScroll: 1,
  variableWidth: true
};

var Carousel =
/*#__PURE__*/
function (_React$Component) {
  _inherits(Carousel, _React$Component);

  function Carousel(props) {
    var _this;

    _classCallCheck(this, Carousel);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(Carousel).call(this, props));
    _this.slider = React.createRef();
    _this.timeoutId = null;
    _this.userHasInteracted = false;

    _this.updateLastIndex = function () {
      var _this$state = _this.state,
          lastIndex = _this$state.lastIndex,
          currentIndex = _this$state.currentIndex;

      var updatedLastIndex = _this.getLastIndex();

      if (lastIndex !== updatedLastIndex) {
        _this.setState({
          lastIndex: updatedLastIndex
        });
      }

      if (currentIndex > updatedLastIndex) {
        _this.slider.current.slickGoTo(updatedLastIndex);
      }
    };

    _this.onCardChange = function (current, nextIndex) {
      _this.timeoutId = null;

      _this.setState({
        currentIndex: nextIndex
      });
    };

    _this.onUserInteraction = function () {
      clearTimeout(_this.timeoutId);
      _this.userHasInteracted = true;

      _this.slider.current.slickPause();
    };

    _this.state = {
      currentIndex: 0,
      lastIndex: _this.getLastIndex()
    };
    return _this;
  }

  _createClass(Carousel, [{
    key: "componentDidUpdate",
    value: function componentDidUpdate() {
      if (this.shouldReplay) {
        this.replay();
      }

      this.updateLastIndex();
    }
  }, {
    key: "getLastIndex",
    value: function getLastIndex() {
      var _this$props = this.props,
          children = _this$props.children,
          numCardsVisible = _this$props.numCardsVisible;
      var numberSlides = children.length;
      return numberSlides - numCardsVisible;
    }
  }, {
    key: "getCarouselSettings",
    value: function getCarouselSettings() {
      var _this$state2 = this.state,
          currentIndex = _this$state2.currentIndex,
          lastIndex = _this$state2.lastIndex;
      var firstIndex = 0;
      var _this$props2 = this.props,
          autoplay = _this$props2.autoplay,
          autoplaySpeed = _this$props2.autoplaySpeed,
          centerMode = _this$props2.centerMode,
          hideArrows = _this$props2.hideArrows,
          hideDots = _this$props2.hideDots,
          infinite = _this$props2.infinite,
          numCardsVisible = _this$props2.numCardsVisible;
      var allowCenterMode = numCardsVisible > 1 || infinite;

      var settings = _objectSpread({}, BASE_SLIDER_CONFIG, {
        arrows: !hideArrows,
        autoplay: autoplay,
        autoplaySpeed: autoplaySpeed,
        centerMode: allowCenterMode ? false : centerMode,
        dots: !hideDots,
        infinite: infinite,
        slidesToShow: numCardsVisible,
        beforeChange: this.onCardChange,
        onSwipe: this.onUserInteraction,
        prevArrow: ___EmotionJSX(Arrow, {
          prev: true,
          disabled: currentIndex === firstIndex && !infinite,
          onUserInteraction: this.onUserInteraction
        }),
        nextArrow: ___EmotionJSX(Arrow, {
          next: true,
          disabled: currentIndex === lastIndex && !infinite,
          onUserInteraction: this.onUserInteraction
        })
      });

      return settings;
    }
  }, {
    key: "replay",
    value: function replay() {
      var _this2 = this;

      var autoplaySpeed = this.props.autoplaySpeed;
      var firstIndex = 0;
      var timeoutId = setTimeout(function () {
        return _this2.slider.current.slickGoTo(firstIndex);
      }, autoplaySpeed);
      this.timeoutId = timeoutId;
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props3 = this.props,
          children = _this$props3.children,
          carouselType = _this$props3.carouselType,
          numCardsVisible = _this$props3.numCardsVisible;
      var settings = this.getCarouselSettings();
      return ___EmotionJSX(OuterContainer, {
        numCardsVisible: numCardsVisible
      }, ___EmotionJSX(InnerContainer, {
        carouselType: carouselType,
        onClick: this.onUserInteraction
      }, ___EmotionJSX(Slider, _extends({
        ref: this.slider
      }, settings), children)));
    }
  }, {
    key: "shouldReplay",
    get: function get() {
      var _this$props4 = this.props,
          autoplay = _this$props4.autoplay,
          infinite = _this$props4.infinite;
      var _this$state3 = this.state,
          currentIndex = _this$state3.currentIndex,
          lastIndex = _this$state3.lastIndex;
      var onLastCard = currentIndex === lastIndex;
      return autoplay && !infinite && onLastCard && !this.userHasInteracted;
    }
  }]);

  return Carousel;
}(React.Component);

Carousel.Card = Card;
Carousel.propTypes = {
  autoplay: PropTypes.bool,
  autoplaySpeed: PropTypes.number,
  carouselType: PropTypes.oneOf(['primary', 'secondary']),
  centerMode: PropTypes.bool,
  children: PropTypes.arrayOf(PropTypes.node).isRequired,
  hideArrows: PropTypes.bool,
  hideDots: PropTypes.bool,
  infinite: PropTypes.bool,
  numCardsVisible: PropTypes.number.isRequired
};
Carousel.defaultProps = {
  autoplay: false,
  autoplaySpeed: 5000,
  carouselType: 'primary',
  centerMode: true,
  hideArrows: false,
  hideDots: false,
  infinite: false
};
export default Carousel;