import g from "./lib/re.js";
function p(t) {
  return Array.prototype.slice.call(arguments, 1).forEach(function(e) {
    e && Object.keys(e).forEach(function(i) {
      t[i] = e[i];
    });
  }), t;
}
function l(t) {
  return Object.prototype.toString.call(t);
}
function y(t) {
  return l(t) === "[object String]";
}
function k(t) {
  return l(t) === "[object Object]";
}
function w(t) {
  return l(t) === "[object RegExp]";
}
function f(t) {
  return l(t) === "[object Function]";
}
function v(t) {
  return t.replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&");
}
const x = {
  fuzzyLink: !0,
  fuzzyEmail: !0,
  fuzzyIP: !1
};
function E(t) {
  return Object.keys(t || {}).reduce(function(_, e) {
    return _ || x.hasOwnProperty(e);
  }, !1);
}
const b = {
  "http:": {
    validate: function(t, _, e) {
      const i = t.slice(_);
      return e.re.http || (e.re.http = new RegExp(
        "^\\/\\/" + e.re.src_auth + e.re.src_host_port_strict + e.re.src_path,
        "i"
      )), e.re.http.test(i) ? i.match(e.re.http)[0].length : 0;
    }
  },
  "https:": "http:",
  "ftp:": "http:",
  "//": {
    validate: function(t, _, e) {
      const i = t.slice(_);
      return e.re.no_http || (e.re.no_http = new RegExp(
        "^" + e.re.src_auth + // Don't allow single-level domains, because of false positives like '//test'
        // with code comments
        "(?:localhost|(?:(?:" + e.re.src_domain + ")\\.)+" + e.re.src_domain_root + ")" + e.re.src_port + e.re.src_host_terminator + e.re.src_path,
        "i"
      )), e.re.no_http.test(i) ? _ >= 3 && t[_ - 3] === ":" || _ >= 3 && t[_ - 3] === "/" ? 0 : i.match(e.re.no_http)[0].length : 0;
    }
  },
  "mailto:": {
    validate: function(t, _, e) {
      const i = t.slice(_);
      return e.re.mailto || (e.re.mailto = new RegExp(
        "^" + e.re.src_email_name + "@" + e.re.src_host_strict,
        "i"
      )), e.re.mailto.test(i) ? i.match(e.re.mailto)[0].length : 0;
    }
  }
}, j = "a[cdefgilmnoqrstuwxz]|b[abdefghijmnorstvwyz]|c[acdfghiklmnoruvwxyz]|d[ejkmoz]|e[cegrstu]|f[ijkmor]|g[abdefghilmnpqrstuwy]|h[kmnrtu]|i[delmnoqrst]|j[emop]|k[eghimnprwyz]|l[abcikrstuvy]|m[acdeghklmnopqrstuvwxyz]|n[acefgilopruz]|om|p[aefghklmnrstwy]|qa|r[eosuw]|s[abcdeghijklmnortuvxyz]|t[cdfghjklmnortvwz]|u[agksyz]|v[aceginu]|w[fs]|y[et]|z[amw]", R = "biz|com|edu|gov|net|org|pro|web|xxx|aero|asia|coop|info|museum|name|shop|рф".split("|");
function O(t) {
  t.__index__ = -1, t.__text_cache__ = "";
}
function S(t) {
  return function(_, e) {
    const i = _.slice(e);
    return t.test(i) ? i.match(t)[0].length : 0;
  };
}
function z() {
  return function(t, _) {
    _.normalize(t);
  };
}
function u(t) {
  const _ = t.re = g(t.__opts__), e = t.__tlds__.slice();
  t.onCompile(), t.__tlds_replaced__ || e.push(j), e.push(_.src_xn), _.src_tlds = e.join("|");
  function i(n) {
    return n.replace("%TLDS%", _.src_tlds);
  }
  _.email_fuzzy = RegExp(i(_.tpl_email_fuzzy), "i"), _.link_fuzzy = RegExp(i(_.tpl_link_fuzzy), "i"), _.link_no_ip_fuzzy = RegExp(i(_.tpl_link_no_ip_fuzzy), "i"), _.host_fuzzy_test = RegExp(i(_.tpl_host_fuzzy_test), "i");
  const s = [];
  t.__compiled__ = {};
  function h(n, r) {
    throw new Error('(LinkifyIt) Invalid schema "' + n + '": ' + r);
  }
  Object.keys(t.__schemas__).forEach(function(n) {
    const r = t.__schemas__[n];
    if (r === null)
      return;
    const a = { validate: null, link: null };
    if (t.__compiled__[n] = a, k(r)) {
      w(r.validate) ? a.validate = S(r.validate) : f(r.validate) ? a.validate = r.validate : h(n, r), f(r.normalize) ? a.normalize = r.normalize : r.normalize ? h(n, r) : a.normalize = z();
      return;
    }
    if (y(r)) {
      s.push(n);
      return;
    }
    h(n, r);
  }), s.forEach(function(n) {
    t.__compiled__[t.__schemas__[n]] && (t.__compiled__[n].validate = t.__compiled__[t.__schemas__[n]].validate, t.__compiled__[n].normalize = t.__compiled__[t.__schemas__[n]].normalize);
  }), t.__compiled__[""] = { validate: null, normalize: z() };
  const o = Object.keys(t.__compiled__).filter(function(n) {
    return n.length > 0 && t.__compiled__[n];
  }).map(v).join("|");
  t.re.schema_test = RegExp("(^|(?!_)(?:[><｜]|" + _.src_ZPCc + "))(" + o + ")", "i"), t.re.schema_search = RegExp("(^|(?!_)(?:[><｜]|" + _.src_ZPCc + "))(" + o + ")", "ig"), t.re.schema_at_start = RegExp("^" + t.re.schema_search.source, "i"), t.re.pretest = RegExp(
    "(" + t.re.schema_test.source + ")|(" + t.re.host_fuzzy_test.source + ")|@",
    "i"
  ), O(t);
}
function A(t, _) {
  const e = t.__index__, i = t.__last_index__, s = t.__text_cache__.slice(e, i);
  this.schema = t.__schema__.toLowerCase(), this.index = e + _, this.lastIndex = i + _, this.raw = s, this.text = s, this.url = s;
}
function d(t, _) {
  const e = new A(t, _);
  return t.__compiled__[e.schema].normalize(e, t), e;
}
function c(t, _) {
  if (!(this instanceof c))
    return new c(t, _);
  _ || E(t) && (_ = t, t = {}), this.__opts__ = p({}, x, _), this.__index__ = -1, this.__last_index__ = -1, this.__schema__ = "", this.__text_cache__ = "", this.__schemas__ = p({}, b, t), this.__compiled__ = {}, this.__tlds__ = R, this.__tlds_replaced__ = !1, this.re = {}, u(this);
}
c.prototype.add = function(_, e) {
  return this.__schemas__[_] = e, u(this), this;
};
c.prototype.set = function(_) {
  return this.__opts__ = p(this.__opts__, _), this;
};
c.prototype.test = function(_) {
  if (this.__text_cache__ = _, this.__index__ = -1, !_.length)
    return !1;
  let e, i, s, h, o, n, r, a, m;
  if (this.re.schema_test.test(_)) {
    for (r = this.re.schema_search, r.lastIndex = 0; (e = r.exec(_)) !== null; )
      if (h = this.testSchemaAt(_, e[2], r.lastIndex), h) {
        this.__schema__ = e[2], this.__index__ = e.index + e[1].length, this.__last_index__ = e.index + e[0].length + h;
        break;
      }
  }
  return this.__opts__.fuzzyLink && this.__compiled__["http:"] && (a = _.search(this.re.host_fuzzy_test), a >= 0 && (this.__index__ < 0 || a < this.__index__) && (i = _.match(this.__opts__.fuzzyIP ? this.re.link_fuzzy : this.re.link_no_ip_fuzzy)) !== null && (o = i.index + i[1].length, (this.__index__ < 0 || o < this.__index__) && (this.__schema__ = "", this.__index__ = o, this.__last_index__ = i.index + i[0].length))), this.__opts__.fuzzyEmail && this.__compiled__["mailto:"] && (m = _.indexOf("@"), m >= 0 && (s = _.match(this.re.email_fuzzy)) !== null && (o = s.index + s[1].length, n = s.index + s[0].length, (this.__index__ < 0 || o < this.__index__ || o === this.__index__ && n > this.__last_index__) && (this.__schema__ = "mailto:", this.__index__ = o, this.__last_index__ = n))), this.__index__ >= 0;
};
c.prototype.pretest = function(_) {
  return this.re.pretest.test(_);
};
c.prototype.testSchemaAt = function(_, e, i) {
  return this.__compiled__[e.toLowerCase()] ? this.__compiled__[e.toLowerCase()].validate(_, i, this) : 0;
};
c.prototype.match = function(_) {
  const e = [];
  let i = 0;
  this.__index__ >= 0 && this.__text_cache__ === _ && (e.push(d(this, i)), i = this.__last_index__);
  let s = i ? _.slice(i) : _;
  for (; this.test(s); )
    e.push(d(this, i)), s = s.slice(this.__last_index__), i += this.__last_index__;
  return e.length ? e : null;
};
c.prototype.matchAtStart = function(_) {
  if (this.__text_cache__ = _, this.__index__ = -1, !_.length) return null;
  const e = this.re.schema_at_start.exec(_);
  if (!e) return null;
  const i = this.testSchemaAt(_, e[2], e[0].length);
  return i ? (this.__schema__ = e[2], this.__index__ = e.index + e[1].length, this.__last_index__ = e.index + e[0].length + i, d(this, 0)) : null;
};
c.prototype.tlds = function(_, e) {
  return _ = Array.isArray(_) ? _ : [_], e ? (this.__tlds__ = this.__tlds__.concat(_).sort().filter(function(i, s, h) {
    return i !== h[s - 1];
  }).reverse(), u(this), this) : (this.__tlds__ = _.slice(), this.__tlds_replaced__ = !0, u(this), this);
};
c.prototype.normalize = function(_) {
  _.schema || (_.url = "http://" + _.url), _.schema === "mailto:" && !/^mailto:/i.test(_.url) && (_.url = "mailto:" + _.url);
};
c.prototype.onCompile = function() {
};
export {
  c as default
};
