const J = {
  LoadFail: 0,
  BadSignature: 1,
  BadTimestamp: 2,
  BadSettingValue: 3,
  BadFormat: 4,
  UnknownSetting: 5
};
class Q extends Error {
  code;
  line;
  constructor(t) {
    super(t.reason), this.code = t.code, this.line = t.line;
  }
}
const B = /\r?\n|\r/gm;
class Z {
  writable;
  readable;
  constructor(t) {
    const e = new U(t);
    this.writable = new WritableStream({
      write(s) {
        e.transform(s);
      },
      close() {
        e.close();
      }
    }), this.readable = new ReadableStream({
      start(s) {
        e.onLine = (a) => s.enqueue(a), e.onClose = () => s.close();
      }
    });
  }
}
class U {
  a = "";
  b;
  onLine;
  onClose;
  constructor(t) {
    this.b = new TextDecoder(t);
  }
  transform(t) {
    this.a += this.b.decode(t, { stream: !0 });
    const e = this.a.split(B);
    this.a = e.pop() || "";
    for (let s = 0; s < e.length; s++)
      this.onLine(e[s].trim());
  }
  close() {
    this.a && this.onLine(this.a.trim()), this.a = "", this.onClose();
  }
}
async function tt(i, t) {
  const e = new ReadableStream({
    start(s) {
      const a = i.split(B);
      for (const r of a)
        s.enqueue(r);
      s.close();
    }
  });
  return N(e, t);
}
async function N(i, t) {
  const e = t?.type ?? "vtt";
  let s;
  if (typeof e == "string")
    switch (e) {
      case "srt":
        s = (await import("./srt-parser-CsN7bp5E.js")).default;
        break;
      case "ssa":
      case "ass":
        s = (await import("./ssa-parser-BJOOPeQF.js")).default;
        break;
      default:
        s = (await Promise.resolve().then(function() {
          return vt;
        })).default;
    }
  else
    s = e;
  let a;
  const r = i.getReader(), n = s(), o = !!t?.strict || !!t?.errors;
  await n.init({
    strict: !1,
    ...t,
    errors: o,
    type: e,
    cancel() {
      r.cancel(), a = n.done(!0);
    }
  });
  let l = 1;
  for (; ; ) {
    const { value: h, done: c } = await r.read();
    if (c) {
      n.parse("", l), a = n.done(!1);
      break;
    }
    n.parse(h, l), l++;
  }
  return a;
}
async function et(i, t) {
  const e = await i;
  if (!e.ok || !e.body)
    return {
      metadata: {},
      cues: [],
      regions: [],
      errors: [void 0]
    };
  const s = e.headers.get("content-type") || "", a = s.match(/text\/(.*?)(?:;|$)/)?.[1], r = s.match(/charset=(.*?)(?:;|$)/)?.[1];
  return z(e.body, { type: a, encoding: r, ...t });
}
async function z(i, { encoding: t = "utf-8", ...e } = {}) {
  const s = i.pipeThrough(new Z(t));
  return N(s, e);
}
class $ extends EventTarget {
  /**
   * A string that identifies the cue.
   *
   * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/TextTrackCue/id}
   */
  id = "";
  /**
   * A `double` that represents the video time that the cue will start being displayed, in seconds.
   *
   * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/TextTrackCue/startTime}
   */
  startTime;
  /**
   * A `double` that represents the video time that the cue will stop being displayed, in seconds.
   *
   * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/TextTrackCue/endTime}
   */
  endTime;
  /**
   * Returns a string with the contents of the cue.
   *
   * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/VTTCue/text}
   */
  text;
  /**
   * A `boolean` for whether the video will pause when this cue stops being displayed.
   *
   * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/TextTrackCue/pauseOnExit}
   */
  pauseOnExit = !1;
  constructor(t, e, s) {
    super(), this.startTime = t, this.endTime = e, this.text = s;
  }
  addEventListener(t, e, s) {
    super.addEventListener(t, e, s);
  }
  removeEventListener(t, e, s) {
    super.removeEventListener(t, e, s);
  }
}
const _ = typeof document > "u", it = _ ? $ : window.VTTCue;
class j extends it {
  /**
   * A `VTTRegion` object describing the video's sub-region that the cue will be drawn onto,
   * or `null` if none is assigned.
   *
   * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/VTTCue/region}
   */
  region = null;
  /**
   * The cue writing direction.
   *
   * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/VTTCue/vertical}
   */
  vertical = "";
  /**
   * Returns `true` if the `VTTCue.line` attribute is an integer number of lines or a percentage
   * of the video size.
   *
   * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/VTTCue/snapToLines}
   */
  snapToLines = !0;
  /**
   * Returns the line positioning of the cue. This can be the string `'auto'` or a number whose
   * interpretation depends on the value of `VTTCue.snapToLines`.
   *
   * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/VTTCue/line}
   */
  line = "auto";
  /**
   * Returns an enum representing the alignment of the `VTTCue.line`.
   *
   * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/VTTCue/lineAlign}
   */
  lineAlign = "start";
  /**
   * Returns the indentation of the cue within the line. This can be the string `'auto'` or a
   * number representing the percentage of the `VTTCue.region`, or the video size if `VTTCue`.region`
   * is `null`.
   *
   * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/VTTCue/position}
   */
  position = "auto";
  /**
   * Returns an enum representing the alignment of the cue. This is used to determine what
   * the `VTTCue.position` is anchored to. The default is `'auto'`.
   *
   * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/VTTCue/positionAlign}
   */
  positionAlign = "auto";
  /**
   * Returns a double representing the size of the cue, as a percentage of the video size.
   *
   * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/VTTCue/size}
   */
  size = 100;
  /**
   * Returns an enum representing the alignment of all the lines of text within the cue box.
   *
   * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/VTTCue/align}
   */
  align = "center";
  /**
   * Additional styles associated with the cue.
   */
  style;
}
class G {
  /**
   * A string that identifies the region.
   */
  id = "";
  /**
   * A `double` representing the width of the region, as a percentage of the video.
   */
  width = 100;
  /**
   * A `double` representing the height of the region, in number of lines.
   */
  lines = 3;
  /**
   * A `double` representing the region anchor X offset, as a percentage of the region.
   */
  regionAnchorX = 0;
  /**
   * A `double` representing the region anchor Y offset, as a percentage of the region.
   */
  regionAnchorY = 100;
  /**
   * A `double` representing the viewport anchor X offset, as a percentage of the video.
   */
  viewportAnchorX = 0;
  /**
   * A `double` representing the viewport anchor Y offset, as a percentage of the video.
   */
  viewportAnchorY = 100;
  /**
   * An enum representing how adding new cues will move existing cues.
   */
  scroll = "";
}
const M = ",", st = "%";
function rt(i) {
  const t = parseInt(i, 10);
  return Number.isNaN(t) ? null : t;
}
function w(i) {
  const t = parseInt(i.replace(st, ""), 10);
  return !Number.isNaN(t) && t >= 0 && t <= 100 ? t : null;
}
function x(i) {
  if (!i.includes(M))
    return null;
  const [t, e] = i.split(M).map(w);
  return t !== null && e !== null ? [t, e] : null;
}
function nt(i) {
  const t = parseFloat(i);
  return Number.isNaN(t) ? null : t;
}
const at = "WEBVTT", P = ",", ot = "%", g = /[:=]/, lt = /^[\s\t]*(region|vertical|line|position|size|align)[:=]/, ht = "NOTE", ct = "REGION", ut = /^REGION:?[\s\t]+/, T = /[\s\t]+/, ft = "-->", dt = /[\s\t]*-->[\s\t]+/, pt = /start|center|end|left|right/, gt = /start|center|end/, mt = /line-(?:left|right)|center|auto/, Tt = /^(?:(\d{1,2}):)?(\d{2}):(\d{2})(?:\.(\d{1,3}))?$/;
var H = /* @__PURE__ */ ((i) => (i[i.None = 0] = "None", i[i.Header = 1] = "Header", i[i.Cue = 2] = "Cue", i[i.Region = 3] = "Region", i[i.Note = 4] = "Note", i))(H || {});
class V {
  h;
  e = 0;
  i = {};
  j = {};
  l = [];
  c = null;
  d = null;
  m = [];
  f;
  n = "";
  async init(t) {
    this.h = t, t.strict && (this.e = 1), t.errors && (this.f = (await import("./errors-CTL-RV3B.js")).ParseErrorBuilder);
  }
  parse(t, e) {
    if (t === "")
      this.c ? (this.l.push(this.c), this.h.onCue?.(this.c), this.c = null) : this.d ? (this.j[this.d.id] = this.d, this.h.onRegion?.(this.d), this.d = null) : this.e === 1 && (this.k(t, e), this.h.onHeaderMetadata?.(this.i)), this.e = 0;
    else if (this.e)
      switch (this.e) {
        case 1:
          this.k(t, e);
          break;
        case 2:
          if (this.c) {
            const s = this.c.text.length > 0;
            !s && lt.test(t) ? this.o(t.split(T), e) : this.c.text += (s ? `
` : "") + t;
          }
          break;
        case 3:
          this.p(t.split(T), e);
          break;
      }
    else if (t.startsWith(ht))
      this.e = 4;
    else if (t.startsWith(ct))
      this.e = 3, this.d = new G(), this.p(t.replace(ut, "").split(T), e);
    else if (t.includes(ft)) {
      const s = this.q(t, e);
      s && (this.c = new j(s[0], s[1], ""), this.c.id = this.n, this.o(s[2], e)), this.e = 2;
    } else e === 1 && this.k(t, e);
    this.n = t;
  }
  done() {
    return {
      metadata: this.i,
      cues: this.l,
      regions: Object.values(this.j),
      errors: this.m
    };
  }
  k(t, e) {
    if (e > 1) {
      if (g.test(t)) {
        const [s, a] = t.split(g);
        s && (this.i[s] = (a || "").replace(T, ""));
      }
    } else t.startsWith(at) ? this.e = 1 : this.g(this.f?.r());
  }
  q(t, e) {
    const [s, a = ""] = t.split(dt), [r, ...n] = a.split(T), o = y(s), l = y(r);
    if (o !== null && l !== null && l > o)
      return [o, l, n];
    o === null && this.g(this.f?.s(s, e)), l === null && this.g(this.f?.t(r, e)), o != null && l !== null && l > o && this.g(this.f?.u(o, l, e));
  }
  /**
   * @see {@link https://www.w3.org/TR/webvtt1/#region-settings-parsing}
   */
  p(t, e) {
    let s;
    for (let a = 0; a < t.length; a++)
      if (g.test(t[a])) {
        s = !1;
        const [r, n] = t[a].split(g);
        switch (r) {
          case "id":
            this.d.id = n;
            break;
          case "width":
            const o = w(n);
            o !== null ? this.d.width = o : s = !0;
            break;
          case "lines":
            const l = rt(n);
            l !== null ? this.d.lines = l : s = !0;
            break;
          case "regionanchor":
            const h = x(n);
            h !== null ? (this.d.regionAnchorX = h[0], this.d.regionAnchorY = h[1]) : s = !0;
            break;
          case "viewportanchor":
            const c = x(n);
            c !== null ? (this.d.viewportAnchorX = c[0], this.d.viewportAnchorY = c[1]) : s = !0;
            break;
          case "scroll":
            n === "up" ? this.d.scroll = "up" : s = !0;
            break;
          default:
            this.g(this.f?.v(r, n, e));
        }
        s && this.g(this.f?.w(r, n, e));
      }
  }
  /**
   * @see {@link https://www.w3.org/TR/webvtt1/#cue-timings-and-settings-parsing}
   */
  o(t, e) {
    let s;
    for (let a = 0; a < t.length; a++)
      if (s = !1, g.test(t[a])) {
        const [r, n] = t[a].split(g);
        switch (r) {
          case "region":
            const o = this.j[n];
            o && (this.c.region = o);
            break;
          case "vertical":
            n === "lr" || n === "rl" ? (this.c.vertical = n, this.c.region = null) : s = !0;
            break;
          case "line":
            const [l, h] = n.split(P);
            if (l.includes(ot)) {
              const m = w(l);
              m !== null ? (this.c.line = m, this.c.snapToLines = !1) : s = !0;
            } else {
              const m = nt(l);
              m !== null ? this.c.line = m : s = !0;
            }
            gt.test(h) ? this.c.lineAlign = h : h && (s = !0), this.c.line !== "auto" && (this.c.region = null);
            break;
          case "position":
            const [c, f] = n.split(P), b = w(c);
            b !== null ? this.c.position = b : s = !0, f && mt.test(f) ? this.c.positionAlign = f : f && (s = !0);
            break;
          case "size":
            const S = w(n);
            S !== null ? (this.c.size = S, S < 100 && (this.c.region = null)) : s = !0;
            break;
          case "align":
            pt.test(n) ? this.c.align = n : s = !0;
            break;
          default:
            this.g(this.f?.x(r, n, e));
        }
        s && this.g(this.f?.y(r, n, e));
      }
  }
  g(t) {
    if (t) {
      if (this.m.push(t), this.h.strict)
        throw this.h.cancel(), t;
      this.h.onError?.(t);
    }
  }
}
function y(i) {
  const t = i.match(Tt);
  if (!t)
    return null;
  const e = t[1] ? parseInt(t[1], 10) : 0, s = parseInt(t[2], 10), a = parseInt(t[3], 10), r = t[4] ? parseInt(t[4].padEnd(3, "0"), 10) : 0, n = e * 3600 + s * 60 + a + r / 1e3;
  return e < 0 || s < 0 || a < 0 || r < 0 || s > 59 || a > 59 ? null : n;
}
function wt() {
  return new V();
}
var vt = /* @__PURE__ */ Object.freeze({
  __proto__: null,
  VTTBlock: H,
  VTTParser: V,
  default: wt,
  parseVTTTimestamp: y
});
const yt = /[0-9]/, bt = /[\s\t]+/, F = {
  c: "span",
  i: "i",
  b: "b",
  u: "u",
  ruby: "ruby",
  rt: "rt",
  v: "span",
  lang: "span",
  timestamp: "span"
}, At = {
  "&amp;": "&",
  "&lt;": "<",
  "&gt;": ">",
  "&quot;": '"',
  "&#39;": "'",
  "&nbsp;": " ",
  "&lrm;": "‎",
  "&rlm;": "‏"
}, Et = /&(?:amp|lt|gt|quot|#(0+)?39|nbsp|lrm|rlm);/g, St = /* @__PURE__ */ new Set([
  "white",
  "lime",
  "cyan",
  "red",
  "yellow",
  "magenta",
  "blue",
  "black"
]), kt = /* @__PURE__ */ new Set(Object.keys(F));
function D(i) {
  let t = "", e = 1, s = [], a = [], r;
  for (let h = 0; h < i.text.length; h++) {
    const c = i.text[h];
    switch (e) {
      case 1:
        c === "<" ? (l(), e = 2) : t += c;
        break;
      case 2:
        switch (c) {
          case `
`:
          case "	":
          case " ":
            n(), e = 4;
            break;
          case ".":
            n(), e = 3;
            break;
          case "/":
            e = 5;
            break;
          case ">":
            n(), e = 1;
            break;
          default:
            !t && yt.test(c) && (e = 6), t += c;
            break;
        }
        break;
      case 3:
        switch (c) {
          case "	":
          case " ":
          case `
`:
            o(), r && r.class?.trim(), e = 4;
            break;
          case ".":
            o();
            break;
          case ">":
            o(), r && r.class?.trim(), e = 1;
            break;
          default:
            t += c;
        }
        break;
      case 4:
        c === ">" ? (t = t.replace(bt, " "), r?.type === "v" ? r.voice = k(t) : r?.type === "lang" && (r.lang = k(t)), t = "", e = 1) : t += c;
        break;
      case 5:
        c === ">" && (t = "", r = a.pop(), e = 1);
        break;
      case 6:
        if (c === ">") {
          const f = y(t);
          f !== null && f >= i.startTime && f <= i.endTime && (t = "timestamp", n(), r.time = f), t = "", e = 1;
        } else
          t += c;
        break;
    }
  }
  function n() {
    if (kt.has(t)) {
      const h = r;
      r = Nt(t), h ? (a[a.length - 1] !== h && a.push(h), h.children.push(r)) : s.push(r);
    }
    t = "", e = 1;
  }
  function o() {
    if (r && t) {
      const h = t.replace("bg_", "");
      St.has(h) ? r[t.startsWith("bg_") ? "bgColor" : "color"] = h : r.class = r.class ? r.class + " " + t : t;
    }
    t = "";
  }
  function l() {
    if (!t)
      return;
    const h = { type: "text", data: k(t) };
    r ? r.children.push(h) : s.push(h), t = "";
  }
  return e === 1 && l(), s;
}
function Nt(i) {
  return {
    tagName: F[i],
    type: i,
    children: []
  };
}
function k(i) {
  return i.replace(Et, (t) => At[t] || "'");
}
function u(i, t, e) {
  i.style.setProperty(`--${t}`, e + "");
}
function d(i, t, e = !0) {
  i.setAttribute(`data-${t}`, e === !0 ? "" : e + "");
}
function A(i, t) {
  i.setAttribute("data-part", t);
}
function _t(i) {
  return parseFloat(getComputedStyle(i).lineHeight) || 0;
}
function Ct(i) {
  if (_)
    throw Error(
      "[media-captions] called `createVTTCueTemplate` on the server - use `renderVTTCueString`"
    );
  const t = document.createElement("template");
  return t.innerHTML = C(i), { cue: i, content: t.content };
}
function C(i, t = 0) {
  return R(D(i), t);
}
function R(i, t = 0) {
  let e, s = "";
  for (const a of i)
    if (a.type === "text")
      s += a.data;
    else {
      const r = a.type === "timestamp";
      e = {}, e.class = a.class, e.title = a.type === "v" && a.voice, e.lang = a.type === "lang" && a.lang, e["data-part"] = a.type === "v" && "voice", r && (e["data-part"] = "timed", e["data-time"] = a.time, e["data-future"] = a.time > t, e["data-past"] = a.time < t), e.style = `${a.color ? `color: ${a.color};` : ""}${a.bgColor ? `background-color: ${a.bgColor};` : ""}`;
      const n = Object.entries(e).filter((o) => o[1]).map((o) => `${o[0]}="${o[1] === !0 ? "" : o[1]}"`).join(" ");
      s += `<${a.tagName}${n ? " " + n : ""}>${R(
        a.children
      )}</${a.tagName}>`;
    }
  return s;
}
function W(i, t) {
  if (!_)
    for (const e of i.querySelectorAll('[data-part="timed"]')) {
      const s = Number(e.getAttribute("data-time"));
      Number.isNaN(s) || (s > t ? d(e, "future") : e.removeAttribute("data-future"), s < t ? d(e, "past") : e.removeAttribute("data-past"));
    }
}
function Rt(i, t) {
  let e = null, s;
  function a() {
    r(), i(...s), s = void 0;
  }
  function r() {
    clearTimeout(e), e = null;
  }
  function n() {
    s = [].slice.call(arguments), r(), e = setTimeout(a, t);
  }
  return n;
}
const p = /* @__PURE__ */ Symbol(0);
function I(i) {
  return i instanceof HTMLElement ? {
    top: i.offsetTop,
    width: i.clientWidth,
    height: i.clientHeight,
    left: i.offsetLeft,
    right: i.offsetLeft + i.clientWidth,
    bottom: i.offsetTop + i.clientHeight
  } : { ...i };
}
function E(i, t, e) {
  switch (t) {
    case "+x":
      i.left += e, i.right += e;
      break;
    case "-x":
      i.left -= e, i.right -= e;
      break;
    case "+y":
      i.top += e, i.bottom += e;
      break;
    case "-y":
      i.top -= e, i.bottom -= e;
      break;
  }
}
function It(i, t) {
  return i.left <= t.right && i.right >= t.left && i.top <= t.bottom && i.bottom >= t.top;
}
function Lt(i, t) {
  for (let e = 0; e < t.length; e++)
    if (It(i, t[e]))
      return t[e];
  return null;
}
function O(i, t) {
  return t.top >= 0 && t.bottom <= i.height && t.left >= 0 && t.right <= i.width;
}
function Mt(i, t, e) {
  switch (e) {
    case "+x":
      return t.left < 0;
    case "-x":
      return t.right > i.width;
    case "+y":
      return t.top < 0;
    case "-y":
      return t.bottom > i.height;
  }
}
function xt(i, t) {
  const e = Math.max(0, Math.min(i.width, t.right) - Math.max(0, t.left)), s = Math.max(0, Math.min(i.height, t.bottom) - Math.max(0, t.top));
  return e * s / (i.height * i.width);
}
function L(i, t) {
  return {
    top: t.top / i.height,
    left: t.left / i.width,
    right: (i.width - t.right) / i.width,
    bottom: (i.height - t.bottom) / i.height
  };
}
function q(i, t) {
  return t.top = t.top * i.height, t.left = t.left * i.width, t.right = i.width - t.right * i.width, t.bottom = i.height - t.bottom * i.height, t;
}
const X = ["top", "left", "right", "bottom"];
function Y(i, t, e, s) {
  const a = L(t, e);
  for (const r of X)
    u(i, `${s}-${r}`, a[r] * 100 + "%");
}
function K(i, t, e, s) {
  let a = 1, r, n = { ...t };
  for (let o = 0; o < s.length; o++) {
    for (; Mt(i, t, s[o]) || O(i, t) && Lt(t, e); )
      E(t, s[o], 1);
    if (O(i, t))
      return t;
    const l = xt(i, t);
    a > l && (r = { ...t }, a = l), t = { ...n };
  }
  return r || n;
}
const v = /* @__PURE__ */ Symbol(0);
function Pt(i, t, e, s) {
  let a = e.firstElementChild, r = zt(t), n, o = [];
  if (e[p] || (e[p] = Ot(i, e)), n = q(i, { ...e[p] }), e[v])
    o = [e[v] === "top" ? "+y" : "-y", "+x", "-x"];
  else if (t.snapToLines) {
    let l;
    switch (t.vertical) {
      case "":
        o = ["+y", "-y"], l = "height";
        break;
      case "rl":
        o = ["+x", "-x"], l = "width";
        break;
      case "lr":
        o = ["-x", "+x"], l = "width";
        break;
    }
    let h = _t(a), c = h * Math.round(r), f = i[l] + h, b = o[0];
    Math.abs(c) > f && (c = c < 0 ? -1 : 1, c *= Math.ceil(f / h) * h), r < 0 && (c += t.vertical === "" ? i.height : i.width, o = o.reverse()), E(n, b, c);
  } else {
    const l = t.vertical === "", h = l ? "+y" : "+x", c = l ? n.height : n.width;
    E(
      n,
      h,
      (l ? i.height : i.width) * r / 100
    ), E(
      n,
      h,
      t.lineAlign === "center" ? c / 2 : t.lineAlign === "end" ? c : 0
    ), o = l ? ["-y", "+y", "-x", "+x"] : ["-x", "+x", "-y", "+y"];
  }
  return n = K(i, n, s, o), Y(e, i, n, "cue"), n;
}
function Ot(i, t) {
  const e = I(t), s = Bt(t);
  if (t[v] = !1, s.top && (e.top = s.top, e.bottom = s.top + e.height, t[v] = "top"), s.bottom) {
    const a = i.height - s.bottom;
    e.top = a - e.height, e.bottom = a, t[v] = "bottom";
  }
  return s.left && (e.left = s.left), s.right && (e.right = i.width - s.right), L(i, e);
}
function Bt(i) {
  const t = {};
  for (const e of X)
    t[e] = parseFloat(i.style.getPropertyValue(`--cue-${e}`));
  return t;
}
function zt(i) {
  return i.line === "auto" ? i.snapToLines ? -1 : 100 : i.line;
}
function $t(i) {
  if (i.position === "auto")
    switch (i.align) {
      case "start":
      case "left":
        return 0;
      case "right":
      case "end":
        return 100;
      default:
        return 50;
    }
  return i.position;
}
function jt(i, t) {
  if (i.positionAlign === "auto")
    switch (i.align) {
      case "start":
        return t === "ltr" ? "line-left" : "line-right";
      case "end":
        return t === "ltr" ? "line-right" : "line-left";
      case "center":
        return "center";
      default:
        return `line-${i.align}`;
    }
  return i.positionAlign;
}
const Gt = ["-y", "+y", "-x", "+x"];
function Ht(i, t, e, s) {
  let a = Array.from(e.querySelectorAll('[data-part="cue-display"]')), r = 0, n = Math.max(0, a.length - t.lines);
  for (let l = a.length - 1; l >= n; l--)
    r += a[l].offsetHeight;
  u(e, "region-height", r + "px"), e[p] || (e[p] = L(i, I(e)));
  let o = { ...e[p] };
  return o = q(i, o), o.width = e.clientWidth, o.height = r, o.right = o.left + o.width, o.bottom = o.top + r, o = K(i, o, s, Gt), Y(e, i, o, "region"), o;
}
class Vt {
  overlay;
  z;
  A = 0;
  C = "ltr";
  B = [];
  D = !1;
  E;
  j = /* @__PURE__ */ new Map();
  l = /* @__PURE__ */ new Map();
  /* Text direction. */
  get dir() {
    return this.C;
  }
  set dir(t) {
    this.C = t, d(this.overlay, "dir", t);
  }
  get currentTime() {
    return this.A;
  }
  set currentTime(t) {
    this.A = t, this.update();
  }
  constructor(t, e) {
    this.overlay = t, this.dir = e?.dir ?? "ltr", t.setAttribute("translate", "yes"), t.setAttribute("aria-live", "off"), t.setAttribute("aria-atomic", "true"), A(t, "captions"), this.G(), this.E = new ResizeObserver(this.I.bind(this)), this.E.observe(t);
  }
  changeTrack({ regions: t, cues: e }) {
    this.reset(), this.J(t);
    for (const s of e)
      this.l.set(s, null);
    this.update();
  }
  addCue(t) {
    this.l.set(t, null), this.update();
  }
  removeCue(t) {
    this.l.delete(t), this.update();
  }
  update(t = !1) {
    this.H(t);
  }
  reset() {
    this.l.clear(), this.j.clear(), this.B = [], this.overlay.textContent = "";
  }
  destroy() {
    this.reset(), this.E.disconnect();
  }
  I() {
    this.D = !0, this.K();
  }
  K = Rt(() => {
    this.D = !1, this.G();
    for (const t of this.j.values())
      t[p] = null;
    for (const t of this.l.values())
      t && (t[p] = null);
    this.H(!0);
  }, 50);
  G() {
    this.z = I(this.overlay), u(this.overlay, "overlay-width", this.z.width + "px"), u(this.overlay, "overlay-height", this.z.height + "px");
  }
  H(t = !1) {
    if (!this.l.size || this.D)
      return;
    let e, s = [...this.l.keys()].filter((r) => this.A >= r.startTime && this.A <= r.endTime).sort(
      (r, n) => r.startTime !== n.startTime ? r.startTime - n.startTime : r.endTime - n.endTime
    ), a = s.map((r) => r.region);
    for (let r = 0; r < this.B.length; r++) {
      if (e = this.B[r], s[r] === e)
        continue;
      if (e.region && !a.includes(e.region)) {
        const o = this.j.get(e.region.id);
        o && (o.removeAttribute("data-active"), t = !0);
      }
      const n = this.l.get(e);
      n && (n.remove(), t = !0);
    }
    for (let r = 0; r < s.length; r++) {
      e = s[r];
      let n = this.l.get(e);
      n || this.l.set(e, n = this.L(e));
      const o = this.F(e) && this.j.get(e.region.id);
      o && !o.hasAttribute("data-active") && (requestAnimationFrame(() => d(o, "active")), t = !0), n.isConnected || ((o || this.overlay).append(n), t = !0);
    }
    if (t) {
      const r = [], n = /* @__PURE__ */ new Set();
      for (let o = s.length - 1; o >= 0; o--) {
        if (e = s[o], n.has(e.region || e))
          continue;
        const l = this.F(e), h = l ? this.j.get(e.region.id) : this.l.get(e);
        l ? r.push(Ht(this.z, e.region, h, r)) : r.push(Pt(this.z, e, h, r)), n.add(l ? e.region : e);
      }
    }
    W(this.overlay, this.A), this.B = s;
  }
  J(t) {
    if (t)
      for (const e of t) {
        const s = this.M(e);
        this.j.set(e.id, s), this.overlay.append(s);
      }
  }
  M(t) {
    const e = document.createElement("div");
    return A(e, "region"), d(e, "id", t.id), d(e, "scroll", t.scroll), u(e, "region-width", t.width + "%"), u(e, "region-anchor-x", t.regionAnchorX), u(e, "region-anchor-y", t.regionAnchorY), u(e, "region-viewport-anchor-x", t.viewportAnchorX), u(e, "region-viewport-anchor-y", t.viewportAnchorY), u(e, "region-lines", t.lines), e;
  }
  L(t) {
    const e = document.createElement("div"), s = $t(t), a = jt(t, this.C);
    if (A(e, "cue-display"), t.vertical !== "" && d(e, "vertical"), u(e, "cue-text-align", t.align), t.style)
      for (const n of Object.keys(t.style))
        e.style.setProperty(n, t.style[n]);
    if (this.F(t))
      u(
        e,
        "cue-offset",
        `${s - (a === "line-right" ? 100 : a === "center" ? 50 : 0)}%`
      );
    else if (u(
      e,
      "cue-writing-mode",
      t.vertical === "" ? "horizontal-tb" : t.vertical === "lr" ? "vertical-lr" : "vertical-rl"
    ), !t.style?.["--cue-width"]) {
      let n = s;
      a === "line-left" ? n = 100 - s : a === "center" && s <= 50 ? n = s * 2 : a === "center" && s > 50 && (n = (100 - s) * 2);
      const o = t.size < n ? t.size : n;
      t.vertical === "" ? u(e, "cue-width", o + "%") : u(e, "cue-height", o + "%");
    }
    const r = document.createElement("div");
    return A(r, "cue"), t.id && d(r, "id", t.id), r.innerHTML = C(t), e.append(r), e;
  }
  F(t) {
    return t.region && t.size === 100 && t.vertical === "" && t.line === "auto";
  }
}
const Ft = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  CaptionsRenderer: Vt,
  ParseError: Q,
  ParseErrorCode: J,
  TextCue: $,
  VTTCue: j,
  VTTRegion: G,
  createVTTCueTemplate: Ct,
  parseByteStream: z,
  parseResponse: et,
  parseText: tt,
  parseTextStream: N,
  parseVTTTimestamp: y,
  renderVTTCueString: C,
  renderVTTTokensString: R,
  tokenizeVTTCue: D,
  updateTimedVTTCueNodes: W
}, Symbol.toStringTag, { value: "Module" }));
export {
  Q as P,
  V,
  H as a,
  j as b,
  J as c,
  Ft as d,
  y as p
};
