"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.LinkMatcher = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _adfSchema = require("@atlaskit/adf-schema");
var _hyperlink = require("./hyperlink");
/* eslint-enable require-unicode-regexp */
/**
 * Instance of class LinkMatcher are used in autoformatting in place of Regex.
 * Hence it has been made similar to regex with an exec method.
 * Extending it directly from class Regex was introducing some issues, thus that has been avoided.
 */
var LinkMatcher = exports.LinkMatcher = /*#__PURE__*/function () {
  function LinkMatcher() {
    (0, _classCallCheck2.default)(this, LinkMatcher);
  }
  return (0, _createClass2.default)(LinkMatcher, null, [{
    key: "create",
    value: function create() {
      var LinkMatcherRegex = /*#__PURE__*/function () {
        function LinkMatcherRegex() {
          (0, _classCallCheck2.default)(this, LinkMatcherRegex);
        }
        return (0, _createClass2.default)(LinkMatcherRegex, [{
          key: "exec",
          value: function exec(str) {
            var stringsBySpace = str.slice(0, str.length - 1).split(' ');
            var lastStringBeforeSpace = stringsBySpace[stringsBySpace.length - 1];
            var isLastStringValid = lastStringBeforeSpace.length > 0;
            if (!str.endsWith(' ') || !isLastStringValid) {
              return null;
            }
            if (_hyperlink.DONTLINKIFY_REGEXP.test(lastStringBeforeSpace)) {
              return null;
            }
            var links = _adfSchema.linkify.match(lastStringBeforeSpace);
            if (!links || links.length === 0) {
              return null;
            }
            var lastMatch = links[links.length - 1];
            var lastLink = links[links.length - 1];
            lastLink.input = str.substring(lastMatch.index);
            lastLink.length = lastLink.lastIndex - lastLink.index + 1;
            lastLink.index = str.lastIndexOf(lastStringBeforeSpace) + lastMatch.index;
            return lastLink;
          }
        }]);
      }();
      return new LinkMatcherRegex();
    }
  }]);
}();