const d = {
  172351395: { name: "EBML", type: "Container" },
  646: { name: "EBMLVersion", type: "Uint" },
  759: { name: "EBMLReadVersion", type: "Uint" },
  754: { name: "EBMLMaxIDLength", type: "Uint" },
  755: { name: "EBMLMaxSizeLength", type: "Uint" },
  642: { name: "DocType", type: "String" },
  647: { name: "DocTypeVersion", type: "Uint" },
  645: { name: "DocTypeReadVersion", type: "Uint" },
  108: { name: "Void", type: "Binary" },
  1161: { name: "Duration", type: "Float" },
  139690087: { name: "Segment", type: "Container" },
  88713574: { name: "Info", type: "Container" },
  710577: { name: "TimecodeScale", type: "Uint" }
};
class c {
  source;
  data;
  name;
  type;
  constructor(t = "Unknown", e = "Unknown") {
    this.name = t, this.type = e;
  }
  updateBySource() {
  }
  setSource(t) {
    this.source = t, this.updateBySource();
  }
  updateByData() {
  }
  setData(t) {
    this.data = t, this.updateByData();
  }
}
class y extends c {
  constructor(t, e) {
    super(t, e || "Uint");
  }
  updateBySource() {
    this.data = "";
    for (let t = 0; t < this.source.length; t++) {
      const e = this.source[t].toString(16);
      this.data += h(e);
    }
  }
  updateByData() {
    const t = this.data.length / 2;
    this.source = new Uint8Array(t);
    for (let e = 0; e < t; e++) {
      const s = this.data.substr(e * 2, 2);
      this.source[e] = parseInt(s, 16);
    }
  }
  getValue() {
    return parseInt(this.data, 16);
  }
  setValue(t) {
    this.setData(h(t.toString(16)));
  }
}
function h(r) {
  return r.length % 2 === 1 ? "0" + r : r;
}
class l extends c {
  constructor(t, e) {
    super(t, e || "Float");
  }
  getFloatArrayType() {
    return this.source && this.source.length === 4 ? Float32Array : Float64Array;
  }
  updateBySource() {
    const t = this.source.slice().reverse(), e = this.getFloatArrayType(), s = new e(t.buffer);
    this.data = s[0];
  }
  updateByData() {
    const t = this.getFloatArrayType(), e = new t([this.data]), s = new Uint8Array(e.buffer);
    this.source = s.slice().reverse();
  }
  getValue() {
    return this.data;
  }
  setValue(t) {
    this.setData(t);
  }
}
class u extends c {
  offset = 0;
  data = [];
  constructor(t, e) {
    super(t, e || "Container");
  }
  readByte() {
    return this.source[this.offset++];
  }
  readUint() {
    const t = this.readByte(), e = 8 - t.toString(2).length;
    let s = t - (1 << 7 - e);
    for (let a = 0; a < e; a++)
      s *= 256, s += this.readByte();
    return s;
  }
  updateBySource() {
    let t;
    for (this.data = [], this.offset = 0; this.offset < this.source.length; this.offset = t) {
      const e = this.readUint(), s = this.readUint();
      t = Math.min(this.offset + s, this.source.length);
      const a = this.source.slice(this.offset, t), n = d[e] || { name: "Unknown", type: "Unknown" };
      let i = c;
      switch (n.type) {
        case "Container":
          i = u;
          break;
        case "Uint":
          i = y;
          break;
        case "Float":
          i = l;
          break;
      }
      const o = new i(n.name, n.type);
      o.setSource(a), this.data.push({
        id: e,
        idHex: e.toString(16),
        data: o
      });
    }
  }
  writeUint(t, e = !1) {
    let s = 1, a = 128;
    for (; t >= a && s < 8; )
      s++, a *= 128;
    if (!e) {
      let n = a + t;
      for (let i = s - 1; i >= 0; i--) {
        const o = n % 256;
        this.source[this.offset + i] = o, n = (n - o) / 256;
      }
    }
    this.offset += s;
  }
  writeSections(t = !1) {
    this.offset = 0;
    for (let e = 0; e < this.data.length; e++) {
      const s = this.data[e], a = s.data.source, n = a.length;
      this.writeUint(s.id, t), this.writeUint(n, t), t || this.source.set(a, this.offset), this.offset += n;
    }
    return this.offset;
  }
  updateByData() {
    const t = this.writeSections(!0);
    this.source = new Uint8Array(t), this.writeSections();
  }
  getSectionById(t) {
    for (let e = 0; e < this.data.length; e++) {
      const s = this.data[e];
      if (s.id === t)
        return s.data;
    }
  }
}
class f extends u {
  constructor(t) {
    super("File", "File"), this.setSource(t);
  }
  fixDuration(t) {
    const e = this.getSectionById(139690087);
    if (!e)
      return !1;
    const s = e.getSectionById(88713574);
    if (!s)
      return !1;
    const a = s.getSectionById(710577);
    if (!a)
      return !1;
    let n = s.getSectionById(1161);
    if (n)
      if (n.getValue() <= 0)
        n.setValue(t);
      else
        return !1;
    else
      n = new l("Duration", "Float"), n.setValue(t), s.data.push({
        id: 1161,
        data: n
      });
    return a.setValue(1e6), s.updateByData(), e.updateByData(), this.updateByData(), !0;
  }
  toBlob(t = "video/webm") {
    return new Blob([this.source.buffer], { type: t });
  }
}
const p = (r, t, e = "video/webm") => new Promise((s, a) => {
  try {
    const n = new FileReader();
    n.addEventListener("loadend", () => {
      try {
        const i = n.result, o = new f(new Uint8Array(i));
        o.fixDuration(t) ? s(o.toBlob(e)) : s(r);
      } catch (i) {
        a(i);
      }
    }), n.addEventListener("error", () => a()), n.readAsArrayBuffer(r);
  } catch (n) {
    a(n);
  }
});
export {
  p as webmFixDuration
};
//# sourceMappingURL=BlobFix.es.js.map
