import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";

var __rest = this && this.__rest || function (s, e) {
  var t = {};

  for (var p in s) {
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  }

  if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
    if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  }
  return t;
};

import classNames from 'classnames';
import omit from 'omit.js';
import * as React from 'react';
import RcMentions from 'rc-mentions';
import Spin from '../spin';
import { ConfigConsumer } from '../config-provider';
var Option = RcMentions.Option;

function loadingFilterOption() {
  return true;
}

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

  var _super = _createSuper(Mentions);

  function Mentions() {
    var _this;

    _classCallCheck(this, Mentions);

    _this = _super.apply(this, arguments);
    _this.state = {
      focused: false
    };

    _this.onFocus = function () {
      var onFocus = _this.props.onFocus;

      if (onFocus) {
        onFocus.apply(void 0, arguments);
      }

      _this.setState({
        focused: true
      });
    };

    _this.onBlur = function () {
      var onBlur = _this.props.onBlur;

      if (onBlur) {
        onBlur.apply(void 0, arguments);
      }

      _this.setState({
        focused: false
      });
    };

    _this.getOptions = function () {
      var _this$props = _this.props,
          children = _this$props.children,
          loading = _this$props.loading;

      if (loading) {
        return /*#__PURE__*/React.createElement(Option, {
          value: "ANTD_SEARCHING",
          disabled: true
        }, /*#__PURE__*/React.createElement(Spin, {
          size: "small"
        }));
      }

      return children;
    };

    _this.getFilterOption = function () {
      var _this$props2 = _this.props,
          filterOption = _this$props2.filterOption,
          loading = _this$props2.loading;

      if (loading) {
        return loadingFilterOption;
      }

      return filterOption;
    };

    _this.saveMentions = function (node) {
      _this.rcMentions = node;
    };

    _this.renderMentions = function (_ref) {
      var _classNames;

      var getPrefixCls = _ref.getPrefixCls,
          renderEmpty = _ref.renderEmpty,
          direction = _ref.direction;
      var focused = _this.state.focused;

      var _a = _this.props,
          customizePrefixCls = _a.prefixCls,
          className = _a.className,
          disabled = _a.disabled,
          restProps = __rest(_a, ["prefixCls", "className", "disabled"]);

      var prefixCls = getPrefixCls('mentions', customizePrefixCls);
      var mentionsProps = omit(restProps, ['loading']);
      var mergedClassName = classNames(className, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-disabled"), disabled), _defineProperty(_classNames, "".concat(prefixCls, "-focused"), focused), _defineProperty(_classNames, "".concat(prefixCls, "-rtl"), direction === 'rtl'), _classNames));
      return /*#__PURE__*/React.createElement(RcMentions, _extends({
        prefixCls: prefixCls,
        notFoundContent: _this.getNotFoundContent(renderEmpty),
        className: mergedClassName,
        disabled: disabled,
        direction: direction
      }, mentionsProps, {
        filterOption: _this.getFilterOption(),
        onFocus: _this.onFocus,
        onBlur: _this.onBlur,
        ref: _this.saveMentions
      }), _this.getOptions());
    };

    return _this;
  }

  _createClass(Mentions, [{
    key: "getNotFoundContent",
    value: function getNotFoundContent(renderEmpty) {
      var notFoundContent = this.props.notFoundContent;

      if (notFoundContent !== undefined) {
        return notFoundContent;
      }

      return renderEmpty('Select');
    }
  }, {
    key: "focus",
    value: function focus() {
      this.rcMentions.focus();
    }
  }, {
    key: "blur",
    value: function blur() {
      this.rcMentions.blur();
    }
  }, {
    key: "render",
    value: function render() {
      return /*#__PURE__*/React.createElement(ConfigConsumer, null, this.renderMentions);
    }
  }]);

  return Mentions;
}(React.Component);

Mentions.Option = Option;

Mentions.getMentions = function () {
  var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  var config = arguments.length > 1 ? arguments[1] : undefined;

  var _ref2 = config || {},
      _ref2$prefix = _ref2.prefix,
      prefix = _ref2$prefix === void 0 ? '@' : _ref2$prefix,
      _ref2$split = _ref2.split,
      split = _ref2$split === void 0 ? ' ' : _ref2$split;

  var prefixList = Array.isArray(prefix) ? prefix : [prefix];
  return value.split(split).map(function () {
    var str = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
    var hitPrefix = null;
    prefixList.some(function (prefixStr) {
      var startStr = str.slice(0, prefixStr.length);

      if (startStr === prefixStr) {
        hitPrefix = prefixStr;
        return true;
      }

      return false;
    });

    if (hitPrefix !== null) {
      return {
        prefix: hitPrefix,
        value: str.slice(hitPrefix.length)
      };
    }

    return null;
  }).filter(function (entity) {
    return !!entity && !!entity.value;
  });
};

export default Mentions;