'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 _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 _reactDom = require('react-dom');

var _reactDom2 = _interopRequireDefault(_reactDom);

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

var _Header = require('./Header');

var _Header2 = _interopRequireDefault(_Header);

var _body = require('./body');

var _body2 = _interopRequireDefault(_body);

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 KeyCode = {
    DOWN: 40,
    END: 35,
    ENTER: 13,
    HOME: 36,
    LEFT: 37,
    PAGE_DOWN: 33,
    PAGE_UP: 34,
    RIGHT: 39,
    UP: 38,
    ESC: 27
};

var layoutStyle = {
    position: 'relative',
    outline: 'none',
    width: '100%',
    minWidth: 200,
    listStyle: 'none',
    textAlign: 'left',
    lineHeight: '1.5'
};

var Panel = function (_PureComponent) {
    _inherits(Panel, _PureComponent);

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

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

        _this.handleKeyDown = _this.handleKeyDown.bind(_this);
        return _this;
    }

    _createClass(Panel, [{
        key: 'render',
        value: function render() {
            var style = this.props.style;


            var props = {
                autoFocus: true,
                tabIndex: "0",
                className: 'picker-day',
                style: (0, _utils.deepAssign)(layoutStyle, (0, _utils.omit)(style, 'body', 'header')),
                onKeyDown: this.handleKeyDown
            };

            return _react2.default.createElement(
                'div',
                props,
                _react2.default.createElement(_Header2.default, _extends({}, this.props, {
                    style: style.header
                })),
                _react2.default.createElement(_body2.default, _extends({}, this.props, {
                    style: style.body
                }))
            );
        }
    }, {
        key: 'handleKeyDown',
        value: function handleKeyDown(event) {
            var _this2 = this;

            // mac
            var ctrlKey = event.ctrlKey || event.metaKey;
            var keyCode = event.keyCode;
            var _props = this.props,
                shadowValue = _props.shadowValue,
                onCollapsePanel = _props.onCollapsePanel;


            var SC = function SC(v) {
                return _this2.props.onShadowChange(v);
            };

            switch (keyCode) {

                case KeyCode.DOWN:
                    event.preventDefault();
                    SC(shadowValue.clone().add(1, 'weeks'));
                    return 1;

                case KeyCode.UP:
                    event.preventDefault();
                    SC(shadowValue.clone().add(-1, 'weeks'));
                    return 1;

                case KeyCode.LEFT:
                    event.preventDefault();
                    SC(shadowValue.clone().add(-1, ctrlKey ? 'years' : 'days'));
                    return 1;

                case KeyCode.RIGHT:
                    event.preventDefault();
                    SC(shadowValue.clone().add(1, ctrlKey ? 'years' : 'days'));
                    return 1;

                case KeyCode.HOME:
                    event.preventDefault();
                    SC(shadowValue.clone().startOf('month'));
                    return 1;

                case KeyCode.END:
                    event.preventDefault();
                    SC(shadowValue.clone().endOf('month'));
                    return 1;

                case KeyCode.PAGE_DOWN:
                    event.preventDefault();
                    SC(shadowValue.clone().add(1, 'months'));
                    return 1;

                case KeyCode.PAGE_UP:
                    event.preventDefault();
                    SC(shadowValue.clone().add(-1, 'months'));
                    return 1;

                case KeyCode.ENTER:
                    event.preventDefault();
                    this.props.onChange(this.props.shadowValue);
                    return 1;

                case KeyCode.ESC:
                    event.preventDefault();
                    onCollapsePanel();
                    return 1;

                default:
                    //onKeyDown(event)
                    return 1;
            }
        }
    }, {
        key: 'componentDidMount',
        value: function componentDidMount() {
            _reactDom2.default.findDOMNode(this).focus();
        }
    }]);

    return Panel;
}(_react.PureComponent);

Panel.propTypes = {
    onCollapsePanel: _react.PropTypes.func.isRequired,
    maxDate: _react.PropTypes.object,
    minDate: _react.PropTypes.object,
    value: _react.PropTypes.object,
    style: _react.PropTypes.object,
    onChange: _react.PropTypes.func.isRequired,
    onShadowChange: _react.PropTypes.func.isRequired
};

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