var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
    if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
    return cooked;
};
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    }
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
import React from 'react';
import styled, { css } from 'react-emotion';
import { Input, Icon } from '@bitbloq/ui';
var Container = styled.div(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n  position: relative;\n  flex: 1;\n  display: flex;\n  border: 1px solid #cfcfcf;\n  border-radius: 4px;\n\n  ", "\n"], ["\n  position: relative;\n  flex: 1;\n  display: flex;\n  border: 1px solid #cfcfcf;\n  border-radius: 4px;\n\n  ",
    "\n"])), function (props) { return props.focused && css(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n    border: 1px solid #5d6069;\n  "], ["\n    border: 1px solid #5d6069;\n  "]))); });
var Button = styled.div(templateObject_3 || (templateObject_3 = __makeTemplateObject(["\n  width: 20px;\n  cursor: pointer;\n  display: flex;\n  align-items: center;\n  justify-content: center; \n"], ["\n  width: 20px;\n  cursor: pointer;\n  display: flex;\n  align-items: center;\n  justify-content: center; \n"])));
var DecrementButton = styled(Button)(templateObject_4 || (templateObject_4 = __makeTemplateObject(["\n  border-radius: 4px 0px 0px 4px;\n  border-right: 1px solid #cfcfcf;\n  color: #8c919b;\n\n  svg {\n    transform: rotate(-90deg);\n  }\n"], ["\n  border-radius: 4px 0px 0px 4px;\n  border-right: 1px solid #cfcfcf;\n  color: #8c919b;\n\n  svg {\n    transform: rotate(-90deg);\n  }\n"])));
var IncrementButton = styled(Button)(templateObject_5 || (templateObject_5 = __makeTemplateObject(["\n  border-radius: 0px 4px 4px 0px;\n  border-left: 1px solid #cfcfcf;\n  color: #8c919b;\n\n  svg {\n    transform: rotate(90deg);\n  }\n"], ["\n  border-radius: 0px 4px 4px 0px;\n  border-left: 1px solid #cfcfcf;\n  color: #8c919b;\n\n  svg {\n    transform: rotate(90deg);\n  }\n"])));
var StyledInput = styled(Input)(templateObject_6 || (templateObject_6 = __makeTemplateObject(["\n  border: none;\n\n  &::-webkit-outer-spin-button,\n  &::-webkit-inner-spin-button {\n    -webkit-appearance: none;\n    margin: 0;\n  }\n\n  &:focus {\n    border: none;\n  }\n"], ["\n  border: none;\n\n  &::-webkit-outer-spin-button,\n  &::-webkit-inner-spin-button {\n    -webkit-appearance: none;\n    margin: 0;\n  }\n\n  &:focus {\n    border: none;\n  }\n"])));
var ValueText = styled.span(templateObject_7 || (templateObject_7 = __makeTemplateObject(["\n  position: absolute;\n  display: flex;\n  align-items: center;\n  font-size: 13px;\n  top: 0px;\n  left: 32px;\n  height: 33px;\n  pointer-events: none;\n"], ["\n  position: absolute;\n  display: flex;\n  align-items: center;\n  font-size: 13px;\n  top: 0px;\n  left: 32px;\n  height: 33px;\n  pointer-events: none;\n"])));
var NumberInput = /** @class */ (function (_super) {
    __extends(NumberInput, _super);
    function NumberInput(props) {
        var _this = _super.call(this, props) || this;
        _this.onChange = function (e) {
            var onChange = _this.props.onChange;
            _this.setState({
                text: e.target.value
            });
            if (onChange) {
                onChange(Number(e.target.value) || 0);
            }
        };
        _this.onFocus = function (e) {
            var onFocus = _this.props.onFocus;
            _this.setState({ focused: true });
            if (onFocus) {
                onFocus(e);
            }
        };
        _this.onBlur = function (e) {
            var _a = _this.props, onBlur = _a.onBlur, value = _a.value;
            _this.setState({
                focused: false,
                text: Number(value) ? Number(value).toString() : ''
            });
            if (onBlur) {
                onBlur(e);
            }
        };
        _this.onDecrementClick = function () {
            var _a = _this.props, value = _a.value, onChange = _a.onChange;
            if (onChange) {
                onChange(Number(value) - 1);
            }
        };
        _this.onIncrementClick = function () {
            var _a = _this.props, value = _a.value, onChange = _a.onChange;
            if (onChange) {
                onChange(Number(value) + 1);
            }
        };
        _this.input = React.createRef();
        _this.state = {
            focused: false,
            text: Number(props.value) ? Number(props.value).toString() : ''
        };
        return _this;
    }
    NumberInput.prototype.componentDidUpdate = function (prevProps, prevState) {
        var value = this.props.value;
        var _a = this.state, focused = _a.focused, text = _a.text;
        var input = this.input.current;
        if (input && focused && !prevState.focused) {
            input.select();
        }
        var numberValue = Number(text) || 0;
        if (value !== numberValue) {
            this.setState({
                text: Number(value) ? Number(value).toString() : ''
            });
        }
    };
    NumberInput.prototype.render = function () {
        var _a = this.state, focused = _a.focused, text = _a.text;
        var _b = this.props, value = _b.value, unit = _b.unit;
        return (React.createElement(Container, { focused: focused },
            React.createElement(DecrementButton, { onClick: this.onDecrementClick },
                React.createElement(Icon, { name: "arrow" })),
            React.createElement(StyledInput, __assign({}, this.props, { innerRef: this.input, value: focused ? text : '', onChange: this.onChange, type: "number", onFocus: this.onFocus, onBlur: this.onBlur })),
            !focused &&
                React.createElement(ValueText, null,
                    value,
                    " ",
                    unit),
            React.createElement(IncrementButton, { onClick: this.onIncrementClick },
                React.createElement(Icon, { name: "arrow" }))));
    };
    return NumberInput;
}(React.Component));
export default NumberInput;
var templateObject_1, templateObject_2, templateObject_3, templateObject_4, templateObject_5, templateObject_6, templateObject_7;
//# sourceMappingURL=NumberInput.js.map