"use strict";

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

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

var _reactMaterialize = require("react-materialize");

require("./MyAutocomplete.css");

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

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 _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 _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

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

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

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

  function MyAutocomplete(props) {
    var _this;

    _classCallCheck(this, MyAutocomplete);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(MyAutocomplete).call(this, props));
    _this.state = {
      autoComplete: false,
      value: props.value || '',
      selected: -1
    };
    return _this;
  }

  _createClass(MyAutocomplete, [{
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps(props) {
      this.search(this.state.value, props);
    }
  }, {
    key: "select",
    value: function select(item) {
      var onComplete = this.props.onComplete;
      onComplete && onComplete(this.state.value, item);
    }
  }, {
    key: "search",
    value: function search(term, props) {
      if (term.length < 2) {
        this.setState({
          autoComplete: false
        });
        return;
      }

      var data = props.data,
          options = props.options;
      var values = Object.keys(data);
      values.sort(function (a, b) {
        return a.localeCompare(b);
      });
      values = values.map(function (item) {
        return item.toUpperCase();
      });
      var segments = term.toUpperCase().split(' ');
      var dataset = Object.keys(data).reduce(function (res, a) {
        res[a.toUpperCase()] = data[a];
        return res;
      }, {});
      var correct = values.filter(function (item) {
        return segments.reduce(function (res, seg) {
          return res && item.includes(seg);
        }, true);
      });
      correct = correct.slice(0, this.props.options.limit);
      this.setState({
        autoComplete: correct.length > 0,
        autoCompleteData: correct,
        data: dataset
      });
    }
  }, {
    key: "change",
    value: function change(e) {
      var term = e.target.value;
      var onChange = this.props.onChange;
      this.search(term, this.props);
      onChange && onChange(e);
      this.setState({
        value: term
      });
    }
  }, {
    key: "keyHandler",
    value: function keyHandler(e) {
      console.log(e.keyCode);

      if (e.keyCode === 40) {
        this.setState({
          selected: this.state.selected < this.state.autoCompleteData.length - 1 ? this.state.selected + 1 : this.state.autoCompleteData.length - 1
        });
      } else if (e.keyCode === 38) {
        this.setState({
          selected: this.state.selected > 0 ? this.state.selected - 1 : 0
        });
      } else if (e.key === "Enter") {
        e.preventDefault();

        if (this.state.selected > -1) {
          this.select(this.state.data[this.state.autoCompleteData[this.state.selected]]);
        } else {
          this.select({
            key: this.state.value
          });
        }
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      return _react.default.createElement("div", {
        className: 'MyAutocomplete ' + this.props.className
      }, _react.default.createElement("form", {
        autoComplete: "off",
        style: {
          flexGrow: 1
        }
      }, _react.default.createElement("input", {
        onChange: function onChange(e) {
          return _this2.change(e);
        },
        value: this.state.value,
        placeholder: this.props.placeholder || 'Search',
        className: "autocomplete-options-fa98bb6c-92e7-abe2-025b-b9f8e505ff98",
        onKeyDown: function onKeyDown(e) {
          return _this2.keyHandler(e);
        }
      })), this.state.autoComplete ? _react.default.createElement("ul", {
        id: "autocomplete-options-d483f2ec-fa7d-1905-b56c-81047362fa8e",
        className: "autocomplete-content dropdown-content " + (this.state.autoComplete ? "myautocomplete-visible" : ""),
        tabindex: "0"
      }, this.state.autoCompleteData.map(function (item, indx) {
        return _react.default.createElement("li", {
          key: item,
          onClick: function onClick() {
            return _this2.select(_this2.state.data[item]);
          },
          className: _this2.state.selected === indx ? "active" : "",
          onMouseEnter: function onMouseEnter() {
            return _this2.setState({
              selected: -1
            });
          }
        }, _this2.state.data[item] && _react.default.createElement("img", {
          src: _this2.state.data[item].image,
          class: "right circle"
        }), _react.default.createElement("span", null, item));
      })) : null);
    }
  }]);

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

var _default = MyAutocomplete;
exports.default = _default;