import { Scalar as S } from "./accelbyte-widgets651.mjs";
function C(h, f, m) {
  const i = h.offset, l = E(h, f, m);
  if (!l)
    return { value: "", type: null, comment: "", range: [i, i, i] };
  const u = l.mode === ">" ? S.BLOCK_FOLDED : S.BLOCK_LITERAL, t = h.source ? I(h.source) : [];
  let g = t.length;
  for (let e = t.length - 1; e >= 0; --e) {
    const c = t[e][1];
    if (c === "" || c === "\r")
      g = e;
    else
      break;
  }
  if (g === 0) {
    const e = l.chomp === "+" && t.length > 0 ? `
`.repeat(Math.max(1, t.length - 1)) : "";
    let c = i + l.length;
    return h.source && (c += h.source.length), { value: e, type: u, comment: l.comment, range: [i, c, c] };
  }
  let r = h.indent + l.indent, p = h.offset + l.length, k = 0;
  for (let e = 0; e < g; ++e) {
    const [c, a] = t[e];
    if (a === "" || a === "\r")
      l.indent === 0 && c.length > r && (r = c.length);
    else {
      if (c.length < r) {
        const b = "Block scalars with more-indented leading empty lines must use an explicit indentation indicator";
        m(p + c.length, "MISSING_CHAR", b);
      }
      l.indent === 0 && (r = c.length), k = e;
      break;
    }
    p += c.length + a.length + 1;
  }
  for (let e = t.length - 1; e >= g; --e)
    t[e][0].length > r && (g = e + 1);
  let o = "", n = "", s = !1;
  for (let e = 0; e < k; ++e)
    o += t[e][0].slice(r) + `
`;
  for (let e = k; e < g; ++e) {
    let [c, a] = t[e];
    p += c.length + a.length + 1;
    const b = a[a.length - 1] === "\r";
    if (b && (a = a.slice(0, -1)), a && c.length < r) {
      const B = `Block scalar lines must not be less indented than their ${l.indent ? "explicit indentation indicator" : "first line"}`;
      m(p - a.length - (b ? 2 : 1), "BAD_INDENT", B), c = "";
    }
    u === S.BLOCK_LITERAL ? (o += n + c.slice(r) + a, n = `
`) : c.length > r || a[0] === "	" ? (n === " " ? n = `
` : !s && n === `
` && (n = `

`), o += n + c.slice(r) + a, n = `
`, s = !0) : a === "" ? n === `
` ? o += `
` : n = `
` : (o += n + a, n = " ", s = !1);
  }
  switch (l.chomp) {
    case "-":
      break;
    case "+":
      for (let e = g; e < t.length; ++e)
        o += `
` + t[e][0].slice(r);
      o[o.length - 1] !== `
` && (o += `
`);
      break;
    default:
      o += `
`;
  }
  const d = i + l.length + h.source.length;
  return { value: o, type: u, comment: l.comment, range: [i, d, d] };
}
function E({ offset: h, props: f }, m, i) {
  if (f[0].type !== "block-scalar-header")
    return i(f[0], "IMPOSSIBLE", "Block scalar header not found"), null;
  const { source: l } = f[0], u = l[0];
  let t = 0, g = "", r = -1;
  for (let n = 1; n < l.length; ++n) {
    const s = l[n];
    if (!g && (s === "-" || s === "+"))
      g = s;
    else {
      const d = Number(s);
      !t && d ? t = d : r === -1 && (r = h + n);
    }
  }
  r !== -1 && i(r, "UNEXPECTED_TOKEN", `Block scalar header includes extra characters: ${l}`);
  let p = !1, k = "", o = l.length;
  for (let n = 1; n < f.length; ++n) {
    const s = f[n];
    switch (s.type) {
      case "space":
        p = !0;
      case "newline":
        o += s.source.length;
        break;
      case "comment":
        m && !p && i(s, "MISSING_CHAR", "Comments must be separated from other tokens by white space characters"), o += s.source.length, k = s.source.substring(1);
        break;
      case "error":
        i(s, "UNEXPECTED_TOKEN", s.message), o += s.source.length;
        break;
      default: {
        const d = `Unexpected token in block scalar header: ${s.type}`;
        i(s, "UNEXPECTED_TOKEN", d);
        const e = s.source;
        e && typeof e == "string" && (o += e.length);
      }
    }
  }
  return { mode: u, indent: t, chomp: g, comment: k, length: o };
}
function I(h) {
  const f = h.split(/\n( *)/), m = f[0], i = m.match(/^( *)/), u = [i != null && i[1] ? [i[1], m.slice(i[1].length)] : ["", m]];
  for (let t = 1; t < f.length; t += 2)
    u.push([f[t], f[t + 1]]);
  return u;
}
export {
  C as resolveBlockScalar
};
