import { b as M, p as R } from "./prod-BoQuiIB3.js";
const b = /^Format:[\s\t]*/, y = /^Style:[\s\t]*/, O = /^Dialogue:[\s\t]*/, S = /[\s\t]*,[\s\t]*/, _ = /\{[^}]+\}/g, w = /\\N/g, I = /^\[(.*)[\s\t]?Styles\]$/, m = /^\[(.*)[\s\t]?Events\]$/;
class F {
  h;
  O = 0;
  c = null;
  l = [];
  m = [];
  N = null;
  f;
  P = {};
  async init(t) {
    this.h = t, t.errors && (this.f = (await import("./errors-CTL-RV3B.js")).ParseErrorBuilder);
  }
  parse(t, l) {
    if (this.O)
      switch (this.O) {
        case 1:
          if (t === "")
            this.O = 0;
          else if (y.test(t))
            if (this.N) {
              const e = t.replace(y, "").split(S);
              this.S(e);
            } else
              this.g(this.f?.T("Style", l));
          else b.test(t) ? this.N = t.replace(b, "").split(S) : m.test(t) && (this.N = null, this.O = 2);
          break;
        case 2:
          if (t === "")
            this.Q();
          else if (O.test(t))
            if (this.Q(), this.N) {
              const e = t.replace(O, "").split(S), s = this.U(e, l);
              s && (this.c = s);
            } else
              this.g(this.f?.T("Dialogue", l));
          else this.c ? this.c.text += `
` + t.replace(_, "").replace(w, `
`) : b.test(t) ? this.N = t.replace(b, "").split(S) : I.test(t) ? (this.N = null, this.O = 1) : m.test(t) && (this.N = null);
      }
    else t === "" || (I.test(t) ? (this.N = null, this.O = 1) : m.test(t) && (this.N = null, this.O = 2));
  }
  done() {
    return {
      metadata: {},
      cues: this.l,
      regions: [],
      errors: this.m
    };
  }
  Q() {
    this.c && (this.l.push(this.c), this.h.onCue?.(this.c), this.c = null);
  }
  S(t) {
    let l = "Default", e = {}, s, a = "center", r = "bottom", g, o = 1.2, c, h, n = 3, f = [];
    for (let u = 0; u < this.N.length; u++) {
      const x = this.N[u], i = t[u];
      switch (x) {
        case "Name":
          l = i;
          break;
        case "Fontname":
          e["font-family"] = i;
          break;
        case "Fontsize":
          e["font-size"] = `calc(${i} / var(--overlay-height))`;
          break;
        case "PrimaryColour":
          const T = k(i);
          T && (e["--cue-color"] = T);
          break;
        case "BorderStyle":
          n = parseInt(i, 10);
          break;
        case "BackColour":
          h = k(i);
          break;
        case "OutlineColour":
          const E = k(i);
          E && (c = E);
          break;
        case "Bold":
          parseInt(i) && (e["font-weight"] = "bold");
          break;
        case "Italic":
          parseInt(i) && (e["font-style"] = "italic");
          break;
        case "Underline":
          parseInt(i) && (e["text-decoration"] = "underline");
          break;
        case "StrikeOut":
          parseInt(i) && (e["text-decoration"] = "line-through");
          break;
        case "Spacing":
          e["letter-spacing"] = i + "px";
          break;
        case "AlphaLevel":
          e.opacity = parseFloat(i);
          break;
        case "ScaleX":
          f.push(`scaleX(${parseFloat(i) / 100})`);
          break;
        case "ScaleY":
          f.push(`scaleY(${parseFloat(i) / 100})`);
          break;
        case "Angle":
          f.push(`rotate(${i}deg)`);
          break;
        case "Shadow":
          o = parseInt(i, 10) * 1.2;
          break;
        case "MarginL":
          e["--cue-width"] = "auto", e["--cue-left"] = parseFloat(i) + "px";
          break;
        case "MarginR":
          e["--cue-width"] = "auto", e["--cue-right"] = parseFloat(i) + "px";
          break;
        case "MarginV":
          g = parseFloat(i);
          break;
        case "Outline":
          s = parseInt(i, 10);
          break;
        case "Alignment":
          const d = parseInt(i, 10);
          switch (d >= 4 && (r = d >= 7 ? "top" : "center"), d % 3) {
            case 1:
              a = "start";
              break;
            case 2:
              a = "center";
              break;
            case 3:
              a = "end";
              break;
          }
      }
    }
    if (e.R = r, e["--cue-white-space"] = "normal", e["--cue-line-height"] = "normal", e["--cue-text-align"] = a, r === "center" ? (e["--cue-top"] = "50%", f.push("translateY(-50%)")) : e[`--cue-${r}`] = (g || 0) + "px", n === 1 && (e["--cue-padding-y"] = "0"), (n === 1 || h) && (e["--cue-bg-color"] = n === 1 ? "none" : h), n === 3 && c && (e["--cue-outline"] = `${s}px solid ${c}`), n === 1 && typeof s == "number") {
      const u = h ?? "#000";
      e["--cue-text-shadow"] = [
        c && N(s * 1.2, o * 1.2, c),
        c ? N(s * (s / 2), o * (s / 2), u) : N(s, o, u)
      ].filter(Boolean).join(", ");
    }
    f.length && (e["--cue-transform"] = f.join(" ")), this.P[l] = e;
  }
  U(t, l) {
    const e = this.V(t), s = this.q(e.Start, e.End, l);
    if (!s)
      return;
    const a = new M(s[0], s[1], ""), r = { ...this.P[e.Style] || {} }, g = e.Name ? `<v ${e.Name}>` : "", o = r.R, c = e.MarginL && parseFloat(e.MarginL), h = e.MarginR && parseFloat(e.MarginR), n = e.MarginV && parseFloat(e.MarginV);
    return c && (r["--cue-width"] = "auto", r["--cue-left"] = c + "px"), h && (r["--cue-width"] = "auto", r["--cue-right"] = h + "px"), n && o !== "center" && (r[`--cue-${o}`] = n + "px"), a.text = g + t.slice(this.N.length - 1).join(", ").replace(_, "").replace(w, `
`), delete r.R, Object.keys(r).length && (a.style = r), a;
  }
  V(t) {
    const l = {};
    for (let e = 0; e < this.N.length; e++)
      l[this.N[e]] = t[e];
    return l;
  }
  q(t, l, e) {
    const s = R(t), a = R(l);
    if (s !== null && a !== null && a > s)
      return [s, a];
    s === null && this.g(this.f?.s(t, e)), a === null && this.g(this.f?.t(l, e)), s != null && a !== null && a > s && this.g(this.f?.u(s, a, e));
  }
  g(t) {
    if (t) {
      if (this.m.push(t), this.h.strict)
        throw this.h.cancel(), t;
      this.h.onError?.(t);
    }
  }
}
function k(p) {
  const t = parseInt(p.replace("&H", ""), 16);
  if (t >= 0) {
    const e = (t >> 24 & 255 ^ 255) / 255, s = t >> 16 & 255, a = t >> 8 & 255;
    return "rgba(" + [t & 255, a, s, e].join(",") + ")";
  }
  return null;
}
function N(p, t, l) {
  const e = Math.ceil(2 * Math.PI * p);
  let s = "";
  for (let a = 0; a < e; a++) {
    const r = 2 * Math.PI * a / e;
    s += p * Math.cos(r) + "px " + t * Math.sin(r) + "px 0 " + l + (a == e - 1 ? "" : ",");
  }
  return s;
}
function L() {
  return new F();
}
export {
  F as SSAParser,
  L as default
};
