"use strict";

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

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

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

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

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

  function AutoComplete(props) {
    var _this;

    _classCallCheck(this, AutoComplete);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(AutoComplete).call(this, props));
    _this.state = {
      errorText: props.attributes.errorText || '',
      value: props.attributes.value || ''
    };
    _this.onUpdateInput = _this.onUpdateInput.bind(_assertThisInitialized(_this));
    _this.onNewRequest = _this.onNewRequest.bind(_assertThisInitialized(_this));
    _this.filter = _this.filter.bind(_assertThisInitialized(_this));
    _this.onBlur = _this.onBlur.bind(_assertThisInitialized(_this));
    _this.onFocus = _this.onFocus.bind(_assertThisInitialized(_this));
    return _this;
  }

  _createClass(AutoComplete, [{
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps(props) {
      this.setState({
        errorText: props.attributes.errorText || '',
        value: props.attributes.value || ''
      });
    }
  }, {
    key: "validate",
    value: function validate(value) {
      var isValid = true;

      if (this.props.rules && this.props.rules.validation) {
        var _iteratorNormalCompletion = true;
        var _didIteratorError = false;
        var _iteratorError = undefined;

        try {
          for (var _iterator = this.props.rules.validation[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
            var data = _step.value;
            isValid = _validation.default[data.rule](value, data.value);

            if (!isValid) {
              return {
                isValid: false,
                message: data.message
              };
            }
          }
        } catch (err) {
          _didIteratorError = true;
          _iteratorError = err;
        } finally {
          try {
            if (!_iteratorNormalCompletion && _iterator.return != null) {
              _iterator.return();
            }
          } finally {
            if (_didIteratorError) {
              throw _iteratorError;
            }
          }
        }
      }

      return {
        isValid: true,
        message: ''
      };
    }
  }, {
    key: "filter",
    value: function filter() {
      if (typeof this.props.filter === 'function') {
        var _this$props;

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

        (_this$props = this.props).filter.apply(_this$props, [this.props.control].concat(args));
      }
    }
  }, {
    key: "onUpdateInput",
    value: function onUpdateInput() {
      for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
        args[_key2] = arguments[_key2];
      }

      this.setState({
        value: args[0]
      });

      if (typeof this.props.onUpdateInput === 'function') {
        var _this$props2;

        (_this$props2 = this.props).onUpdateInput.apply(_this$props2, [this.props.control].concat(args));
      }
    }
  }, {
    key: "onNewRequest",
    value: function onNewRequest() {
      if (typeof this.props.onNewRequest === 'function') {
        var _this$props3;

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

        (_this$props3 = this.props).onNewRequest.apply(_this$props3, [this.props.control].concat(args));
      }
    }
  }, {
    key: "onBlur",
    value: function onBlur() {
      var props = this.props;

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

      var validator = this.validate(args[0].target.value);

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

      if (typeof props.onBlur === 'function') {
        props.onBlur.apply(props, [props.control].concat(args));
      }
    }
  }, {
    key: "onFocus",
    value: function onFocus() {
      if (typeof this.props.onFocus === 'function') {
        var _this$props4;

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

        (_this$props4 = this.props).onFocus.apply(_this$props4, [this.props.control].concat(args));
      }
    }
  }, {
    key: "render",
    value: function render() {
      var props = this.props;
      var AUTOCOMPLETE = props.library[props.component];
      var filter = typeof this.props.filter === 'function' ? this.props.filter : AUTOCOMPLETE[props.attributes.filter];
      return _react.default.createElement("div", {
        style: {
          display: 'flex'
        }
      }, _react.default.createElement(AUTOCOMPLETE, _extends({}, props.attributes, {
        value: this.state.value,
        filter: filter,
        errorText: this.state.errorText,
        onBlur: this.onBlur,
        onFocus: this.onFocus,
        onUpdateInput: this.onUpdateInput,
        onNewRequest: this.onNewRequest
      })), this.props.attributes.tooltip && _react.default.createElement(_TooltipComponent.default, {
        tooltip: this.props.attributes.tooltip
      }));
    }
  }]);

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

AutoComplete.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,
  onUpdateInput: _propTypes.default.func,
  onNewRequest: _propTypes.default.func,
  onFocus: _propTypes.default.func,
  onBlur: _propTypes.default.func,
  filter: _propTypes.default.func
} : {};
var _default = AutoComplete;
exports.default = _default;