import * as m from "./common/utils.js";
import { isString as c, assign as a } from "./common/utils.js";
import * as p from "./helpers/index.js";
import d from "./renderer.js";
import w from "./parser_core.js";
import y from "./parser_block.js";
import k from "./parser_inline.js";
import g from "../../linkify-it/index.js";
import s from "../../mdurl/lib/decode.js";
import b from "../../mdurl/lib/encode.js";
import f from "../../mdurl/lib/format.js";
import u from "../../mdurl/lib/parse.js";
import l from "../../punycode.js/punycode.es6.js";
import O from "./presets/default.js";
import E from "./presets/zero.js";
import I from "./presets/commonmark.js";
const A = {
  default: O,
  zero: E,
  commonmark: I
}, _ = /^(vbscript|javascript|file|data):/, x = /^data:image\/(gif|png|jpeg|webp);/;
function L(t) {
  const r = t.trim().toLowerCase();
  return _.test(r) ? x.test(r) : !0;
}
const h = ["http:", "https:", "mailto:"];
function z(t) {
  const r = u(t, !0);
  if (r.hostname && (!r.protocol || h.indexOf(r.protocol) >= 0))
    try {
      r.hostname = l.toASCII(r.hostname);
    } catch {
    }
  return b(f(r));
}
function R(t) {
  const r = u(t, !0);
  if (r.hostname && (!r.protocol || h.indexOf(r.protocol) >= 0))
    try {
      r.hostname = l.toUnicode(r.hostname);
    } catch {
    }
  return s(f(r), s.defaultChars + "%");
}
function n(t, r) {
  if (!(this instanceof n))
    return new n(t, r);
  r || c(t) || (r = t || {}, t = "default"), this.inline = new k(), this.block = new y(), this.core = new w(), this.renderer = new d(), this.linkify = new g(), this.validateLink = L, this.normalizeLink = z, this.normalizeLinkText = R, this.utils = m, this.helpers = a({}, p), this.options = {}, this.configure(t), r && this.set(r);
}
n.prototype.set = function(t) {
  return a(this.options, t), this;
};
n.prototype.configure = function(t) {
  const r = this;
  if (c(t)) {
    const o = t;
    if (t = A[o], !t)
      throw new Error('Wrong `markdown-it` preset "' + o + '", check name');
  }
  if (!t)
    throw new Error("Wrong `markdown-it` preset, can't be empty");
  return t.options && r.set(t.options), t.components && Object.keys(t.components).forEach(function(o) {
    t.components[o].rules && r[o].ruler.enableOnly(t.components[o].rules), t.components[o].rules2 && r[o].ruler2.enableOnly(t.components[o].rules2);
  }), this;
};
n.prototype.enable = function(t, r) {
  let o = [];
  Array.isArray(t) || (t = [t]), ["core", "block", "inline"].forEach(function(e) {
    o = o.concat(this[e].ruler.enable(t, !0));
  }, this), o = o.concat(this.inline.ruler2.enable(t, !0));
  const i = t.filter(function(e) {
    return o.indexOf(e) < 0;
  });
  if (i.length && !r)
    throw new Error("MarkdownIt. Failed to enable unknown rule(s): " + i);
  return this;
};
n.prototype.disable = function(t, r) {
  let o = [];
  Array.isArray(t) || (t = [t]), ["core", "block", "inline"].forEach(function(e) {
    o = o.concat(this[e].ruler.disable(t, !0));
  }, this), o = o.concat(this.inline.ruler2.disable(t, !0));
  const i = t.filter(function(e) {
    return o.indexOf(e) < 0;
  });
  if (i.length && !r)
    throw new Error("MarkdownIt. Failed to disable unknown rule(s): " + i);
  return this;
};
n.prototype.use = function(t) {
  const r = [this].concat(Array.prototype.slice.call(arguments, 1));
  return t.apply(t, r), this;
};
n.prototype.parse = function(t, r) {
  if (typeof t != "string")
    throw new Error("Input data should be a String");
  const o = new this.core.State(t, this, r);
  return this.core.process(o), o.tokens;
};
n.prototype.render = function(t, r) {
  return r = r || {}, this.renderer.render(this.parse(t, r), this.options, r);
};
n.prototype.parseInline = function(t, r) {
  const o = new this.core.State(t, this, r);
  return o.inlineMode = !0, this.core.process(o), o.tokens;
};
n.prototype.renderInline = function(t, r) {
  return r = r || {}, this.renderer.render(this.parseInline(t, r), this.options, r);
};
export {
  n as default
};
