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

exports.default = createPicker;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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 showHour = function showHour(format) {
    return format.toLowerCase().indexOf('h') >= 0;
};

var showMinute = function showMinute(format) {
    return format.indexOf('m') >= 0;
};

var showSecond = function showSecond(format) {
    return format.toLowerCase().indexOf('s') >= 0;
};

var formatOption = function formatOption(option) {
    return {
        value: option < 10 ? '0' + option : '' + option,
        disabled: false
    };
};

var layoutStyle = {
    fontSize: 0,
    listStyle: 'none',
    outline: 'none',
    position: 'relative',
    textAlign: 'left'
};

function createPicker(Header, Column) {
    var Picker = function (_Component) {
        _inherits(Picker, _Component);

        function Picker() {
            _classCallCheck(this, Picker);

            return _possibleConstructorReturn(this, (Picker.__proto__ || Object.getPrototypeOf(Picker)).apply(this, arguments));
        }

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

                var _props = this.props,
                    style = _props.style,
                    format = _props.format;

                var styleBody = _extends({
                    position: 'relative'
                }, (0, _utils.omit)(style, 'header', 'hours', 'minutes', 'seconds', 'entry'));

                return _react2.default.createElement(
                    'div',
                    {
                        className: 'time-picker',
                        style: layoutStyle,
                        tabIndex: '1',
                        onKeyDown: function onKeyDown(evt) {
                            var _props2 = _this2.props,
                                onKeyDown = _props2.onKeyDown,
                                onCollapsePanel = _props2.onCollapsePanel;

                            if (onKeyDown) {
                                onKeyDown(evt);
                            }
                            if (evt.keyCode === 13 || evt.keyCode === 27) {
                                onCollapsePanel();
                            }
                        }
                    },
                    this.renderHeader(),
                    _react2.default.createElement(
                        'div',
                        { className: 'body', style: styleBody },
                        showHour(format) ? this.renderHour() : null,
                        showMinute(format) ? this.renderMinute() : null,
                        showSecond(format) ? this.renderSecond() : null
                    )
                );
            }
        }, {
            key: 'renderHeader',
            value: function renderHeader() {
                var _props3 = this.props,
                    value = _props3.value,
                    style = _props3.style,
                    translations = _props3.translations,
                    defaultValue = _props3.defaultValue;
                var header = style.header;

                return _react2.default.createElement(Header, {
                    translations: translations,
                    value: value,
                    defaultValue: defaultValue,
                    style: header
                });
            }
        }, {
            key: 'renderHour',
            value: function renderHour() {
                var _this3 = this;

                var style = this.props.style;

                var hourOptions = (0, _utils.range)(24);
                return _react2.default.createElement(Column, {
                    style: hoursStyle(style),
                    options: hourOptions.map(formatOption),
                    selectedIndex: hourOptions.indexOf(hourValue(this.props)),
                    onChange: function onChange(itemValue) {
                        var _props4 = _this3.props,
                            value = _props4.value,
                            defaultValue = _props4.defaultValue,
                            onChange = _props4.onChange;

                        onChange((value || defaultValue).clone().hour(itemValue));
                    }
                });
            }
        }, {
            key: 'renderMinute',
            value: function renderMinute() {
                var _this4 = this;

                var style = this.props.style;

                var minuteOptions = (0, _utils.range)(60);
                return _react2.default.createElement(Column, {
                    style: minutesStyle(style),
                    options: minuteOptions.map(formatOption),
                    selectedIndex: minuteOptions.indexOf(minuteValue(this.props)),
                    onChange: function onChange(itemValue) {
                        var _props5 = _this4.props,
                            value = _props5.value,
                            defaultValue = _props5.defaultValue,
                            onChange = _props5.onChange;

                        onChange((value || defaultValue).clone().minute(itemValue));
                    }
                });
            }
        }, {
            key: 'renderSecond',
            value: function renderSecond() {
                var _this5 = this;

                var style = this.props.style;

                var secondOptions = (0, _utils.range)(60);

                return _react2.default.createElement(Column, {
                    style: secondsStyle(style),
                    options: secondOptions.map(formatOption),
                    selectedIndex: secondOptions.indexOf(secondValue(this.props)),
                    onChange: function onChange(itemValue) {
                        var _props6 = _this5.props,
                            value = _props6.value,
                            defaultValue = _props6.defaultValue,
                            onChange = _props6.onChange;

                        onChange((value || defaultValue).clone().second(itemValue));
                    }
                });
            }
        }]);

        return Picker;
    }(_react.Component);

    Picker.propTypes = {
        defaultValue: _react.PropTypes.object,
        value: _react.PropTypes.object,
        onChange: _react.PropTypes.func,
        onCollapsePanel: _react.PropTypes.func.isRequired
    };

    return Picker;
}

function hourValue(_ref) {
    var value = _ref.value,
        defaultValue = _ref.defaultValue;

    return (value || defaultValue).hour();
}

function hoursStyle(style) {
    return columnStyle(style, ':hours');
}

function minuteValue(_ref2) {
    var value = _ref2.value,
        defaultValue = _ref2.defaultValue;

    return (value || defaultValue).minute();
}

function minutesStyle(style) {
    return columnStyle(style, ':minutes');
}

function secondValue(_ref3) {
    var value = _ref3.value,
        defaultValue = _ref3.defaultValue;

    return (value || defaultValue).second();
}

function secondsStyle(style) {
    return columnStyle(style, ':seconds');
}

function columnStyle(style, columnName) {
    return (0, _utils.deepAssign)((0, _utils.omit)(style.column, ':hours', ':minutes', ':seconds'), style.column[columnName]);
}
//# sourceMappingURL=createPicker.js.map