import { Scalar as b } from "./accelbyte-widgets651.mjs";
import { foldFlowLines as E, FOLD_QUOTED as U, FOLD_FLOW as N, FOLD_BLOCK as A } from "./accelbyte-widgets755.mjs";
const D = (s, t) => ({
  indentAtStart: t ? s.indent.length : s.indentAtStart,
  lineWidth: s.options.lineWidth,
  minContentWidth: s.options.minContentWidth
}), F = (s) => /^(%|---|\.\.\.)/m.test(s);
function I(s, t, n) {
  if (!t || t < 0)
    return !1;
  const l = t - n, u = s.length;
  if (u <= l)
    return !1;
  for (let r = 0, e = 0; r < u; ++r)
    if (s[r] === `
`) {
      if (r - e > l)
        return !0;
      if (e = r + 1, u - e <= l)
        return !1;
    }
  return !0;
}
function S(s, t) {
  const n = JSON.stringify(s);
  if (t.options.doubleQuotedAsJSON)
    return n;
  const { implicitKey: l } = t, u = t.options.doubleQuotedMinMultiLineLength, r = t.indent || (F(s) ? "  " : "");
  let e = "", f = 0;
  for (let i = 0, o = n[i]; o; o = n[++i])
    if (o === " " && n[i + 1] === "\\" && n[i + 2] === "n" && (e += n.slice(f, i) + "\\ ", i += 1, f = i, o = "\\"), o === "\\")
      switch (n[i + 1]) {
        case "u":
          {
            e += n.slice(f, i);
            const a = n.substr(i + 2, 4);
            switch (a) {
              case "0000":
                e += "\\0";
                break;
              case "0007":
                e += "\\a";
                break;
              case "000b":
                e += "\\v";
                break;
              case "001b":
                e += "\\e";
                break;
              case "0085":
                e += "\\N";
                break;
              case "00a0":
                e += "\\_";
                break;
              case "2028":
                e += "\\L";
                break;
              case "2029":
                e += "\\P";
                break;
              default:
                a.substr(0, 2) === "00" ? e += "\\x" + a.substr(2) : e += n.substr(i, 6);
            }
            i += 5, f = i + 1;
          }
          break;
        case "n":
          if (l || n[i + 2] === '"' || n.length < u)
            i += 1;
          else {
            for (e += n.slice(f, i) + `

`; n[i + 2] === "\\" && n[i + 3] === "n" && n[i + 4] !== '"'; )
              e += `
`, i += 2;
            e += r, n[i + 2] === " " && (e += "\\"), i += 1, f = i + 1;
          }
          break;
        default:
          i += 1;
      }
  return e = f ? e + n.slice(f) : n, l ? e : E(e, r, U, D(t, !1));
}
function w(s, t) {
  if (t.options.singleQuote === !1 || t.implicitKey && s.includes(`
`) || /[ \t]\n|\n[ \t]/.test(s))
    return S(s, t);
  const n = t.indent || (F(s) ? "  " : ""), l = "'" + s.replace(/'/g, "''").replace(/\n+/g, `$&
${n}`) + "'";
  return t.implicitKey ? l : E(l, n, N, D(t, !1));
}
function O(s, t) {
  const { singleQuote: n } = t.options;
  let l;
  if (n === !1)
    l = S;
  else {
    const u = s.includes('"'), r = s.includes("'");
    u && !r ? l = w : r && !u ? l = S : l = n ? w : S;
  }
  return l(s, t);
}
let B;
try {
  B = new RegExp(`(^|(?<!
))
+(?!
|$)`, "g");
} catch {
  B = /\n+(?!\n|$)/g;
}
function y({ comment: s, type: t, value: n }, l, u, r) {
  const { blockQuote: e, commentString: f, lineWidth: i } = l.options;
  if (!e || /\n[\t ]+$/.test(n) || /^\s*$/.test(n))
    return O(n, l);
  const o = l.indent || (l.forceBlockIndent || F(n) ? "  " : ""), a = e === "literal" ? !0 : e === "folded" || t === b.BLOCK_FOLDED ? !1 : t === b.BLOCK_LITERAL ? !0 : !I(n, i, o.length);
  if (!n)
    return a ? `|
` : `>
`;
  let g, d;
  for (d = n.length; d > 0; --d) {
    const h = n[d - 1];
    if (h !== `
` && h !== "	" && h !== " ")
      break;
  }
  let c = n.substring(d);
  const k = c.indexOf(`
`);
  k === -1 ? g = "-" : n === c || k !== c.length - 1 ? (g = "+", r && r()) : g = "", c && (n = n.slice(0, -c.length), c[c.length - 1] === `
` && (c = c.slice(0, -1)), c = c.replace(B, `$&${o}`));
  let p = !1, $, _ = -1;
  for ($ = 0; $ < n.length; ++$) {
    const h = n[$];
    if (h === " ")
      p = !0;
    else if (h === `
`)
      _ = $;
    else
      break;
  }
  let L = n.substring(0, _ < $ ? _ + 1 : $);
  L && (n = n.substring(L.length), L = L.replace(/\n+/g, `$&${o}`));
  let Q = (a ? "|" : ">") + (p ? o ? "2" : "1" : "") + g;
  if (s && (Q += " " + f(s.replace(/ ?[\r\n]+/g, " ")), u && u()), a)
    return n = n.replace(/\n+/g, `$&${o}`), `${Q}
${o}${L}${n}${c}`;
  n = n.replace(/\n+/g, `
$&`).replace(/(?:^|\n)([\t ].*)(?:([\n\t ]*)\n(?![\n\t ]))?/g, "$1$2").replace(/\n+/g, `$&${o}`);
  const T = E(`${L}${n}${c}`, o, A, D(l, !0));
  return `${Q}
${o}${T}`;
}
function K(s, t, n, l) {
  const { type: u, value: r } = s, { actualString: e, implicitKey: f, indent: i, indentStep: o, inFlow: a } = t;
  if (f && /[\n[\]{},]/.test(r) || a && /[[\]{},]/.test(r))
    return O(r, t);
  if (!r || /^[\n\t ,[\]{}#&*!|>'"%@`]|^[?-]$|^[?-][ \t]|[\n:][ \t]|[ \t]\n|[\n\t ]#|[\n\t :]$/.test(r))
    return f || a || !r.includes(`
`) ? O(r, t) : y(s, t, n, l);
  if (!f && !a && u !== b.PLAIN && r.includes(`
`))
    return y(s, t, n, l);
  if (F(r)) {
    if (i === "")
      return t.forceBlockIndent = !0, y(s, t, n, l);
    if (f && i === o)
      return O(r, t);
  }
  const g = r.replace(/\n+/g, `$&
${i}`);
  if (e) {
    const d = (p) => {
      var $;
      return p.default && p.tag !== "tag:yaml.org,2002:str" && (($ = p.test) == null ? void 0 : $.test(g));
    }, { compat: c, tags: k } = t.doc.schema;
    if (k.some(d) || c != null && c.some(d))
      return O(r, t);
  }
  return f ? g : E(g, i, N, D(t, !1));
}
function R(s, t, n, l) {
  const { implicitKey: u, inFlow: r } = t, e = typeof s.value == "string" ? s : Object.assign({}, s, { value: String(s.value) });
  let { type: f } = s;
  f !== b.QUOTE_DOUBLE && /[\x00-\x08\x0b-\x1f\x7f-\x9f\u{D800}-\u{DFFF}]/u.test(e.value) && (f = b.QUOTE_DOUBLE);
  const i = (a) => {
    switch (a) {
      case b.BLOCK_FOLDED:
      case b.BLOCK_LITERAL:
        return u || r ? O(e.value, t) : y(e, t, n, l);
      case b.QUOTE_DOUBLE:
        return S(e.value, t);
      case b.QUOTE_SINGLE:
        return w(e.value, t);
      case b.PLAIN:
        return K(e, t, n, l);
      default:
        return null;
    }
  };
  let o = i(f);
  if (o === null) {
    const { defaultKeyType: a, defaultStringType: g } = t.options, d = u && a || g;
    if (o = i(d), o === null)
      throw new Error(`Unsupported default string type ${d}`);
  }
  return o;
}
export {
  R as stringifyString
};
