'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _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; };

var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _nukaCarousel = require('nuka-carousel');

var _nukaCarousel2 = _interopRequireDefault(_nukaCarousel);

var _SlideNext = require('./SlideNext');

var _SlideNext2 = _interopRequireDefault(_SlideNext);

var _SlidePrevious = require('./SlidePrevious');

var _SlidePrevious2 = _interopRequireDefault(_SlidePrevious);

var _BottomSlider = require('./BottomSlider');

var _BottomSlider2 = _interopRequireDefault(_BottomSlider);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

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

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

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

var propTypes = {
  /** @ignore */
  children: _propTypes2.default.node,
  /** @ignore */
  style: _propTypes2.default.object,
  SlideNextDecorator: _propTypes2.default.object,
  SlidePreviousDecorator: _propTypes2.default.object,
  BottomDecorator: _propTypes2.default.object,
  className: _propTypes2.default.string
};

var defaultProps = {
  children: null,
  style: null,
  numberSlides: 1,
  slidesToScroll: 1,
  slidesToShow: 1,
  cellSpacing: 20,
  SlideNextDecorator: {
    component: _SlideNext2.default,
    position: 'TopRight'
  },
  SlidePreviousDecorator: {
    component: _SlidePrevious2.default,
    position: 'TopLeft'
  },
  BottomDecorator: {
    component: _BottomSlider2.default,
    position: 'BottomCenter'
  },
  className: undefined
};

var Slideshow = function (_Component) {
  _inherits(Slideshow, _Component);

  function Slideshow() {
    var _ref;

    var _temp, _this, _ret;

    _classCallCheck(this, Slideshow);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Slideshow.__proto__ || Object.getPrototypeOf(Slideshow)).call.apply(_ref, [this].concat(args))), _this), _this.childRefs = [], _temp), _possibleConstructorReturn(_this, _ret);
  }

  _createClass(Slideshow, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      this.forceUpdate();
    }
  }, {
    key: 'componentDidUpdate',
    value: function componentDidUpdate() {
      // fix: height is now set to the tallest children and not the first one
      if (this.childRefs[0]) {
        for (var index = 0; index < this.childRefs.length; index += 1) {
          if (this.childRefs[index + 1]) {
            if (this.childRefs[0].parentNode.offsetHeight < this.childRefs[index + 1].parentNode.offsetHeight) {
              this.childRefs[0].style.height = this.childRefs[index + 1].parentNode.offsetHeight + 'px';
            }
          }
        }
      }
    }
  }, {
    key: 'render',
    value: function render() {
      var _this2 = this;

      var _props = this.props,
          SlideNextDecorator = _props.SlideNextDecorator,
          SlidePreviousDecorator = _props.SlidePreviousDecorator,
          BottomDecorator = _props.BottomDecorator,
          children = _props.children,
          style = _props.style,
          className = _props.className,
          rest = _objectWithoutProperties(_props, ['SlideNextDecorator', 'SlidePreviousDecorator', 'BottomDecorator', 'children', 'style', 'className']);

      var carouselDecorators = [SlidePreviousDecorator, SlideNextDecorator, BottomDecorator];
      var childrenWithProps = _react2.default.Children.map(this.props.children, function (child) {
        return _react2.default.createElement(
          'div',
          { ref: function ref(node) {
              return _this2.childRefs.push(node);
            } },
          child
        );
      });
      return _react2.default.createElement(
        'div',
        { itemScope: true },
        _react2.default.createElement(
          _nukaCarousel2.default,
          _extends({
            decorators: carouselDecorators,
            style: style || { paddingTop: '25px', paddingBottom: '25px' },
            ref: function ref(carousel) {
              _this2.carousel = carousel;
            },
            beforeSlide: this.beforeSlide,
            className: className
          }, rest),
          childrenWithProps
        )
      );
    }
  }]);

  return Slideshow;
}(_react.Component);

Slideshow.propTypes = propTypes;

Slideshow.defaultProps = defaultProps;

exports.default = Slideshow;
module.exports = exports['default'];