"use client";
import { factorySpace as v } from "../../micromark-factory-space/index.js";
import { markdownLineEnding as j, markdownSpace as g, markdownLineEndingOrSpace as P } from "../../micromark-util-character/index.js";
import { EditMap as N } from "./edit-map.js";
import { gfmTableAlign as I } from "./infer.js";
function U() {
  return {
    flow: {
      null: {
        name: "table",
        tokenize: V,
        resolveAll: _
      }
    }
  };
}
function V(t, i, n) {
  const a = this;
  let s = 0, r = 0, l;
  return D;
  function D(e) {
    let R = a.events.length - 1;
    for (; R > -1; ) {
      const z = a.events[R][1].type;
      if (z === "lineEnding" || // Note: markdown-rs uses `whitespace` instead of `linePrefix`
      z === "linePrefix") R--;
      else break;
    }
    const M = R > -1 ? a.events[R][1].type : null, S = M === "tableHead" || M === "tableRow" ? H : p;
    return S === H && a.parser.lazy[a.now().line] ? n(e) : S(e);
  }
  function p(e) {
    return t.enter("tableHead"), t.enter("tableRow"), f(e);
  }
  function f(e) {
    return e === 124 || (l = !0, r += 1), d(e);
  }
  function d(e) {
    return e === null ? n(e) : j(e) ? r > 1 ? (r = 0, a.interrupt = !0, t.exit("tableRow"), t.enter("lineEnding"), t.consume(e), t.exit("lineEnding"), w) : n(e) : g(e) ? v(t, d, "whitespace")(e) : (r += 1, l && (l = !1, s += 1), e === 124 ? (t.enter("tableCellDivider"), t.consume(e), t.exit("tableCellDivider"), l = !0, d) : (t.enter("data"), b(e)));
  }
  function b(e) {
    return e === null || e === 124 || P(e) ? (t.exit("data"), d(e)) : (t.consume(e), e === 92 ? m : b);
  }
  function m(e) {
    return e === 92 || e === 124 ? (t.consume(e), b) : b(e);
  }
  function w(e) {
    return a.interrupt = !1, a.parser.lazy[a.now().line] ? n(e) : (t.enter("tableDelimiterRow"), l = !1, g(e) ? v(t, u, "linePrefix", a.parser.constructs.disable.null.includes("codeIndented") ? void 0 : 4)(e) : u(e));
  }
  function u(e) {
    return e === 45 || e === 58 ? A(e) : e === 124 ? (l = !0, t.enter("tableCellDivider"), t.consume(e), t.exit("tableCellDivider"), T) : h(e);
  }
  function T(e) {
    return g(e) ? v(t, A, "whitespace")(e) : A(e);
  }
  function A(e) {
    return e === 58 ? (r += 1, l = !0, t.enter("tableDelimiterMarker"), t.consume(e), t.exit("tableDelimiterMarker"), B) : e === 45 ? (r += 1, B(e)) : e === null || j(e) ? E(e) : h(e);
  }
  function B(e) {
    return e === 45 ? (t.enter("tableDelimiterFiller"), F(e)) : h(e);
  }
  function F(e) {
    return e === 45 ? (t.consume(e), F) : e === 58 ? (l = !0, t.exit("tableDelimiterFiller"), t.enter("tableDelimiterMarker"), t.consume(e), t.exit("tableDelimiterMarker"), C) : (t.exit("tableDelimiterFiller"), C(e));
  }
  function C(e) {
    return g(e) ? v(t, E, "whitespace")(e) : E(e);
  }
  function E(e) {
    return e === 124 ? u(e) : e === null || j(e) ? !l || s !== r ? h(e) : (t.exit("tableDelimiterRow"), t.exit("tableHead"), i(e)) : h(e);
  }
  function h(e) {
    return n(e);
  }
  function H(e) {
    return t.enter("tableRow"), o(e);
  }
  function o(e) {
    return e === 124 ? (t.enter("tableCellDivider"), t.consume(e), t.exit("tableCellDivider"), o) : e === null || j(e) ? (t.exit("tableRow"), i(e)) : g(e) ? v(t, o, "whitespace")(e) : (t.enter("data"), O(e));
  }
  function O(e) {
    return e === null || e === 124 || P(e) ? (t.exit("data"), o(e)) : (t.consume(e), e === 92 ? L : O);
  }
  function L(e) {
    return e === 92 || e === 124 ? (t.consume(e), O) : O(e);
  }
}
function _(t, i) {
  let n = -1, a = !0, s = 0, r = [0, 0, 0, 0], l = [0, 0, 0, 0], D = !1, p = 0, f, d, b;
  const m = new N();
  for (; ++n < t.length; ) {
    const w = t[n], u = w[1];
    w[0] === "enter" ? u.type === "tableHead" ? (D = !1, p !== 0 && (x(m, i, p, f, d), d = void 0, p = 0), f = {
      type: "table",
      start: Object.assign({}, u.start),
      // Note: correct end is set later.
      end: Object.assign({}, u.end)
    }, m.add(n, 0, [["enter", f, i]])) : u.type === "tableRow" || u.type === "tableDelimiterRow" ? (a = !0, b = void 0, r = [0, 0, 0, 0], l = [0, n + 1, 0, 0], D && (D = !1, d = {
      type: "tableBody",
      start: Object.assign({}, u.start),
      // Note: correct end is set later.
      end: Object.assign({}, u.end)
    }, m.add(n, 0, [["enter", d, i]])), s = u.type === "tableDelimiterRow" ? 2 : d ? 3 : 1) : s && (u.type === "data" || u.type === "tableDelimiterMarker" || u.type === "tableDelimiterFiller") ? (a = !1, l[2] === 0 && (r[1] !== 0 && (l[0] = l[1], b = k(m, i, r, s, void 0, b), r = [0, 0, 0, 0]), l[2] = n)) : u.type === "tableCellDivider" && (a ? a = !1 : (r[1] !== 0 && (l[0] = l[1], b = k(m, i, r, s, void 0, b)), r = l, l = [r[1], n, 0, 0])) : u.type === "tableHead" ? (D = !0, p = n) : u.type === "tableRow" || u.type === "tableDelimiterRow" ? (p = n, r[1] !== 0 ? (l[0] = l[1], b = k(m, i, r, s, n, b)) : l[1] !== 0 && (b = k(m, i, l, s, n, b)), s = 0) : s && (u.type === "data" || u.type === "tableDelimiterMarker" || u.type === "tableDelimiterFiller") && (l[3] = n);
  }
  for (p !== 0 && x(m, i, p, f, d), m.consume(i.events), n = -1; ++n < i.events.length; ) {
    const w = i.events[n];
    w[0] === "enter" && w[1].type === "table" && (w[1]._align = I(i.events, n));
  }
  return t;
}
function k(t, i, n, a, s, r) {
  const l = a === 1 ? "tableHeader" : a === 2 ? "tableDelimiter" : "tableData", D = "tableContent";
  n[0] !== 0 && (r.end = Object.assign({}, y(i.events, n[0])), t.add(n[0], 0, [["exit", r, i]]));
  const p = y(i.events, n[1]);
  if (r = {
    type: l,
    start: Object.assign({}, p),
    // Note: correct end is set later.
    end: Object.assign({}, p)
  }, t.add(n[1], 0, [["enter", r, i]]), n[2] !== 0) {
    const f = y(i.events, n[2]), d = y(i.events, n[3]), b = {
      type: D,
      start: Object.assign({}, f),
      end: Object.assign({}, d)
    };
    if (t.add(n[2], 0, [["enter", b, i]]), a !== 2) {
      const m = i.events[n[2]], w = i.events[n[3]];
      if (m[1].end = Object.assign({}, w[1].end), m[1].type = "chunkText", m[1].contentType = "text", n[3] > n[2] + 1) {
        const u = n[2] + 1, T = n[3] - n[2] - 1;
        t.add(u, T, []);
      }
    }
    t.add(n[3] + 1, 0, [["exit", b, i]]);
  }
  return s !== void 0 && (r.end = Object.assign({}, y(i.events, s)), t.add(s, 0, [["exit", r, i]]), r = void 0), r;
}
function x(t, i, n, a, s) {
  const r = [], l = y(i.events, n);
  s && (s.end = Object.assign({}, l), r.push(["exit", s, i])), a.end = Object.assign({}, l), r.push(["exit", a, i]), t.add(n + 1, 0, r);
}
function y(t, i) {
  const n = t[i], a = n[0] === "enter" ? "start" : "end";
  return n[1][a];
}
export {
  U as gfmTable
};
