"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});

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 _propTypes = require("prop-types");

var _propTypes2 = _interopRequireDefault(_propTypes);

var _reactBootstrapTypeahead = require("react-bootstrap-typeahead");

var _AsyncTypeaheadAPI = require("./AsyncTypeaheadAPI");

var _utils = require("./utils");

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

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 DEFAULT_OPTIONS = {
  required: false,
  labelKey: "name",
  minLength: 3,
  placeholder: "Search..."
};

var AsyncTypeaheadField = function (_Component) {
  _inherits(AsyncTypeaheadField, _Component);

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

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

    _this.handleSearch = function (query) {
      if (!query) {
        return;
      }

      var _this$props$uiSchema$ = _this.props.uiSchema.typeahead,
          url = _this$props$uiSchema$.url,
          optionsMapping = _this$props$uiSchema$.optionsMapping;


      (0, _AsyncTypeaheadAPI.search)(url, query, optionsMapping).then(function (options) {
        return _this.setState({ options: options });
      });
    };

    _this.handleSelectionChange = function (events) {
      if (events.length > 0) {
        var schemaEvents = (0, _utils.mapSchema)(events, _this.props.schema, _this.props.uiSchema.typeahead.responseSchemaMapping);
        _this.props.onChange(schemaEvents);
        setTimeout(function () {
          if (_this.refs.typeahead) {
            _this.refs.typeahead.getInstance().clear();
          }
        }, 0);
      }
    };

    _this.state = {
      options: []
    };
    return _this;
  }

  _createClass(AsyncTypeaheadField, [{
    key: "render",
    value: function render() {
      var typeahead = this.props.uiSchema.typeahead;


      var typeConf = Object.assign({}, DEFAULT_OPTIONS, typeahead);
      typeConf.onChange = this.handleSelectionChange;
      typeConf.onSearch = this.handleSearch;
      typeConf.options = this.state.options;
      typeConf.ref = "typeahead";
      typeConf.labelKey = (0, _utils.mapLabelKey)(typeahead.labelKey);

      return _react2.default.createElement(_reactBootstrapTypeahead.AsyncTypeahead, typeConf);
    }
  }]);

  return AsyncTypeaheadField;
}(_react.Component);

if ((0, _utils.isDevelopment)()) {
  AsyncTypeaheadField.propTypes = {
    schema: _propTypes2.default.object.isRequired,
    uiSchema: _propTypes2.default.shape({
      typeahead: _propTypes2.default.shape({
        url: _propTypes2.default.string.required,
        optionsMapping: _propTypes2.default.string,
        responseSchemaMapping: _propTypes2.default.object
      })
    })
  };
}

exports.default = AsyncTypeaheadField;