"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _readOnlyError2 = _interopRequireDefault(require("@babel/runtime/helpers/readOnlyError"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireDefault(require("react"));
var _reactIntl = require("react-intl");
var _analytics = require("../../analytics");
var _LinkSearchList = _interopRequireDefault(require("./LinkSearchList"));
var _withActivityProvider = _interopRequireDefault(require("./withActivityProvider"));
function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2.default)(o), (0, _possibleConstructorReturn2.default)(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2.default)(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
var DEFAULT_ITEMS_LIMIT = 5;
var limit = function limit(items, max) {
  return items.slice(0, max);
};

// Ignored via go/ees005
// eslint-disable-next-line @repo/internal/react/no-class-components
var RecentLink = /*#__PURE__*/function (_React$Component) {
  function RecentLink(props) {
    var _this;
    (0, _classCallCheck2.default)(this, RecentLink);
    _this = _callSuper(this, RecentLink, [props]);
    (0, _defineProperty2.default)(_this, "activityProvider", null);
    (0, _defineProperty2.default)(_this, "handleSubmit", function () {
      var _this$state = _this.state,
        items = _this$state.items,
        url = _this$state.url,
        selectedIndex = _this$state.selectedIndex;
      var inputMethod = _this.getCurrentInputMethod();
      if (!inputMethod) {
        return; // No call submit, if there is nothing to submit
      }
      switch (inputMethod) {
        case _analytics.INPUT_METHOD.MANUAL:
          {
            _this.props.onSubmit({
              url: url,
              text: url,
              inputMethod: _analytics.INPUT_METHOD.MANUAL
            });
            break;
          }
        case _analytics.INPUT_METHOD.TYPEAHEAD:
          {
            var item = items[selectedIndex];
            _this.setState(function () {
              return {
                url: item.url
              };
            });
            if (_this.props.onSubmit) {
              _this.props.onSubmit({
                url: item.url,
                text: item.name,
                inputMethod: _analytics.INPUT_METHOD.TYPEAHEAD
              });
            }
            break;
          }
      }
    });
    (0, _defineProperty2.default)(_this, "handleSelected", function (href, text) {
      if (_this.props.onSubmit) {
        _this.setState(function () {
          return {
            url: href
          };
        });
        _this.props.onSubmit({
          text: text,
          url: href,
          inputMethod: _analytics.INPUT_METHOD.TYPEAHEAD
        });
      }
    });
    (0, _defineProperty2.default)(_this, "handleChange", /*#__PURE__*/function () {
      var _ref = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(input) {
        var _t, _t2, _t3, _t4, _t5, _t6, _t7;
        return _regenerator.default.wrap(function (_context) {
          while (1) switch (_context.prev = _context.next) {
            case 0:
              _this.setState({
                url: input
              });
              if (!_this.activityProvider) {
                _context.next = 4;
                break;
              }
              if (!(input.length === 0)) {
                _context.next = 2;
                break;
              }
              _t = _this;
              _t2 = limit;
              _context.next = 1;
              return _this.activityProvider.getRecentItems();
            case 1:
              _t3 = _t2(_context.sent, _this.props.limit);
              _t4 = -1;
              _t.setState.call(_t, {
                items: _t3,
                selectedIndex: _t4
              });
              _context.next = 4;
              break;
            case 2:
              _t5 = _this;
              _t6 = limit;
              _context.next = 3;
              return _this.activityProvider.searchRecent(input);
            case 3:
              _t7 = _t6(_context.sent, _this.props.limit);
              _t5.setState.call(_t5, {
                items: _t7,
                selectedIndex: 0
              });
            case 4:
            case "end":
              return _context.stop();
          }
        }, _callee);
      }));
      return function (_x) {
        return _ref.apply(this, arguments);
      };
    }());
    // Ignored via go/ees005
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    (0, _defineProperty2.default)(_this, "handleKeyDown", function (e) {
      var _this$state2 = _this.state,
        items = _this$state2.items,
        selectedIndex = _this$state2.selectedIndex;
      if (!items || !items.length) {
        return;
      }
      if (e.key === 'ArrowDown') {
        // down
        e.preventDefault();
        _this.setState({
          selectedIndex: (selectedIndex + 1) % items.length
        });
      } else if (e.key === 'ArrowUp') {
        // up
        e.preventDefault();
        _this.setState({
          selectedIndex: selectedIndex > 0 ? selectedIndex - 1 : items.length - 1
        });
      }
    });
    (0, _defineProperty2.default)(_this, "handleMouseMove", function (objectId) {
      var items = _this.state.items;
      if (items) {
        var index = items.findIndex(function (item) {
          return item.objectId === objectId;
        });
        _this.setState({
          selectedIndex: index
        });
      }
    });
    (0, _defineProperty2.default)(_this, "renderRecentList", function () {
      var _this$state3 = _this.state,
        items = _this$state3.items,
        isLoading = _this$state3.isLoading,
        selectedIndex = _this$state3.selectedIndex;
      return /*#__PURE__*/_react.default.createElement(_LinkSearchList.default, {
        items: items,
        isLoading: isLoading,
        selectedIndex: selectedIndex,
        onSelect: _this.handleSelected,
        onMouseMove: _this.handleMouseMove
      });
    });
    (0, _defineProperty2.default)(_this, "clearValue", function () {
      _this.setState({
        url: ''
      });
    });
    _this.state = {
      selectedIndex: -1,
      isLoading: false,
      items: [],
      url: props.defaultUrl || ''
    };
    return _this;
  }
  (0, _inherits2.default)(RecentLink, _React$Component);
  return (0, _createClass2.default)(RecentLink, [{
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      var _this2 = this;
      if (prevProps.defaultUrl !== this.props.defaultUrl) {
        this.setState(function (state) {
          if (state.url !== _this2.props.defaultUrl) {
            return {
              items: [],
              selectedIndex: -1,
              url: _this2.props.defaultUrl || ''
            };
          }
          return null;
        });
      }
    }
  }, {
    key: "componentDidMount",
    value: function () {
      var _componentDidMount = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2() {
        return _regenerator.default.wrap(function (_context2) {
          while (1) switch (_context2.prev = _context2.next) {
            case 0:
              if (!this.props.activityProvider) {
                _context2.next = 2;
                break;
              }
              _context2.next = 1;
              return this.props.activityProvider;
            case 1:
              this.activityProvider = _context2.sent;
              _context2.next = 2;
              return this.loadRecentItems(this.activityProvider);
            case 2:
            case "end":
              return _context2.stop();
          }
        }, _callee2, this);
      }));
      function componentDidMount() {
        return _componentDidMount.apply(this, arguments);
      }
      return componentDidMount;
    }()
  }, {
    key: "loadRecentItems",
    value: function () {
      var _loadRecentItems = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3(activityProvider) {
        var _t8, _t9, _t0;
        return _regenerator.default.wrap(function (_context3) {
          while (1) switch (_context3.prev = _context3.next) {
            case 0:
              _context3.prev = 0;
              if (this.state.url) {
                _context3.next = 2;
                break;
              }
              _t8 = this;
              _t9 = limit;
              _context3.next = 1;
              return activityProvider.getRecentItems();
            case 1:
              _t0 = _t9(_context3.sent, this.props.limit);
              _t8.setState.call(_t8, {
                isLoading: true,
                items: _t0
              });
            case 2:
              _context3.prev = 2;
              this.setState({
                isLoading: false
              });
              return _context3.finish(2);
            case 3:
            case "end":
              return _context3.stop();
          }
        }, _callee3, this, [[0,, 2, 3]]);
      }));
      function loadRecentItems(_x2) {
        return _loadRecentItems.apply(this, arguments);
      }
      return loadRecentItems;
    }()
  }, {
    key: "getCurrentInputMethod",
    value: function getCurrentInputMethod() {
      var _this$state4 = this.state,
        items = _this$state4.items,
        url = _this$state4.url,
        selectedIndex = _this$state4.selectedIndex;
      if (items && items.length > 0 && selectedIndex > -1) {
        return _analytics.INPUT_METHOD.TYPEAHEAD;
      } else if (url && url.length > 0) {
        return _analytics.INPUT_METHOD.MANUAL;
      }
      return;
    }
  }, {
    key: "render",
    value: function render() {
      var render = this.props.render;
      var url = this.state.url;
      return render({
        activityProvider: this.activityProvider,
        inputProps: {
          onChange: this.handleChange,
          onKeyDown: this.handleKeyDown,
          onSubmit: this.handleSubmit,
          value: url
        },
        clearValue: this.clearValue,
        currentInputMethod: this.getCurrentInputMethod(),
        renderRecentList: this.renderRecentList
      });
    }
  }]);
}(_react.default.Component);
(0, _defineProperty2.default)(RecentLink, "defaultProps", {
  limit: DEFAULT_ITEMS_LIMIT
});
var _default_1 = (0, _withActivityProvider.default)((0, _reactIntl.injectIntl)(RecentLink));
var _default = exports.default = _default_1;