const C = [
  [/^(<!--)(.+)(-->)$/, !1],
  [/^(\/\*)(.+)(\*\/)$/, !1],
  [/^(\/\/|["'#]|;{1,2}|%{1,2}|--)(.*)$/, !0],
  /**
   * for multi-line comments like this
   */
  [/^(\*)(.+)$/, !0]
];
function T(e, o, c) {
  const s = [];
  for (const n of e) {
    if (c === "v3") {
      const l = n.children.flatMap((a, t) => {
        if (a.type !== "element")
          return a;
        const h = a.children[0];
        if (h.type !== "text")
          return a;
        const f = t === n.children.length - 1;
        if (!k(h.value, f))
          return a;
        const i = h.value.split(/(\s+\/\/)/);
        if (i.length <= 1)
          return a;
        let u = [i[0]];
        for (let d = 1; d < i.length; d += 2)
          u.push(i[d] + (i[d + 1] || ""));
        return u = u.filter(Boolean), u.length <= 1 ? a : u.map((d) => ({
          ...a,
          children: [
            {
              type: "text",
              value: d
            }
          ]
        }));
      });
      l.length !== n.children.length && (n.children = l);
    }
    const r = n.children;
    let m = r.length - 1;
    c === "v1" ? m = 0 : o && (m = r.length - 2);
    for (let l = Math.max(m, 0); l < r.length; l++) {
      const a = r[l];
      if (a.type !== "element")
        continue;
      const t = a.children.at(0);
      if ((t == null ? void 0 : t.type) !== "text")
        continue;
      const h = l === r.length - 1;
      let f = k(t.value, h), g;
      if (!f && l > 0 && t.value.trim().startsWith("[!code")) {
        const i = r[l - 1];
        if ((i == null ? void 0 : i.type) === "element") {
          const u = i.children.at(0);
          if ((u == null ? void 0 : u.type) === "text" && u.value.includes("//")) {
            const d = u.value + t.value, v = k(d, h);
            if (v) {
              f = v, s.push({
                info: v,
                line: n,
                token: i,
                // Use the previous token as the main token
                isLineCommentOnly: r.length === 2 && i.children.length === 1 && a.children.length === 1,
                isJsxStyle: !1,
                additionalTokens: [a]
                // Current token is the additional one
              });
              continue;
            }
          }
        }
      }
      if (f)
        if (o && !h && l !== 0) {
          const i = x(r[l - 1], "{") && x(r[l + 1], "}");
          s.push({
            info: f,
            line: n,
            token: a,
            isLineCommentOnly: r.length === 3 && a.children.length === 1,
            isJsxStyle: i,
            additionalTokens: g
          });
        } else
          s.push({
            info: f,
            line: n,
            token: a,
            isLineCommentOnly: r.length === 1 && a.children.length === 1,
            isJsxStyle: !1,
            additionalTokens: g
          });
    }
  }
  return s;
}
function x(e, o) {
  if (e.type !== "element")
    return !1;
  const c = e.children[0];
  return c.type !== "text" ? !1 : c.value.trim() === o;
}
function k(e, o) {
  let c = e.trimStart();
  const s = e.length - c.length;
  c = c.trimEnd();
  const n = e.length - c.length - s;
  for (const [r, m] of C) {
    if (m && !o)
      continue;
    const l = r.exec(c);
    if (l)
      return [
        " ".repeat(s) + l[1],
        l[2],
        l[3] ? l[3] + " ".repeat(n) : void 0
      ];
  }
}
function A(e) {
  const o = e.match(/(?:\/\/|["'#]|;{1,2}|%{1,2}|--)(\s*)$/);
  return o && o[1].trim().length === 0 ? e.slice(0, o.index) : e;
}
function L(e, o, c, s) {
  return s == null && (s = "v3"), {
    name: e,
    code(n) {
      const r = n.children.filter((t) => t.type === "element"), m = [];
      n.data ?? (n.data = {});
      const l = n.data;
      l._shiki_notation ?? (l._shiki_notation = T(r, ["jsx", "tsx"].includes(this.options.lang), s));
      const a = l._shiki_notation;
      for (const t of a) {
        if (t.info[1].length === 0)
          continue;
        let h = r.indexOf(t.line);
        t.isLineCommentOnly && s !== "v1" && h++;
        let f = !1;
        if (t.info[1] = t.info[1].replace(o, (...i) => c.call(this, i, t.line, t.token, r, h) ? (f = !0, "") : i[0]), !f)
          continue;
        s === "v1" && (t.info[1] = A(t.info[1]));
        const g = t.info[1].trim().length === 0;
        if (g && (t.info[1] = ""), g && t.isLineCommentOnly)
          m.push(t.line);
        else if (g && t.isJsxStyle)
          t.line.children.splice(t.line.children.indexOf(t.token) - 1, 3);
        else if (g) {
          if (t.additionalTokens)
            for (let i = t.additionalTokens.length - 1; i >= 0; i--) {
              const u = t.additionalTokens[i], d = t.line.children.indexOf(u);
              d !== -1 && t.line.children.splice(d, 1);
            }
          t.line.children.splice(t.line.children.indexOf(t.token), 1);
        } else {
          const i = t.token.children[0];
          if (i.type === "text" && (i.value = t.info.join(""), t.additionalTokens))
            for (const u of t.additionalTokens) {
              const d = u.children[0];
              (d == null ? void 0 : d.type) === "text" && (d.value = "");
            }
        }
      }
      for (const t of m) {
        const h = n.children.indexOf(t), f = n.children[h + 1];
        let g = 1;
        (f == null ? void 0 : f.type) === "text" && (f == null ? void 0 : f.value) === `
` && (g = 2), n.children.splice(h, g);
      }
    }
  };
}
function M(e) {
  if (!e)
    return null;
  const o = e.match(/\{([\d,-]+)\}/);
  return o ? o[1].split(",").flatMap((s) => {
    const n = s.split("-").map((r) => Number.parseInt(r, 10));
    return n.length === 1 ? [n[0]] : Array.from({ length: n[1] - n[0] + 1 }, (r, m) => n[0] + m);
  }) : null;
}
const p = /* @__PURE__ */ Symbol("highlighted-lines");
function _(e = {}) {
  const { className: o = "highlighted", zeroIndexed: c = !1 } = e;
  return {
    name: "@shikijs/transformers:meta-highlight",
    line(s, n) {
      var a;
      if (!((a = this.options.meta) != null && a.__raw))
        return;
      const r = this.meta;
      r[p] ?? (r[p] = M(this.options.meta.__raw));
      const m = r[p] ?? [], l = c ? n - 1 : n;
      return m.includes(l) && this.addClassToHast(s, o), s;
    }
  };
}
function O(e) {
  return e.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
function y(e = {}, o = "@shikijs/transformers:notation-map") {
  const {
    classMap: c = {},
    classActivePre: s = void 0,
    classActiveCode: n = void 0
  } = e;
  return L(
    o,
    new RegExp(`#?\\s*\\[!code (${Object.keys(c).map(O).join("|")})(:\\d+)?\\]`, "gi"),
    function([r, m, l = ":1"], a, t, h, f) {
      const g = Number.parseInt(l.slice(1), 10);
      for (let i = f; i < Math.min(f + g, h.length); i++)
        this.addClassToHast(h[i], c[m]);
      return s && this.addClassToHast(this.pre, s), n && this.addClassToHast(this.code, n), !0;
    },
    e.matchAlgorithm
  );
}
function j(e = {}) {
  const {
    classLineAdd: o = "diff add",
    classLineRemove: c = "diff remove",
    classActivePre: s = "has-diff",
    classActiveCode: n
  } = e;
  return y(
    {
      classMap: {
        "++": o,
        "--": c
      },
      classActivePre: s,
      classActiveCode: n,
      matchAlgorithm: e.matchAlgorithm
    },
    "@shikijs/transformers:notation-diff"
  );
}
function N(e = {}) {
  const {
    classActiveLine: o = "focused",
    classActivePre: c = "has-focused",
    classActiveCode: s
  } = e;
  return y(
    {
      classMap: {
        focus: o
      },
      classActivePre: c,
      classActiveCode: s,
      matchAlgorithm: e.matchAlgorithm
    },
    "@shikijs/transformers:notation-focus"
  );
}
function S(e = {}) {
  const {
    classActiveLine: o = "highlighted",
    classActivePre: c = "has-highlighted",
    classActiveCode: s
  } = e;
  return y(
    {
      classMap: {
        highlight: o,
        hl: o
      },
      classActivePre: c,
      classActiveCode: s,
      matchAlgorithm: e.matchAlgorithm
    },
    "@shikijs/transformers:notation-highlight"
  );
}
export {
  L as createCommentNotationTransformer,
  M as parseMetaHighlightString,
  _ as transformerMetaHighlight,
  j as transformerNotationDiff,
  N as transformerNotationFocus,
  S as transformerNotationHighlight,
  y as transformerNotationMap
};
