var TOKEN_STYLES = {
  addition: "inserted",
  attributes: "propertyName",
  bold: "strong",
  cite: "keyword",
  code: "monospace",
  definitionList: "list",
  deletion: "deleted",
  div: "punctuation",
  em: "emphasis",
  footnote: "variable",
  footCite: "qualifier",
  header: "heading",
  html: "comment",
  image: "atom",
  italic: "emphasis",
  link: "link",
  linkDefinition: "link",
  list1: "list",
  list2: "list.special",
  list3: "list",
  notextile: "string.special",
  pre: "operator",
  p: "content",
  quote: "bracket",
  span: "quote",
  specialChar: "character",
  strong: "strong",
  sub: "content.special",
  sup: "content.special",
  table: "variableName.special",
  tableHeading: "operator"
};
function startNewLine(e, a) {
  a.mode = Modes.newLayout, a.tableHeading = false, a.layoutType === "definitionList" && a.spanningLayout && e.match(RE("definitionListEnd"), false) && (a.spanningLayout = false);
}
function handlePhraseModifier(a, o, c) {
  if (c === "_") return a.eat("_") ? togglePhraseModifier(a, o, "italic", /__/, 2) : togglePhraseModifier(a, o, "em", /_/, 1);
  if (c === "*") return a.eat("*") ? togglePhraseModifier(a, o, "bold", /\*\*/, 2) : togglePhraseModifier(a, o, "strong", /\*/, 1);
  if (c === "[") return a.match(/\d+\]/) && (o.footCite = true), tokenStyles(o);
  if (c === "(" && a.match(/^(r|tm|c)\)/)) return TOKEN_STYLES.specialChar;
  if (c === "<" && a.match(/(\w+)[^>]+>[^<]+<\/\1>/)) return TOKEN_STYLES.html;
  if (c === "?" && a.eat("?")) return togglePhraseModifier(a, o, "cite", /\?\?/, 2);
  if (c === "=" && a.eat("=")) return togglePhraseModifier(a, o, "notextile", /==/, 2);
  if (c === "-" && !a.eat("-")) return togglePhraseModifier(a, o, "deletion", /-/, 1);
  if (c === "+") return togglePhraseModifier(a, o, "addition", /\+/, 1);
  if (c === "~") return togglePhraseModifier(a, o, "sub", /~/, 1);
  if (c === "^") return togglePhraseModifier(a, o, "sup", /\^/, 1);
  if (c === "%") return togglePhraseModifier(a, o, "span", /%/, 1);
  if (c === "@") return togglePhraseModifier(a, o, "code", /@/, 1);
  if (c === "!") {
    var l = togglePhraseModifier(a, o, "image", /(?:\([^\)]+\))?!/, 1);
    return a.match(/^:\S+/), l;
  }
  return tokenStyles(o);
}
function togglePhraseModifier(e, a, o, s, c) {
  var l = e.pos > c ? e.string.charAt(e.pos - c - 1) : null, u = e.peek();
  if (a[o]) {
    if ((!u || /\W/.test(u)) && l && /\S/.test(l)) {
      var d = tokenStyles(a);
      return a[o] = false, d;
    }
  } else (!l || /\W/.test(l)) && u && /\S/.test(u) && e.match(/* @__PURE__ */ RegExp("^.*\\S" + s.source + "(?:\\W|$)"), false) && (a[o] = true, a.mode = Modes.attributes);
  return tokenStyles(a);
}
function tokenStyles(a) {
  var o = textileDisabled(a);
  if (o) return o;
  var s = [];
  return a.layoutType && s.push(TOKEN_STYLES[a.layoutType]), s = s.concat(activeStyles(a, "addition", "bold", "cite", "code", "deletion", "em", "footCite", "image", "italic", "link", "span", "strong", "sub", "sup", "table", "tableHeading")), a.layoutType === "header" && s.push(TOKEN_STYLES.header + "-" + a.header), s.length ? s.join(" ") : null;
}
function textileDisabled(a) {
  var o = a.layoutType;
  switch (o) {
    case "notextile":
    case "code":
    case "pre":
      return TOKEN_STYLES[o];
    default:
      return a.notextile ? TOKEN_STYLES.notextile + (o ? " " + TOKEN_STYLES[o] : "") : null;
  }
}
function activeStyles(a) {
  for (var o = [], s = 1; s < arguments.length; ++s) a[arguments[s]] && o.push(TOKEN_STYLES[arguments[s]]);
  return o;
}
function blankLine(e) {
  var a = e.spanningLayout, o = e.layoutType;
  for (var s in e) e.hasOwnProperty(s) && delete e[s];
  e.mode = Modes.newLayout, a && (e.layoutType = o, e.spanningLayout = true);
}
var REs = {
  cache: {},
  single: {
    bc: "bc",
    bq: "bq",
    definitionList: /- .*?:=+/,
    definitionListEnd: /.*=:\s*$/,
    div: "div",
    drawTable: /\|.*\|/,
    foot: /fn\d+/,
    header: /h[1-6]/,
    html: /\s*<(?:\/)?(\w+)(?:[^>]+)?>(?:[^<]+<\/\1>)?/,
    link: /[^"]+":\S/,
    linkDefinition: /\[[^\s\]]+\]\S+/,
    list: /(?:#+|\*+)/,
    notextile: "notextile",
    para: "p",
    pre: "pre",
    table: "table",
    tableCellAttributes: /[\/\\]\d+/,
    tableHeading: /\|_\./,
    tableText: /[^"_\*\[\(\?\+~\^%@|-]+/,
    text: /[^!"_=\*\[\(<\?\+~\^%@-]+/
  },
  attributes: {
    align: /(?:<>|<|>|=)/,
    selector: /\([^\(][^\)]+\)/,
    lang: /\[[^\[\]]+\]/,
    pad: /(?:\(+|\)+){1,2}/,
    css: /\{[^\}]+\}/
  },
  createRe: function(e) {
    switch (e) {
      case "drawTable":
        return REs.makeRe("^", REs.single.drawTable, "$");
      case "html":
        return REs.makeRe("^", REs.single.html, "(?:", REs.single.html, ")*", "$");
      case "linkDefinition":
        return REs.makeRe("^", REs.single.linkDefinition, "$");
      case "listLayout":
        return REs.makeRe("^", REs.single.list, RE("allAttributes"), "*\\s+");
      case "tableCellAttributes":
        return REs.makeRe("^", REs.choiceRe(REs.single.tableCellAttributes, RE("allAttributes")), "+\\.");
      case "type":
        return REs.makeRe("^", RE("allTypes"));
      case "typeLayout":
        return REs.makeRe("^", RE("allTypes"), RE("allAttributes"), "*\\.\\.?", "(\\s+|$)");
      case "attributes":
        return REs.makeRe("^", RE("allAttributes"), "+");
      case "allTypes":
        return REs.choiceRe(REs.single.div, REs.single.foot, REs.single.header, REs.single.bc, REs.single.bq, REs.single.notextile, REs.single.pre, REs.single.table, REs.single.para);
      case "allAttributes":
        return REs.choiceRe(REs.attributes.selector, REs.attributes.css, REs.attributes.lang, REs.attributes.align, REs.attributes.pad);
      default:
        return REs.makeRe("^", REs.single[e]);
    }
  },
  makeRe: function() {
    for (var e = "", a = 0; a < arguments.length; ++a) {
      var o = arguments[a];
      e += typeof o == "string" ? o : o.source;
    }
    return new RegExp(e);
  },
  choiceRe: function() {
    for (var e = [arguments[0]], a = 1; a < arguments.length; ++a) e[a * 2 - 1] = "|", e[a * 2] = arguments[a];
    return e.unshift("(?:"), e.push(")"), REs.makeRe.apply(null, e);
  }
};
function RE(e) {
  return REs.cache[e] || (REs.cache[e] = REs.createRe(e));
}
var Modes = {
  newLayout: function(e, a) {
    if (e.match(RE("typeLayout"), false)) return a.spanningLayout = false, (a.mode = Modes.blockType)(e, a);
    var o;
    return textileDisabled(a) || (e.match(RE("listLayout"), false) ? o = Modes.list : e.match(RE("drawTable"), false) ? o = Modes.table : e.match(RE("linkDefinition"), false) ? o = Modes.linkDefinition : e.match(RE("definitionList")) ? o = Modes.definitionList : e.match(RE("html"), false) && (o = Modes.html)), (a.mode = o || Modes.text)(e, a);
  },
  blockType: function(e, a) {
    var o, s;
    if (a.layoutType = null, o = e.match(RE("type"))) s = o[0];
    else return (a.mode = Modes.text)(e, a);
    return (o = s.match(RE("header"))) ? (a.layoutType = "header", a.header = parseInt(o[0][1])) : s.match(RE("bq")) ? a.layoutType = "quote" : s.match(RE("bc")) ? a.layoutType = "code" : s.match(RE("foot")) ? a.layoutType = "footnote" : s.match(RE("notextile")) ? a.layoutType = "notextile" : s.match(RE("pre")) ? a.layoutType = "pre" : s.match(RE("div")) ? a.layoutType = "div" : s.match(RE("table")) && (a.layoutType = "table"), a.mode = Modes.attributes, tokenStyles(a);
  },
  text: function(e, a) {
    if (e.match(RE("text"))) return tokenStyles(a);
    var s = e.next();
    return s === '"' ? (a.mode = Modes.link)(e, a) : handlePhraseModifier(e, a, s);
  },
  attributes: function(a, o) {
    return o.mode = Modes.layoutLength, a.match(RE("attributes")) ? TOKEN_STYLES.attributes : tokenStyles(o);
  },
  layoutLength: function(e, a) {
    return e.eat(".") && e.eat(".") && (a.spanningLayout = true), a.mode = Modes.text, tokenStyles(a);
  },
  list: function(e, a) {
    a.listDepth = e.match(RE("list"))[0].length;
    var o = (a.listDepth - 1) % 3;
    return o ? o === 1 ? a.layoutType = "list2" : a.layoutType = "list3" : a.layoutType = "list1", a.mode = Modes.attributes, tokenStyles(a);
  },
  link: function(a, o) {
    return o.mode = Modes.text, a.match(RE("link")) ? (a.match(/\S+/), TOKEN_STYLES.link) : tokenStyles(o);
  },
  linkDefinition: function(a) {
    return a.skipToEnd(), TOKEN_STYLES.linkDefinition;
  },
  definitionList: function(e, a) {
    return e.match(RE("definitionList")), a.layoutType = "definitionList", e.match(/\s*$/) ? a.spanningLayout = true : a.mode = Modes.attributes, tokenStyles(a);
  },
  html: function(a) {
    return a.skipToEnd(), TOKEN_STYLES.html;
  },
  table: function(e, a) {
    return a.layoutType = "table", (a.mode = Modes.tableCell)(e, a);
  },
  tableCell: function(e, a) {
    return e.match(RE("tableHeading")) ? a.tableHeading = true : e.eat("|"), a.mode = Modes.tableCellAttributes, tokenStyles(a);
  },
  tableCellAttributes: function(a, o) {
    return o.mode = Modes.tableText, a.match(RE("tableCellAttributes")) ? TOKEN_STYLES.attributes : tokenStyles(o);
  },
  tableText: function(e, a) {
    return e.match(RE("tableText")) ? tokenStyles(a) : e.peek() === "|" ? (a.mode = Modes.tableCell, tokenStyles(a)) : handlePhraseModifier(e, a, e.next());
  }
};
const textile = {
  name: "textile",
  startState: function() {
    return { mode: Modes.newLayout };
  },
  token: function(e, o) {
    return e.sol() && startNewLine(e, o), o.mode(e, o);
  },
  blankLine
};
export {
  textile as t
};
