'use strict';

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _semanticUiReact = require('semantic-ui-react');

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _moment = require('moment');

var _moment2 = _interopRequireDefault(_moment);

var _utils = require('../utils.js');

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

function Cell(props) {
  var active = props.active,
      disabled = props.disabled,
      className = props.className,
      children = props.children,
      onClick = props.onClick;

  var classes = (0, _classnames2.default)(className, 'suir-calendar', 'date');
  return _react2.default.createElement(
    _semanticUiReact.Table.Cell,
    {
      onClick: onClick,
      active: active,
      disabled: disabled,
      className: classes },
    children
  );
}

function Calendar(props) {
  var onDateClick = props.onDateClick,
      onNextBtnClick = props.onNextBtnClick,
      onPrevBtnClick = props.onPrevBtnClick,
      activeDate = props.activeDate,
      showedMonth = props.showedMonth,
      className = props.className;

  var data = (0, _utils.getArrayOfWeeks)(showedMonth);
  var _getBodyRow = function _getBodyRow(week, key) {
    var days = week.map(function (day) {
      var active = (0, _utils.compareDates)(day, activeDate);
      var disabled = !(0, _utils.isDayInMonth)(day, showedMonth);
      return _react2.default.createElement(
        Cell,
        {
          onClick: onDateClick.bind(null, day),
          active: active,
          disabled: disabled,
          key: day.format('DD-MM-YYYY') },
        day.format('D')
      );
    });
    return _react2.default.createElement(
      _semanticUiReact.Table.Row,
      { key: key },
      days
    );
  };

  var _getBody = function _getBody(weeks) {
    return weeks.map(function (week) {
      return _getBodyRow(week, week[0].format('YYYY-MM-DD'));
    });
  };

  var _getWeekDayHeaders = function _getWeekDayHeaders() {
    return (0, _utils.getWeekDays)().map(function (weekDay) {
      return _react2.default.createElement(
        _semanticUiReact.Table.HeaderCell,
        {
          key: weekDay,
          className: 'suir-calendar week-day',
          colSpan: '1' },
        weekDay
      );
    });
  };

  var classes = (0, _classnames2.default)(className);
  var cellClasses = (0, _classnames2.default)('suir-calendar', 'cell');
  var buttonClasses = (0, _classnames2.default)('suir-calendar', 'button');
  return _react2.default.createElement(
    _semanticUiReact.Table,
    { unstackable: true, celled: true, textAlign: 'center', className: classes },
    _react2.default.createElement(
      _semanticUiReact.Table.Header,
      null,
      _react2.default.createElement(
        _semanticUiReact.Table.Row,
        null,
        _react2.default.createElement(
          _semanticUiReact.Table.HeaderCell,
          { className: cellClasses, colSpan: '1' },
          _react2.default.createElement(_semanticUiReact.Icon, {
            className: buttonClasses,
            onClick: onPrevBtnClick,
            name: 'chevron left' })
        ),
        _react2.default.createElement(
          _semanticUiReact.Table.HeaderCell,
          { className: cellClasses, colSpan: '5' },
          showedMonth.format('MMMM YYYY')
        ),
        _react2.default.createElement(
          _semanticUiReact.Table.HeaderCell,
          { className: cellClasses, colSpan: '1' },
          _react2.default.createElement(_semanticUiReact.Icon, {
            className: buttonClasses,
            onClick: onNextBtnClick,
            name: 'chevron right' })
        )
      ),
      _react2.default.createElement(
        _semanticUiReact.Table.Row,
        null,
        _getWeekDayHeaders()
      )
    ),
    _react2.default.createElement(
      _semanticUiReact.Table.Body,
      null,
      _getBody(data)
    )
  );
}

Cell.propTypes = {
  active: _propTypes2.default.bool.isRequired,
  disabled: _propTypes2.default.bool.isRequired,
  className: _propTypes2.default.string,
  children: _propTypes2.default.node,
  onClick: _propTypes2.default.func
};

Calendar.propTypes = {
  /** (clickedDate) => { do something }
   * 
   * @param clickedDate `moment` instance
   */
  onDateClick: _propTypes2.default.func.isRequired,
  onNextBtnClick: _propTypes2.default.func.isRequired,
  onPrevBtnClick: _propTypes2.default.func.isRequired,
  /** Currently selected date */
  activeDate: _propTypes2.default.instanceOf(_moment2.default).isRequired,
  /** calendar shows month of this `moment` */
  showedMonth: _propTypes2.default.instanceOf(_moment2.default).isRequired,
  className: _propTypes2.default.string
};

exports.default = Calendar;
exports.Calendar = Calendar;