'use strict';

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

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 _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _projectSimilarCard = require('kitten/components/cards/project-similar-card');

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 SimilarProjectsCard = exports.SimilarProjectsCard = function (_Component) {
  _inherits(SimilarProjectsCard, _Component);

  function SimilarProjectsCard() {
    _classCallCheck(this, SimilarProjectsCard);

    var _this = _possibleConstructorReturn(this, (SimilarProjectsCard.__proto__ || Object.getPrototypeOf(SimilarProjectsCard)).call(this));

    _this.state = {
      currentIndex: 0
    };
    return _this;
  }

  _createClass(SimilarProjectsCard, [{
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps(nextProps) {
      if (this.props.projects != nextProps.projects) {
        this.setState({ currentIndex: 0 });
      }
    }
  }, {
    key: 'curryHandleArrowClick',
    value: function curryHandleArrowClick(newCurrentIndex, callback) {
      var _this2 = this;

      return function () {
        _this2.setState({ currentIndex: newCurrentIndex });
        callback();
      };
    }
  }, {
    key: 'hasProjects',
    value: function hasProjects() {
      return this.props.projects.length > 0;
    }
  }, {
    key: 'isAtEnd',
    value: function isAtEnd() {
      if (!this.hasProjects()) return false;

      return this.state.currentIndex == this.props.projects.length - 1;
    }
  }, {
    key: 'isAtStart',
    value: function isAtStart() {
      if (!this.hasProjects()) return false;

      return this.state.currentIndex == 0;
    }
  }, {
    key: 'currentProject',
    value: function currentProject() {
      if (!this.hasProjects()) return;

      return this.props.projects[this.state.currentIndex];
    }
  }, {
    key: 'currentProjectProps',
    value: function currentProjectProps() {
      var project = this.currentProject();

      if (!project) return;

      var description = project.description,
          otherProjectProps = _objectWithoutProperties(project, ['description']);

      return _extends({}, otherProjectProps, {
        paragraph: description
      });
    }
  }, {
    key: 'currentStep',
    value: function currentStep() {
      if (!this.hasProjects()) return;

      return this.state.currentIndex + 1 + '/' + this.props.projects.length;
    }
  }, {
    key: 'render',
    value: function render() {
      var _props = this.props,
          projects = _props.projects,
          onLeftArrowClick = _props.onLeftArrowClick,
          onRightArrowClick = _props.onRightArrowClick,
          others = _objectWithoutProperties(_props, ['projects', 'onLeftArrowClick', 'onRightArrowClick']);

      var nextIndex = this.state.currentIndex + 1;
      var previousIndex = this.state.currentIndex - 1;

      return _react2.default.createElement(_projectSimilarCard.SimilarProjectCard, _extends({}, others, this.currentProjectProps(), {
        step: this.currentStep(),
        onLeftArrowClick: this.curryHandleArrowClick(previousIndex, this.props.onLeftArrowClick),
        onRightArrowClick: this.curryHandleArrowClick(nextIndex, this.props.onRightArrowClick),
        leftArrowDisabled: this.isAtStart(),
        rightArrowDisabled: this.isAtEnd()
      }));
    }
  }]);

  return SimilarProjectsCard;
}(_react.Component);

SimilarProjectsCard.defaultProps = {
  projects: [], // Check ProjectSimilarCard for project data format.
  onLeftArrowClick: function onLeftArrowClick() {},
  onRightArrowClick: function onRightArrowClick() {}
};