import { isSpace as v } from "../common/utils.js";
function T(r, c) {
  const u = r.eMarks[c];
  let i = r.bMarks[c] + r.tShift[c];
  const e = r.src.charCodeAt(i++);
  if (e !== 42 && e !== 45 && e !== 43)
    return -1;
  if (i < u) {
    const l = r.src.charCodeAt(i);
    if (!v(l))
      return -1;
  }
  return i;
}
function y(r, c) {
  const u = r.bMarks[c] + r.tShift[c], i = r.eMarks[c];
  let e = u;
  if (e + 1 >= i)
    return -1;
  let l = r.src.charCodeAt(e++);
  if (l < 48 || l > 57)
    return -1;
  for (; ; ) {
    if (e >= i)
      return -1;
    if (l = r.src.charCodeAt(e++), l >= 48 && l <= 57) {
      if (e - u >= 10)
        return -1;
      continue;
    }
    if (l === 41 || l === 46)
      break;
    return -1;
  }
  return e < i && (l = r.src.charCodeAt(e), !v(l)) ? -1 : e;
}
function N(r, c) {
  const u = r.level + 2;
  for (let i = c + 2, e = r.tokens.length - 2; i < e; i++)
    r.tokens[i].level === u && r.tokens[i].type === "paragraph_open" && (r.tokens[i + 2].hidden = !0, r.tokens[i].hidden = !0, i += 2);
}
function a(r, c, u, i) {
  let e, l, m, o, n = c, x = !0;
  if (r.sCount[n] - r.blkIndent >= 4 || r.listIndent >= 0 && r.sCount[n] - r.listIndent >= 4 && r.sCount[n] < r.blkIndent)
    return !1;
  let C = !1;
  i && r.parentType === "paragraph" && r.sCount[n] >= r.blkIndent && (C = !0);
  let k, b, f;
  if ((f = y(r, n)) >= 0) {
    if (k = !0, m = r.bMarks[n] + r.tShift[n], b = Number(r.src.slice(m, f - 1)), C && b !== 1) return !1;
  } else if ((f = T(r, n)) >= 0)
    k = !1;
  else
    return !1;
  if (C && r.skipSpaces(f) >= r.eMarks[n])
    return !1;
  if (i)
    return !0;
  const d = r.src.charCodeAt(f - 1), E = r.tokens.length;
  k ? (o = r.push("ordered_list_open", "ol", 1), b !== 1 && (o.attrs = [["start", b]])) : o = r.push("bullet_list_open", "ul", 1);
  const S = [n, 0];
  o.map = S, o.markup = String.fromCharCode(d);
  let I = !1;
  const M = r.md.block.ruler.getRules("list"), P = r.parentType;
  for (r.parentType = "list"; n < u; ) {
    l = f, e = r.eMarks[n];
    const t = r.sCount[n] + f - (r.bMarks[n] + r.tShift[n]);
    let p = t;
    for (; l < e; ) {
      const h = r.src.charCodeAt(l);
      if (h === 9)
        p += 4 - (p + r.bsCount[n]) % 4;
      else if (h === 32)
        p++;
      else
        break;
      l++;
    }
    const g = l;
    let s;
    g >= e ? s = 1 : s = p - t, s > 4 && (s = 1);
    const w = t + s;
    o = r.push("list_item_open", "li", 1), o.markup = String.fromCharCode(d);
    const _ = [n, 0];
    o.map = _, k && (o.info = r.src.slice(m, f - 1));
    const B = r.tight, O = r.tShift[n], R = r.sCount[n], z = r.listIndent;
    if (r.listIndent = r.blkIndent, r.blkIndent = w, r.tight = !0, r.tShift[n] = g - r.bMarks[n], r.sCount[n] = p, g >= e && r.isEmpty(n + 1) ? r.line = Math.min(r.line + 2, u) : r.md.block.tokenize(r, n, u, !0), (!r.tight || I) && (x = !1), I = r.line - n > 1 && r.isEmpty(r.line - 1), r.blkIndent = r.listIndent, r.listIndent = z, r.tShift[n] = O, r.sCount[n] = R, r.tight = B, o = r.push("list_item_close", "li", -1), o.markup = String.fromCharCode(d), n = r.line, _[1] = n, n >= u || r.sCount[n] < r.blkIndent || r.sCount[n] - r.blkIndent >= 4)
      break;
    let A = !1;
    for (let h = 0, D = M.length; h < D; h++)
      if (M[h](r, n, u, !0)) {
        A = !0;
        break;
      }
    if (A)
      break;
    if (k) {
      if (f = y(r, n), f < 0)
        break;
      m = r.bMarks[n] + r.tShift[n];
    } else if (f = T(r, n), f < 0)
      break;
    if (d !== r.src.charCodeAt(f - 1))
      break;
  }
  return k ? o = r.push("ordered_list_close", "ol", -1) : o = r.push("bullet_list_close", "ul", -1), o.markup = String.fromCharCode(d), S[1] = n, r.line = n, r.parentType = P, x && N(r, E), !0;
}
export {
  a as default
};
