"use strict";

exports.__esModule = true;
exports.default = void 0;
var _map2 = _interopRequireDefault(require("lodash/fp/map"));
var _getOr2 = _interopRequireDefault(require("lodash/fp/getOr"));
var _get2 = _interopRequireDefault(require("lodash/fp/get"));
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _novaIcons = require("@coorpacademy/nova-icons");
var _style = _interopRequireDefault(require("./style.css"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const mapWithIndex = _map2.default.convert({
  cap: false
});
// eslint-disable-next-line no-undef
const Hammer = typeof window !== 'undefined' ? require('hammerjs') : undefined;
class Slider extends _react.default.Component {
  constructor(props) {
    super(props);
    this.state = {
      currentSlide: 0
    };
    this.interval = 8000;
    this.handleNextSlide = this.handleNextSlide.bind(this);
    this.handlePreviousSlide = this.handlePreviousSlide.bind(this);
    this.autoPlay = this.autoPlay.bind(this);
    this.setSlider = this.setSlider.bind(this);
  }
  componentDidMount() {
    if (Hammer) {
      this.hammer = new Hammer(this.slider);
      this.hammer.on('swipeleft', this.handlePreviousSlide);
      this.hammer.on('swiperight', this.handleNextSlide);
    }
    this.autoPlay();
  }
  componentWillUnmount() {
    if (this.hammer) {
      this.hammer.stop();
      this.hammer.destroy();
    }
    this.hammer = null;
    clearTimeout(this.timer);
  }
  setSlider(el) {
    this.slider = el;
  }
  handleNextSlide() {
    this.setState(prevState => {
      const {
        children
      } = this.props;
      const totalSlide = children.length;
      const nextSlide = prevState.currentSlide + 1;
      return {
        currentSlide: nextSlide >= totalSlide ? 0 : nextSlide
      };
    });
    this.autoPlay();
  }
  handlePreviousSlide() {
    this.setState(prevState => {
      const {
        children
      } = this.props;
      const totalSlide = children.length;
      const nextSlide = prevState.currentSlide - 1;
      return {
        currentSlide: nextSlide < 0 ? totalSlide - 1 : nextSlide
      };
    });
    this.autoPlay();
  }
  autoPlay() {
    clearTimeout(this.timer);
    this.timer = setTimeout(() => {
      this.handleNextSlide();
      this.autoPlay();
    }, this.interval);
  }
  render() {
    const {
      currentSlide
    } = this.state;
    const {
      children
    } = this.props;
    const totalSlide = children.length;
    const myslides = mapWithIndex((child, index) => {
      const isActive = currentSlide === index;
      return /*#__PURE__*/_react.default.createElement("div", {
        key: index,
        className: isActive ? _style.default.activeSlide : _style.default.slide
      }, child);
    }, children);
    const currentSlide_ = (0, _get2.default)(currentSlide, children);
    const controlColor = (0, _getOr2.default)(false, 'light', currentSlide_) ? '#FFF' : '#000';
    const leftControl = totalSlide > 1 ? /*#__PURE__*/_react.default.createElement(_novaIcons.NovaCompositionNavigationArrowLeft, {
      className: _style.default.leftControl,
      style: {
        color: controlColor
      },
      onClick: this.handlePreviousSlide
    }) : null;
    const rightControl = totalSlide > 1 ? /*#__PURE__*/_react.default.createElement(_novaIcons.NovaCompositionNavigationArrowRight, {
      className: _style.default.rightControl,
      style: {
        color: controlColor
      },
      onClick: this.handleNextSlide
    }) : null;
    return /*#__PURE__*/_react.default.createElement("div", {
      className: _style.default.slidesWrapper,
      ref: this.setSlider
    }, myslides, leftControl, rightControl);
  }
}
Slider.propTypes = process.env.NODE_ENV !== "production" ? {
  children: _propTypes.default.arrayOf(_propTypes.default.node)
} : {};
var _default = exports.default = Slider;
//# sourceMappingURL=index.js.map