'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 _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _moment = require('moment');

var _moment2 = _interopRequireDefault(_moment);

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

var _DayCell = require('./DayCell');

var _DayCell2 = _interopRequireDefault(_DayCell);

var _WeekdayCell = require('./WeekdayCell');

var _WeekdayCell2 = _interopRequireDefault(_WeekdayCell);

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

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

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

var DATE_ROW_COUNT = 6;
var DATE_COL_COUNT = 7;

var Body = function Body(props) {
    var shadowValue = props.shadowValue,
        style = props.style;


    var dateTable = dayValues(shadowValue);

    var bodyStyle = (0, _utils.omit)(style, 'weekdayCell', 'dayCell');
    var layoutStyle = {
        fontSize: 0,
        position: 'relative'
    };

    return _react2.default.createElement(
        'div',
        { className: 'day-picker-body', style: bodyStyle },
        _react2.default.createElement(
            'div',
            { className: 'header', style: layoutStyle },
            dayNames(shadowValue).map(function (_ref, index) {
                var name = _ref.name,
                    title = _ref.title;
                return _react2.default.createElement(_WeekdayCell2.default, {
                    key: index,
                    name: name,
                    title: title,
                    style: style.weekdayCell
                });
            })
        ),
        (0, _utils.range)(DATE_ROW_COUNT).map(function (indexWeek) {
            return _react2.default.createElement(
                'div',
                { key: indexWeek, className: 'row', style: layoutStyle },
                renderValues(dateTable, indexWeek, props)
            );
        })
    );
};

function renderValues(dateTable, rowNumber, props) {
    var shadowValue = props.shadowValue,
        minDate = props.minDate,
        maxDate = props.maxDate,
        style = props.style,
        value = props.value,
        rest = _objectWithoutProperties(props, ['shadowValue', 'minDate', 'maxDate', 'style', 'value']);

    return (0, _utils.range)(DATE_COL_COUNT).map(function (colNumber) {
        var currentCellNumber = rowNumber * DATE_COL_COUNT + colNumber;

        var cellValue = dateTable[currentCellNumber];
        var isDisabled = !isAllowedDate(cellValue, minDate, maxDate);
        return _react2.default.createElement(_DayCell2.default, _extends({}, rest, {
            widgetValue: value,
            cellValue: cellValue,
            shadowValue: shadowValue,
            isDisabled: isDisabled,
            style: style.dayCell,
            key: currentCellNumber
        }));
    });
}

function dayNames(shadowValue) {
    var locale = shadowValue.localeData();

    return (0, _utils.range)(DATE_COL_COUNT).reduce(function (result, nextDay) {
        var now = (0, _moment2.default)().day((locale.firstDayOfWeek() + nextDay) % DATE_COL_COUNT);

        return [].concat(_toConsumableArray(result), [{
            name: locale.weekdaysMin(now),
            title: locale.weekdays(now)
        }]);
    }, []);
}

function dayValues(shadowValue) {

    var firstDay = shadowValue.clone().date(1);
    var firstWeekday = firstDay.day();
    var carryoverDay = (firstWeekday + 7 - shadowValue.localeData().firstDayOfWeek()) % 7;
    // calculate last month
    var startDate = firstDay.clone().add(0 - carryoverDay, 'days');

    var howMuch = DATE_ROW_COUNT * DATE_COL_COUNT;
    return (0, _utils.range)(howMuch).map(function (delta) {
        return startDate.clone().add(delta, 'days');
    });
}

function isAllowedDate(cellValue, minDate, maxDate) {
    var isAfter = !minDate ? true : cellValue.isAfter(minDate);
    var isBefore = !maxDate ? true : cellValue.isBefore(maxDate);
    return isBefore && isAfter;
}

Body.propTypes = {
    shadowValue: _react.PropTypes.instanceOf(_moment2.default).isRequired
};

exports.default = Body;
//# sourceMappingURL=index.js.map