"use client";
import { ok as k } from "../../devlop/lib/default.js";
import { markdownTable as R } from "../../markdown-table/index.js";
import { handle as D } from "../../mdast-util-to-markdown/lib/handle/index.js";
function M() {
  return {
    enter: {
      table: v,
      tableData: g,
      tableHeader: g,
      tableRow: _
    },
    exit: {
      codeText: A,
      table: y,
      tableData: u,
      tableHeader: u,
      tableRow: u
    }
  };
}
function v(e) {
  const l = e._align;
  this.enter(
    {
      type: "table",
      align: l.map(function(o) {
        return o === "none" ? null : o;
      }),
      children: []
    },
    e
  ), this.data.inTable = !0;
}
function y(e) {
  this.exit(e), this.data.inTable = void 0;
}
function _(e) {
  this.enter({ type: "tableRow", children: [] }, e);
}
function u(e) {
  this.exit(e);
}
function g(e) {
  this.enter({ type: "tableCell", children: [] }, e);
}
function A(e) {
  let l = this.resume();
  this.data.inTable && (l = l.replace(/\\([\\|])/g, B));
  const o = this.stack[this.stack.length - 1];
  k(o.type === "inlineCode"), o.value = l, this.exit(e);
}
function B(e, l) {
  return l === "|" ? l : e;
}
function z(e) {
  const l = e || {}, o = l.tableCellPadding, C = l.tablePipeAlign, x = l.stringLength, h = o ? " " : "|";
  return {
    unsafe: [
      { character: "\r", inConstruct: "tableCell" },
      { character: `
`, inConstruct: "tableCell" },
      // A pipe, when followed by a tab or space (padding), or a dash or colon
      // (unpadded delimiter row), could result in a table.
      { atBreak: !0, character: "|", after: "[	 :-]" },
      // A pipe in a cell must be encoded.
      { character: "|", inConstruct: "tableCell" },
      // A colon must be followed by a dash, in which case it could start a
      // delimiter row.
      { atBreak: !0, character: ":", after: "-" },
      // A delimiter row can also start with a dash, when followed by more
      // dashes, a colon, or a pipe.
      // This is a stricter version than the built in check for lists, thematic
      // breaks, and setex heading underlines though:
      // <https://github.com/syntax-tree/mdast-util-to-markdown/blob/51a2038/lib/unsafe.js#L57>
      { atBreak: !0, character: "-", after: "[:|-]" }
    ],
    handlers: {
      inlineCode: m,
      table: T,
      tableCell: b,
      tableRow: p
    }
  };
  function T(t, r, a, n) {
    return d(w(t, a, n), t.align);
  }
  function p(t, r, a, n) {
    const i = f(t, a, n), c = d([i]);
    return c.slice(0, c.indexOf(`
`));
  }
  function b(t, r, a, n) {
    const i = a.enter("tableCell"), c = a.enter("phrasing"), s = a.containerPhrasing(t, {
      ...n,
      before: h,
      after: h
    });
    return c(), i(), s;
  }
  function d(t, r) {
    return R(t, {
      align: r,
      // @ts-expect-error: `markdown-table` types should support `null`.
      alignDelimiters: C,
      // @ts-expect-error: `markdown-table` types should support `null`.
      padding: o,
      // @ts-expect-error: `markdown-table` types should support `null`.
      stringLength: x
    });
  }
  function w(t, r, a) {
    const n = t.children;
    let i = -1;
    const c = [], s = r.enter("table");
    for (; ++i < n.length; )
      c[i] = f(n[i], r, a);
    return s(), c;
  }
  function f(t, r, a) {
    const n = t.children;
    let i = -1;
    const c = [], s = r.enter("tableRow");
    for (; ++i < n.length; )
      c[i] = b(n[i], t, r, a);
    return s(), c;
  }
  function m(t, r, a) {
    let n = D.inlineCode(t, r, a);
    return a.stack.includes("tableCell") && (n = n.replace(/\|/g, "\\$&")), n;
  }
}
export {
  M as gfmTableFromMarkdown,
  z as gfmTableToMarkdown
};
