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

var _moment2 = _interopRequireDefault(_moment);

var _hover = require('../_hoc/hover');

var _hover2 = _interopRequireDefault(_hover);

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

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

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 Input = function (_PureComponent) {
    _inherits(Input, _PureComponent);

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

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

        var value = props.value,
            format = props.format;

        _this.state = {
            stringValue: value && value.format(format) || '',
            invalid: false
        };
        _this.handleKeyDown = _this.handleKeyDown.bind(_this);
        _this.handleChange = _this.handleChange.bind(_this);
        return _this;
    }

    _createClass(Input, [{
        key: 'render',
        value: function render() {
            var _props = this.props,
                withClear = _props.withClear,
                isExpanded = _props.isExpanded,
                _props$style = _props.style,
                expanded = _props$style[':expanded'],
                input = _props$style.input,
                clear = _props$style.clear,
                rest = _objectWithoutProperties(_props$style, [':expanded', 'input', 'clear']);

            var withClearButton = withClear && Boolean(this.props.value);

            return _react2.default.createElement(
                'div',
                { className: 'field', style: _extends({
                        position: 'relative'
                    }, rest, isExpanded ? expanded : {}) },
                this.renderInput(),
                withClearButton ? this.renderClearButton() : null
            );
        }
    }, {
        key: 'renderInput',
        value: function renderInput() {
            var _this2 = this;

            var _props2 = this.props,
                placeholder = _props2.placeholder,
                style = _props2.style,
                onFocus = _props2.onFocus,
                onBlur = _props2.onBlur;
            var _state = this.state,
                invalid = _state.invalid,
                stringValue = _state.stringValue;

            var _style$input = style.input,
                _style$input$Invalid = _style$input[':invalid'],
                invalidStyle = _style$input$Invalid === undefined ? {} : _style$input$Invalid,
                rest = _objectWithoutProperties(_style$input, [':invalid']);

            return _react2.default.createElement('input', {
                className: 'date-picker-input',
                style: _extends({}, rest, invalid ? invalidStyle : {}),
                ref: function ref(_ref) {
                    return _this2.input = _ref;
                },
                value: stringValue,
                placeholder: placeholder,
                onKeyDown: this.handleKeyDown,
                onChange: this.handleChange,
                onBlur: onBlur,
                onFocus: onFocus
            });
        }
    }, {
        key: 'handleChange',
        value: function handleChange(_ref2) {
            var target = _ref2.target;
            var _props3 = this.props,
                format = _props3.format,
                value = _props3.value,
                onChange = _props3.onChange;


            var parsed = target.value && (0, _moment2.default)(target.value, format, true);

            this.setState({
                stringValue: target.value,
                invalid: Boolean(parsed && !parsed.isValid())
            }, function () {
                if (!parsed && value) {
                    onChange(null);
                }
                if (parsed && parsed.isValid() && (!value || !parsed.isSame(value))) {
                    onChange(parsed);
                }
            });
            /*
            const {value: stringValue} = target
            this.setState({stringValue})
             const { format, onChange, value} = this.props
             if(value && !stringValue) {
                onChange(null)
                return
            }
             const parsed = moment(stringValue, format, true)
            if (!parsed.isValid()) {
                this.setState({invalid: true})
                return
            }
             if ((value && !value.isSame(parsed)) || !value) {
                onChange(parsed)
            }
            this.setState({invalid: false})
            */
        }
    }, {
        key: 'renderClearButton',
        value: function renderClearButton() {
            var _props4 = this.props,
                style = _props4.style,
                isHovered = _props4.isHovered,
                clearText = _props4.clearText,
                onChange = _props4.onChange;
            var _style$clear = style.clear,
                clearStyle = _style$clear === undefined ? {} : _style$clear;
            var hoveredStyle = clearStyle[':hover'];


            var styleIcon = {
                fontFamily: 'SignavioFont',
                fontStyle: 'normal',
                verticalAlign: 'top',
                WebkitFontSmoothing: 'antialiased',
                MozFontSmoothing: 'antialiased',
                MsFontSmoothing: 'antialiased',
                OFontSmoothing: 'antialiased',
                fontSmoothing: 'antialiased'
            };

            var props = {
                className: 'date-picker-input-clear-btn',
                style: _extends({}, clearStyle, isHovered ? hoveredStyle : {}),
                title: clearText,
                onClick: function onClick() {
                    return onChange(null);
                }
            };
            return _react2.default.createElement(
                'div',
                props,
                _react2.default.createElement(
                    'i',
                    { style: styleIcon },
                    '\u2715'
                )
            );
        }
    }, {
        key: 'handleClear',
        value: function handleClear(evt) {
            evt.preventDefault();
            this.setState({ stringValue: '' });
            this.props.onClear(evt);
        }
    }, {
        key: 'handleKeyDown',
        value: function handleKeyDown(evt) {
            if (evt.keyCode !== 13 && evt.keyCode !== 27) {
                return;
            }
            var _props5 = this.props,
                onCollapsePanel = _props5.onCollapsePanel,
                onKeyDown = _props5.onKeyDown;

            this.input.blur();
            onCollapsePanel();
            onKeyDown && onKeyDown(evt);
        }
    }, {
        key: 'componentWillReceiveProps',
        value: function componentWillReceiveProps(nextProps) {
            var value = nextProps.value,
                format = nextProps.format;


            this.setState({
                stringValue: value && value.format(format) || '',
                invalid: false
            });
        }
    }]);

    return Input;
}(_react.PureComponent);

Input.propTypes = {
    format: _react.PropTypes.string,
    placeholder: _react.PropTypes.string,
    clearText: _react.PropTypes.string,
    value: _react.PropTypes.object,
    withClear: _react.PropTypes.bool,
    isExpanded: _react.PropTypes.bool.isRequired,
    isHovered: _react.PropTypes.bool.isRequired,
    onChange: _react.PropTypes.func,
    onClear: _react.PropTypes.func,
    onCollapsePanel: _react.PropTypes.func
};

Input.defaultProps = {
    withClear: false,
    format: 'YYYY/MM/DD'
};

exports.default = (0, _hover2.default)(Input);
//# sourceMappingURL=Input.js.map