text: "use strict";
  Object.defineProperty(exports, "__esModule", { value: true });
  var tslib_1 = require("tslib");
  var React = require("react");
  var Utilities_1 = require("../../../Utilities");
  var SELECTION_FORWARD = 'forward';
  var SELECTION_BACKWARD = 'backward';
  var BaseAutoFill = /** @class */ (function (_super) {
      tslib_1.__extends(BaseAutoFill, _super);
      function BaseAutoFill(props) {
          var _this = _super.call(this, props) || this;
          _this._autoFillEnabled = true;
          _this._value = '';
          _this.state = {
              displayValue: props.defaultVisibleValue || ''
          };
          return _this;
      }
      Object.defineProperty(BaseAutoFill.prototype, "cursorLocation", {
          get: function () {
              if (this._inputElement) {
                  var inputElement = this._inputElement;
                  if (inputElement.selectionDirection !== SELECTION_FORWARD) {
                      return inputElement.selectionEnd;
                  }
                  else {
                      return inputElement.selectionStart;
                  }
              }
              else {
                  return -1;
              }
          },
          enumerable: true,
          configurable: true
      });
      Object.defineProperty(BaseAutoFill.prototype, "isValueSelected", {
          get: function () {
              return this.inputElement.selectionStart !== this.inputElement.selectionEnd;
          },
          enumerable: true,
          configurable: true
      });
      Object.defineProperty(BaseAutoFill.prototype, "value", {
          get: function () {
              return this._value;
          },
          enumerable: true,
          configurable: true
      });
      Object.defineProperty(BaseAutoFill.prototype, "selectionStart", {
          get: function () {
              return this._inputElement ? this._inputElement.selectionStart : -1;
          },
          enumerable: true,
          configurable: true
      });
      Object.defineProperty(BaseAutoFill.prototype, "selectionEnd", {
          get: function () {
              return this._inputElement ? this._inputElement.selectionEnd : -1;
          },
          enumerable: true,
          configurable: true
      });
      Object.defineProperty(BaseAutoFill.prototype, "inputElement", {
          get: function () {
              return this._inputElement;
          },
          enumerable: true,
          configurable: true
      });
      BaseAutoFill.prototype.componentWillReceiveProps = function (nextProps) {
          var newValue;
          if (this.props.updateValueInWillReceiveProps) {
              newValue = this.props.updateValueInWillReceiveProps();
          }
          if (this._autoFillEnabled && this._doesTextStartWith(nextProps.suggestedDisplayValue, newValue ? newValue : this._value)) {
              newValue = nextProps.suggestedDisplayValue;
          }
          if (typeof newValue === 'string') {
              this.setState({ displayValue: newValue });
          }
      };
      BaseAutoFill.prototype.componentDidUpdate = function () {
          var value = this._value;
          var _a = this.props, suggestedDisplayValue = _a.suggestedDisplayValue, shouldSelectFullInputValueInComponentDidUpdate = _a.shouldSelectFullInputValueInComponentDidUpdate;
          var differenceIndex = 0;
          if (this._autoFillEnabled && value && suggestedDisplayValue && this._doesTextStartWith(suggestedDisplayValue, value)) {
              var shouldSelectFullRange = false;
              if (shouldSelectFullInputValueInComponentDidUpdate) {
                  shouldSelectFullRange = shouldSelectFullInputValueInComponentDidUpdate();
              }
              if (shouldSelectFullRange) {
                  this._inputElement.setSelectionRange(0, suggestedDisplayValue.length, SELECTION_BACKWARD);
              }
              else {
                  while (differenceIndex < value.length && value[differenceIndex].toLocaleLowerCase() === suggestedDisplayValue[differenceIndex].toLocaleLowerCase()) {
                      differenceIndex++;
                  }
                  if (differenceIndex > 0) {
                      this._inputElement.setSelectionRange(differenceIndex, suggestedDisplayValue.length, SELECTION_BACKWARD);
                  }
              }
          }
      };
      BaseAutoFill.prototype.render = function () {
          var displayValue = this.state.displayValue;
          var nativeProps = Utilities_1.getNativeProps(this.props, Utilities_1.inputProperties);
          return (React.createElement("input", tslib_1.__assign({}, nativeProps, { ref: this._resolveRef('_inputElement'), value: displayValue, autoCapitalize: 'off', autoComplete: 'off', onChange: this._onChange, onKeyDown: this._onKeyDown, onClick: this.props.onClick ? this.props.onClick : this._onClick, "data-lpignore": true })));
      };
      BaseAutoFill.prototype.focus = function () {
          this._inputElement.focus();
      };
      BaseAutoFill.prototype.clear = function () {
          this._autoFillEnabled = true;
          this._updateValue('');
          this._inputElement.setSelectionRange(0, 0);
      };
      BaseAutoFill.prototype._onClick = function () {
          if (this._value && this._value !== '' && this._autoFillEnabled) {
              this._autoFillEnabled = false;
          }
      };
      BaseAutoFill.prototype._onKeyDown = function (ev) {
          if (this.props.onKeyDown) {
              this.props.onKeyDown(ev);
          }
          switch (ev.which) {
              case 8 /* backspace */:
                  this._autoFillEnabled = false;
                  break;
              case 37 /* left */:
                  if (this._autoFillEnabled) {
                      this._autoFillEnabled = false;
                  }
                  break;
              case 39 /* right */:
                  if (this._autoFillEnabled) {
                      this._autoFillEnabled = false;
                  }
                  break;
              default:
                  if (!this._autoFillEnabled) {
                      if (this.props.enableAutoFillOnKeyPress.indexOf(ev.which) !== -1) {
                          this._autoFillEnabled = true;
                      }
                  }
                  break;
          }
      };
      BaseAutoFill.prototype._onChange = function (ev) {
          var value = ev.target.value;
          if (value && ev.target.selectionStart === value.length && !this._autoFillEnabled && value.length > this._value.length) {
              this._autoFillEnabled = true;
          }
          this._updateValue(value);
      };
      BaseAutoFill.prototype._notifyInputChange = function (newValue) {
          if (this.props.onInputValueChange) {
              this.props.onInputValueChange(newValue);
          }
      };
      BaseAutoFill.prototype._updateValue = function (newValue) {
          var _this = this;
          this._value = this.props.onInputChange ? this.props.onInputChange(newValue) : newValue;
          var displayValue = newValue;
          if (this.props.suggestedDisplayValue &&
              this._doesTextStartWith(this.props.suggestedDisplayValue, displayValue)
              && this._autoFillEnabled) {
              displayValue = this.props.suggestedDisplayValue;
          }
          this.setState({
              displayValue: newValue
          }, function () { return _this._notifyInputChange(_this._value); });
      };
      BaseAutoFill.prototype._doesTextStartWith = function (text, startWith) {
          if (!text || !startWith) {
              return false;
          }
          return text.toLocaleLowerCase().indexOf(startWith.toLocaleLowerCase()) === 0;
      };
      BaseAutoFill.defaultProps = {
          enableAutoFillOnKeyPress: [40 /* down */, 38 /* up */]
      };
      tslib_1.__decorate([
          Utilities_1.autobind
      ], BaseAutoFill.prototype, "_onClick", null);
      tslib_1.__decorate([
          Utilities_1.autobind
      ], BaseAutoFill.prototype, "_onKeyDown", null);
      tslib_1.__decorate([
          Utilities_1.autobind
      ], BaseAutoFill.prototype, "_onChange", null);
      tslib_1.__decorate([
          Utilities_1.autobind
      ], BaseAutoFill.prototype, "_updateValue", null);
      return BaseAutoFill;
  }(Utilities_1.BaseComponent));
  exports.BaseAutoFill = BaseAutoFill;
  //# sourceMappingURL=BaseAutoFill.js.map