function inBlock(e, p, h) {
  return function(g, _) {
    for (; !g.eol(); ) {
      if (g.match(p)) {
        _.tokenize = inText;
        break;
      }
      g.next();
    }
    return h && (_.tokenize = h), e;
  };
}
function inLine(e) {
  return function(p, h) {
    for (; !p.eol(); ) p.next();
    return h.tokenize = inText, e;
  };
}
function inText(h, g) {
  function v(e) {
    return g.tokenize = e, e(h, g);
  }
  var y = h.sol(), b = h.next();
  switch (b) {
    case "{":
      return h.eat("/"), h.eatSpace(), h.eatWhile(/[^\s\u00a0=\"\'\/?(}]/), g.tokenize = inPlugin, "tag";
    case "_":
      if (h.eat("_")) return v(inBlock("strong", "__", inText));
      break;
    case "'":
      if (h.eat("'")) return v(inBlock("em", "''", inText));
      break;
    case "(":
      if (h.eat("(")) return v(inBlock("link", "))", inText));
      break;
    case "[":
      return v(inBlock("url", "]", inText));
    case "|":
      if (h.eat("|")) return v(inBlock("comment", "||"));
      break;
    case "-":
      if (h.eat("=")) return v(inBlock("header string", "=-", inText));
      if (h.eat("-")) return v(inBlock("error tw-deleted", "--", inText));
      break;
    case "=":
      if (h.match("==")) return v(inBlock("tw-underline", "===", inText));
      break;
    case ":":
      if (h.eat(":")) return v(inBlock("comment", "::"));
      break;
    case "^":
      return v(inBlock("tw-box", "^"));
    case "~":
      if (h.match("np~")) return v(inBlock("meta", "~/np~"));
      break;
  }
  if (y) switch (b) {
    case "!":
      return h.match("!!!!!") || h.match("!!!!") || h.match("!!!") || h.match("!!"), v(inLine("header string"));
    case "*":
    case "#":
    case "+":
      return v(inLine("tw-listitem bracket"));
  }
  return null;
}
var pluginName, type;
function inPlugin(e, p) {
  var h = e.next(), _ = e.peek();
  return h == "}" ? (p.tokenize = inText, "tag") : h == "(" || h == ")" ? "bracket" : h == "=" ? (type = "equals", _ == ">" && (e.next(), _ = e.peek()), /[\'\"]/.test(_) || (p.tokenize = inAttributeNoQuote()), "operator") : /[\'\"]/.test(h) ? (p.tokenize = inAttribute(h), p.tokenize(e, p)) : (e.eatWhile(/[^\s\u00a0=\"\'\/?]/), "keyword");
}
function inAttribute(e) {
  return function(p, m) {
    for (; !p.eol(); ) if (p.next() == e) {
      m.tokenize = inPlugin;
      break;
    }
    return "string";
  };
}
function inAttributeNoQuote() {
  return function(e, p) {
    for (; !e.eol(); ) {
      var m = e.next(), h = e.peek();
      if (m == " " || m == "," || /[ )}]/.test(h)) {
        p.tokenize = inPlugin;
        break;
      }
    }
    return "string";
  };
}
var curState, setStyle;
function pass() {
  for (var e = arguments.length - 1; e >= 0; e--) curState.cc.push(arguments[e]);
}
function cont() {
  return pass.apply(null, arguments), true;
}
function pushContext(e, p) {
  var m = curState.context && curState.context.noIndent;
  curState.context = {
    prev: curState.context,
    pluginName: e,
    indent: curState.indented,
    startOfLine: p,
    noIndent: m
  };
}
function popContext() {
  curState.context && (curState.context = curState.context.prev);
}
function element(e) {
  if (e == "openPlugin") return curState.pluginName = pluginName, cont(attributes, endplugin(curState.startOfLine));
  if (e == "closePlugin") {
    var p = false;
    return curState.context ? (p = curState.context.pluginName != pluginName, popContext()) : p = true, p && (setStyle = "error"), cont(endcloseplugin(p));
  } else if (e == "string") return (!curState.context || curState.context.name != "!cdata") && pushContext("!cdata"), curState.tokenize == inText && popContext(), cont();
  else return cont();
}
function endplugin(e) {
  return function(p) {
    return p == "selfclosePlugin" || p == "endPlugin" || p == "endPlugin" && pushContext(curState.pluginName, e), cont();
  };
}
function endcloseplugin(e) {
  return function(p) {
    return e && (setStyle = "error"), p == "endPlugin" ? cont() : pass();
  };
}
function attributes(e) {
  return e == "keyword" ? (setStyle = "attribute", cont(attributes)) : e == "equals" ? cont(attvalue, attributes) : pass();
}
function attvalue(e) {
  return e == "keyword" ? (setStyle = "string", cont()) : e == "string" ? cont(attvaluemaybe) : pass();
}
function attvaluemaybe(e) {
  return e == "string" ? cont(attvaluemaybe) : pass();
}
const tiki = {
  name: "tiki",
  startState: function() {
    return {
      tokenize: inText,
      cc: [],
      indented: 0,
      startOfLine: true,
      pluginName: null,
      context: null
    };
  },
  token: function(e, p) {
    if (e.sol() && (p.startOfLine = true, p.indented = e.indentation()), e.eatSpace()) return null;
    setStyle = type = pluginName = null;
    var m = p.tokenize(e, p);
    if ((m || type) && m != "comment") for (curState = p; !(p.cc.pop() || element)(type || m); ) ;
    return p.startOfLine = false, setStyle || m;
  },
  indent: function(e, p, m) {
    var h = e.context;
    if (h && h.noIndent) return 0;
    for (h && /^{\//.test(p) && (h = h.prev); h && !h.startOfLine; ) h = h.prev;
    return h ? h.indent + m.unit : 0;
  }
};
export {
  tiki
};
