"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _numeral = _interopRequireDefault(require("numeral"));

var _Icon = _interopRequireDefault(require("@material-ui/core/Icon"));

var _validation = _interopRequireDefault(require("../../helpers/validation"));

var _TooltipComponent = _interopRequireDefault(require("../TooltipComponent"));

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

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _extends() { _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; }; return _extends.apply(this, arguments); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

/** Textfield Component */
var TextField =
/*#__PURE__*/
function (_React$Component) {
  _inherits(TextField, _React$Component);

  function TextField(props) {
    var _this;

    _classCallCheck(this, TextField);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(TextField).call(this, props));
    _this.onChange = _this.onChange.bind(_assertThisInitialized(_this));
    _this.onBlur = _this.onBlur.bind(_assertThisInitialized(_this));
    _this.onFocus = _this.onFocus.bind(_assertThisInitialized(_this));
    _this.format = _this.format.bind(_assertThisInitialized(_this));
    _this.getFormattedValue = _this.getFormattedValue.bind(_assertThisInitialized(_this));
    _this.state = {
      errorText: props.attributes.errorText || '',
      value: _this.format(props.attributes.value) || _this.format(props.attributes.defaultValue) || ''
    };
    return _this;
  }

  _createClass(TextField, [{
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps(props) {
      this.setState({
        errorText: props.attributes.errorText || '',
        value: this.format(props.attributes.value) || ''
      });
    }
  }, {
    key: "format",
    value: function format(value) {
      var formattedValue = value;
      var props = this.props;
      var formatter = props.formatter;
      var number = (0, _numeral.default)(value).value() || 0;

      if (props.formatter && props.formatter.func && props.formatter.func.format) {
        number = (0, _numeral.default)(number)[props.formatter.func.format.name](props.formatter.func.format.value).value();
        formattedValue = number;
      }

      if (formatter) {
        switch (formatter.type) {
          case 'number':
            formattedValue = (0, _numeral.default)(number).format(formatter.expression);
            break;

          default:
            break;
        }
      }

      return formattedValue;
    }
  }, {
    key: "unformat",
    value: function unformat(value) {
      var unformattedValue = value;
      var props = this.props;
      var formatter = props.formatter;

      if (props.formatter && props.formatter.func && props.formatter.func.unformat) {
        unformattedValue = (0, _numeral.default)(value)[props.formatter.func.unformat.name](props.formatter.func.unformat.value).value();
      }

      if (formatter) {
        switch (formatter.type) {
          case 'number':
            unformattedValue = (0, _numeral.default)(unformattedValue).value();
            break;

          default:
            break;
        }
      }

      return unformattedValue;
    }
  }, {
    key: "validate",
    value: function validate(value) {
      var isValid = true;
      var props = this.props;

      if (props.rules && props.rules.validation) {
        for (var i = 0; i < props.rules.validation.length; i += 1) {
          var data = props.rules.validation[i];
          isValid = _validation.default[data.rule](value, data.value);

          if (!isValid) {
            return {
              isValid: false,
              message: data.message
            };
          }
        }
      }

      return {
        isValid: true,
        message: ''
      };
    }
  }, {
    key: "getFormattedValue",
    value: function getFormattedValue(val) {
      var props = this.props;
      var formatter = props.formatter;
      var value = val;

      if (formatter) {
        switch (formatter.type) {
          case 'number':
            value = (0, _numeral.default)(val).value();
            break;

          default:
            break;
        }
      }

      return value;
    }
  }, {
    key: "onChange",
    value: function onChange() {
      var props = this.props;

      for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
        args[_key] = arguments[_key];
      }

      this.setState({
        value: args[0].target.value
      });
      var formattedValue = this.getFormattedValue(args[0].target.value);

      if (typeof props.onChange === 'function') {
        props.onChange(props.control, args[0], formattedValue);
      }
    }
  }, {
    key: "onBlur",
    value: function onBlur() {
      var props = this.props;

      for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
        args[_key2] = arguments[_key2];
      }

      var validator = this.validate(this.format(args[0].target.value));
      var formattedValue = this.getFormattedValue(args[0].target.value);
      this.setState({
        value: this.format(args[0].target.value)
      });

      if (!validator.isValid) {
        this.setState({
          errorText: validator.message
        });
      } else {
        this.setState({
          errorText: ''
        });
      }

      if (typeof props.onBlur === 'function') {
        props.onBlur(props.control, args[0], formattedValue);
      }
    }
  }, {
    key: "onFocus",
    value: function onFocus() {
      var props = this.props;

      for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
        args[_key3] = arguments[_key3];
      }

      var formattedValue = this.getFormattedValue(args[0].target.value);
      this.setState({
        value: this.unformat(args[0].target.value)
      });

      if (typeof props.onFocus === 'function') {
        props.onFocus(props.control, args[0], formattedValue);
      }
    }
  }, {
    key: "getInputProps",
    value: function getInputProps(props) {
      var attributes = props.attributes;

      if (attributes.InputProps) {
        if (attributes.InputProps.InputAdornment) {
          var INPUTADORMENT = props.library.InputAdornment;
          return {
            startAdornment: _react.default.createElement(INPUTADORMENT, attributes.InputProps.InputAdornment, attributes.InputProps.InputAdornment.icon ? _react.default.createElement(_Icon.default, null, attributes.InputProps.InputAdornment.icon) : '')
          };
        }
      }

      return {};
    }
  }, {
    key: "render",
    value: function render() {
      var props = this.props;
      var _this$state = this.state,
          value = _this$state.value,
          errorText = _this$state.errorText;
      var TEXTFIELD = props.library[props.component];
      return _react.default.createElement("div", {
        style: {
          display: 'flex'
        }
      }, _react.default.createElement(TEXTFIELD, _extends({}, props.attributes, {
        InputProps: this.getInputProps(props),
        value: value,
        error: !!errorText,
        helperText: errorText || '',
        onChange: this.onChange,
        onBlur: this.onBlur,
        onFocus: this.onFocus
      })), props.attributes.tooltip && _react.default.createElement(_TooltipComponent.default, {
        tooltip: props.attributes.tooltip
      }));
    }
  }]);

  return TextField;
}(_react.default.Component);

TextField.propTypes = process.env.NODE_ENV !== "production" ? {
  library: _propTypes.default.object,
  component: _propTypes.default.string.isRequired,
  attributes: _propTypes.default.object,
  control: _propTypes.default.object,
  option: _propTypes.default.string.isRequired,
  rules: _propTypes.default.object,
  formatter: _propTypes.default.object,
  onChange: _propTypes.default.func,
  onFocus: _propTypes.default.func,
  onBlur: _propTypes.default.func
} : {};
TextField.defaultProps = {
  library: null,
  attributes: null,
  control: null,
  rules: null,
  formatter: null,
  onChange: null,
  onFocus: null,
  onBlur: null
};
var _default = TextField;
exports.default = _default;