'use strict';

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

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 _moment = require('moment');

var _moment2 = _interopRequireDefault(_moment);

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

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

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 ROW = 4;
var COL = 3;

function chooseMonth(month) {
    var next = this.state.value.clone();
    next.month(month);
    this.setAndSelectValue(next);
}

function getTodayTime(value) {
    var today = (0, _moment2.default)();
    today.locale(value.locale()).utcOffset(value.utcOffset());
    return today;
}

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

    function MonthTable(props) {
        _classCallCheck(this, MonthTable);

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

        _this.state = {
            value: props.shadowValue
        };
        return _this;
    }

    _createClass(MonthTable, [{
        key: 'render',
        value: function render() {
            var _this2 = this;

            var value = this.state.value;

            var today = getTodayTime(value);
            var currentMonth = value.month();
            var prefixCls = this.props.prefixCls;


            var monthsEls = months(value).map(function (month, index) {
                var tds = month.map(function (monthData) {
                    var disabled = isDisabled(value, _this2.props);
                    var style = getStyles();
                    var cellEl = _react2.default.createElement(
                        'a',
                        { className: prefixCls + '-month' },
                        monthData.content
                    );
                    var cellProps = {
                        role: "gridcell",
                        key: monthData.value,
                        onClick: disabled ? null : chooseMonth.bind(_this2, monthData.value),
                        title: monthData.title
                    };
                    return _react2.default.createElement(
                        'td',
                        cellProps,
                        cellEl
                    );
                });
                return _react2.default.createElement(
                    'tr',
                    { key: index, role: 'row' },
                    tds
                );
            });

            return _react2.default.createElement(
                'table',
                { className: prefixCls + '-table', cellSpacing: '0', role: 'grid' },
                _react2.default.createElement(
                    'tbody',
                    { className: prefixCls + '-tbody' },
                    monthsEls
                )
            );
        }
    }, {
        key: 'componentWillReceiveProps',
        value: function componentWillReceiveProps(nextProps) {
            if (!nextProps.shadowValue.isSame(this.state.value)) {
                this.setState({ value: nextProps.shadowValue });
            }
        }
    }, {
        key: 'setAndSelectValue',
        value: function setAndSelectValue(value) {
            var _this3 = this;

            this.setState({ value: value }, function () {
                _this3.props.onShadowValueChange(value);
                _this3.props.onChangeMode('day');
            });
        }
    }]);

    return MonthTable;
}(_react.Component);

function isDisabled(value, disabledDate) {
    return false;
    // if (disabledDate) {
    //     const testValue = value.clone()
    //     testValue.month(monthData.value);
    //     disabled = disabledDate(testValue);
    // }
}
function getStyles() {
    return {};
    // const classNameMap = {
    //     [`${prefixCls}-cell`]: 1,
    //     [`${prefixCls}-cell-disabled`]: disabled,
    //     [`${prefixCls}-selected-cell`]: monthData.value === currentMonth,
    //     [`${prefixCls}-current-cell`]: today.year() === value.year() &&
    //         monthData.value === today.month(),
    // };
}
function months(value) {
    var current = value.clone();
    var months = [];
    var localeData = value.localeData();
    var base = [];
    //TODO: refactoring is needed below
    (0, _utils.range)(ROW).map(function (rowIndex) {
        months[rowIndex] = [];

        // return range(COL).reduce( () => {}, months[rowIndex])
        (0, _utils.range)(COL).map(function (colIndex) {
            var index = COL * rowIndex + colIndex;
            current.month(index);
            months[rowIndex][colIndex] = {
                value: index,
                content: localeData.monthsShort(current),
                title: localeData.monthsShort(current)
            };
        });
    });
    return months;
}

MonthTable.defaultProps = {
    onSelect: function onSelect() {}
};

MonthTable.propTypes = {
    onSelect: _react.PropTypes.func,
    cellRender: _react.PropTypes.func,
    prefixCls: _react.PropTypes.string,
    value: _react.PropTypes.object
};

exports.default = MonthTable;

//
// class MonthTable extends Component {
//   constructor(props) {
//     super(props);
//
//     this.state = {
//       value: props.value,
//     };
//   }
//
//   componentWillReceiveProps(nextProps) {
//     if ('value' in nextProps) {
//       this.setState({
//         value: nextProps.value,
//       });
//     }
//   }
//
//   setAndSelectValue(value) {
//     this.setState({
//       value,
//     });
//     this.props.onSelect(value);
//   }
//
//   months() {
//     const value = this.state.value;
//     const current = value.clone();
//     const months = [];
//     const localeData = value.localeData();
//     let index = 0;
//     for (let rowIndex = 0; rowIndex < ROW; rowIndex++) {
//       months[rowIndex] = [];
//       for (let colIndex = 0; colIndex < COL; colIndex++) {
//         current.month(index);
//         const content = localeData.monthsShort(current);
//         months[rowIndex][colIndex] = {
//           value: index,
//           content,
//           title: content,
//         };
//         index++;
//       }
//     }
//     return months;
//   }
//
//   render() {
//     const props = this.props;
//     const value = this.state.value;
//     const today = getTodayTime(value);
//     const months = this.months();
//     const currentMonth = value.month();
//     const { prefixCls, locale, contentRender, cellRender } = props;
//     const monthsEls = months.map((month, index) => {
//       const tds = month.map(monthData => {
//         let disabled = false;
//         if (props.disabledDate) {
//           const testValue = value.clone();
//           testValue.month(monthData.value);
//           disabled = props.disabledDate(testValue);
//         }
//         const classNameMap = {
//           [`${prefixCls}-cell`]: 1,
//           [`${prefixCls}-cell-disabled`]: disabled,
//           [`${prefixCls}-selected-cell`]: monthData.value === currentMonth,
//           [`${prefixCls}-current-cell`]: today.year() === value.year() &&
//           monthData.value === today.month(),
//         };
//         let cellEl;
//         if (cellRender) {
//           const currentValue = value.clone();
//           currentValue.month(monthData.value);
//           cellEl = cellRender(currentValue, locale);
//         } else {
//           let content;
//           if (contentRender) {
//             const currentValue = value.clone();
//             currentValue.month(monthData.value);
//             content = contentRender(currentValue, locale);
//           } else {
//             content = monthData.content;
//           }
//           cellEl = (
//             <a className={`${prefixCls}-month`}>
//               {content}
//             </a>
//           );
//         }
//         return (
//           <td
//             role="gridcell"
//             key={monthData.value}
//             onClick={disabled ? null : chooseMonth.bind(this, monthData.value)}
//             title={monthData.title}
//             className={classnames(classNameMap)}
//           >
//             {cellEl}
//           </td>);
//       });
//       return (<tr key={index} role="row">{tds}</tr>);
//     });
//
//     return (
//       <table className={`${prefixCls}-table`} cellSpacing="0" role="grid">
//         <tbody className={`${prefixCls}-tbody`}>
//         {monthsEls}
//         </tbody>
//       </table>
//     );
//   }
// }
//
// MonthTable.defaultProps = {
//   onSelect: noop,
// };
// MonthTable.propTypes = {
//   onSelect: PropTypes.func,
//   cellRender: PropTypes.func,
//   prefixCls: PropTypes.string,
//   value: PropTypes.object,
// };
// export default MonthTable;
//# sourceMappingURL=Body.js.map