import { isNode as f } from "./accelbyte-widgets711.mjs";
import { visit as c } from "./accelbyte-widgets649.mjs";
const o = {
  "!": "%21",
  ",": "%2C",
  "[": "%5B",
  "]": "%5D",
  "{": "%7B",
  "}": "%7D"
}, u = (h) => h.replace(/[!,[\]{}]/g, (t) => o[t]);
class n {
  constructor(t, e) {
    this.docStart = null, this.docEnd = !1, this.yaml = Object.assign({}, n.defaultYaml, t), this.tags = Object.assign({}, n.defaultTags, e);
  }
  clone() {
    const t = new n(this.yaml, this.tags);
    return t.docStart = this.docStart, t;
  }
  atDocument() {
    const t = new n(this.yaml, this.tags);
    switch (this.yaml.version) {
      case "1.1":
        this.atNextDocument = !0;
        break;
      case "1.2":
        this.atNextDocument = !1, this.yaml = {
          explicit: n.defaultYaml.explicit,
          version: "1.2"
        }, this.tags = Object.assign({}, n.defaultTags);
        break;
    }
    return t;
  }
  add(t, e) {
    this.atNextDocument && (this.yaml = { explicit: n.defaultYaml.explicit, version: "1.1" }, this.tags = Object.assign({}, n.defaultTags), this.atNextDocument = !1);
    const a = t.trim().split(/[ \t]+/), l = a.shift();
    switch (l) {
      case "%TAG": {
        if (a.length !== 2 && (e(0, "%TAG directive should contain exactly two parts"), a.length < 2))
          return !1;
        const [s, i] = a;
        return this.tags[s] = i, !0;
      }
      case "%YAML": {
        if (this.yaml.explicit = !0, a.length !== 1)
          return e(0, "%YAML directive should contain exactly one part"), !1;
        const [s] = a;
        if (s === "1.1" || s === "1.2")
          return this.yaml.version = s, !0;
        {
          const i = /^\d+\.\d+$/.test(s);
          return e(6, `Unsupported YAML version ${s}`, i), !1;
        }
      }
      default:
        return e(0, `Unknown directive ${l}`, !0), !1;
    }
  }
  tagName(t, e) {
    if (t === "!")
      return "!";
    if (t[0] !== "!")
      return e(`Not a valid tag: ${t}`), null;
    if (t[1] === "<") {
      const i = t.slice(2, -1);
      return i === "!" || i === "!!" ? (e(`Verbatim tags aren't resolved, so ${t} is invalid.`), null) : (t[t.length - 1] !== ">" && e("Verbatim tags must end with a >"), i);
    }
    const [, a, l] = t.match(/^(.*!)([^!]*)$/);
    l || e(`The ${t} tag has no suffix`);
    const s = this.tags[a];
    return s ? s + decodeURIComponent(l) : a === "!" ? t : (e(`Could not resolve tag: ${t}`), null);
  }
  tagString(t) {
    for (const [e, a] of Object.entries(this.tags))
      if (t.startsWith(a))
        return e + u(t.substring(a.length));
    return t[0] === "!" ? t : `!<${t}>`;
  }
  toString(t) {
    const e = this.yaml.explicit ? [`%YAML ${this.yaml.version || "1.2"}`] : [], a = Object.entries(this.tags);
    let l;
    if (t && a.length > 0 && f(t.contents)) {
      const s = {};
      c(t.contents, (i, r) => {
        f(r) && r.tag && (s[r.tag] = !0);
      }), l = Object.keys(s);
    } else
      l = [];
    for (const [s, i] of a)
      s === "!!" && i === "tag:yaml.org,2002:" || (!t || l.some((r) => r.startsWith(i))) && e.push(`%TAG ${s} ${i}`);
    return e.join(`
`);
  }
}
n.defaultYaml = { explicit: !1, version: "1.2" };
n.defaultTags = { "!!": "tag:yaml.org,2002:" };
export {
  n as Directives
};
