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 _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 _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

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

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); }

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

import React from "react";
import { rangeRight } from "lodash";
import Dropdown from "../Dropdown/Dropdown";
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

var createMonths = function createMonths(minDate, maxDate) {
  var minMonth = minDate.slice(0, 3);
  var maxMonth = maxDate.slice(0, 3);
  var minYear = minDate.slice(-4);
  var maxYear = maxDate.slice(-4);
  var years = rangeRight(+minYear, +maxYear + 1);
  var minMonths = months.slice(months.indexOf(minMonth));
  var maxMonths = months.slice(0, months.indexOf(maxMonth) + 1);
  var monthOptions = years.map(function (year, index, arr) {
    var monthYears;

    if (index === 0) {
      monthYears = maxMonths.map(function (month) {
        return month + year;
      }).reverse();
    } else if (index === arr.length - 1) {
      monthYears = minMonths.map(function (month) {
        return month + year;
      }).reverse();
    } else {
      monthYears = months.map(function (month) {
        return month + year;
      }).reverse();
    }

    return monthYears;
  }).reduce(function (a, b) {
    return a.concat(b);
  }, []);
  return monthOptions;
};

var dateHelper = function dateHelper() {
  var meta = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  var dates = [];

  if (meta.dates && meta.dates.date_granularity !== null) {
    if (meta.dates.date_granularity.toLowerCase() === "year") {
      dates = rangeRight(+meta.dates.min_date, +meta.dates.max_date + 1).map(String);
    }

    if (meta.dates.date_granularity.toLowerCase() === "decade") {
      dates = rangeRight(+meta.dates.min_date, +meta.dates.max_date + 1, 10).map(String);
    }

    if (meta.dates.date_granularity.toLowerCase() === "month") {
      dates = createMonths(meta.dates.min_date, meta.dates.max_date);
    }

    return dates.map(function (date) {
      return {
        label: date,
        value: date
      };
    });
  }

  return [meta.dates ? {
    label: meta.dates.default_date_filter,
    value: meta.dates.default_date_filter
  } : {}];
};

var DateDropdown = function (_React$Component) {
  _inherits(DateDropdown, _React$Component);

  function DateDropdown(props) {
    var _this;

    _classCallCheck(this, DateDropdown);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(DateDropdown).call(this));

    _defineProperty(_assertThisInitialized(_this), "handleChange", function (date) {
      _this.setState({
        date: date
      });

      _this.props.fetchSlideByDate(_this.props.slide, date, _this.props.type);
    });

    _this.state = {
      date: props.selectedSlideData.slide_meta ? props.selectedSlideData.slide_meta.dates.default_date_filter : ""
    };
    _this.handleChange = _this.handleChange.bind(_assertThisInitialized(_this));
    return _this;
  }

  _createClass(DateDropdown, [{
    key: "render",
    value: function render() {
      var options = dateHelper(this.props.selectedSlideData.slide_meta);
      return React.createElement(Dropdown, {
        value: this.state.date,
        options: options,
        simpleValue: true,
        onChange: this.handleChange,
        placeholder: "Please select a date"
      });
    }
  }]);

  return DateDropdown;
}(React.Component);

export default DateDropdown;