import O from "../../../../@videojs_vhs-utils@4.1.1/node_modules/@videojs/vhs-utils/es/stream.js";
import I from "../../../../@babel_runtime@7.27.6/node_modules/@babel/runtime/helpers/esm/extends.js";
import U from "../../../../@videojs_vhs-utils@4.1.1/node_modules/@videojs/vhs-utils/es/decode-b64-to-uint8-array.js";
/*! @name m3u8-parser @version 7.2.0 @license Apache-2.0 */
class w extends O {
  constructor() {
    super(), this.buffer = "";
  }
  /**
   * Add new data to be parsed.
   *
   * @param {string} data the text to process
   */
  push(n) {
    let e;
    for (this.buffer += n, e = this.buffer.indexOf(`
`); e > -1; e = this.buffer.indexOf(`
`))
      this.trigger("data", this.buffer.substring(0, e)), this.buffer = this.buffer.substring(e + 1);
  }
}
const x = "	", D = function(f) {
  const n = /([0-9.]*)?@?([0-9.]*)?/.exec(f || ""), e = {};
  return n[1] && (e.length = parseInt(n[1], 10)), n[2] && (e.offset = parseInt(n[2], 10)), e;
}, $ = function() {
  const e = "(?:" + "[^=]*" + ")=(?:" + '"[^"]*"|[^,]*' + ")";
  return new RegExp("(?:^|,)(" + e + ")");
}, h = function(f) {
  const n = {};
  if (!f)
    return n;
  const e = f.split($());
  let t = e.length, a;
  for (; t--; )
    e[t] !== "" && (a = /([^=]*)=(.*)/.exec(e[t]).slice(1), a[0] = a[0].replace(/^\s+|\s+$/g, ""), a[1] = a[1].replace(/^\s+|\s+$/g, ""), a[1] = a[1].replace(/^['"](.*)['"]$/g, "$1"), n[a[0]] = a[1]);
  return n;
}, M = (f) => {
  const n = f.split("x"), e = {};
  return n[0] && (e.width = parseInt(n[0], 10)), n[1] && (e.height = parseInt(n[1], 10)), e;
};
class G extends O {
  constructor() {
    super(), this.customParsers = [], this.tagMappers = [];
  }
  /**
   * Parses an additional line of input.
   *
   * @param {string} line a single line of an M3U8 file to parse
   */
  push(n) {
    let e, t;
    if (n = n.trim(), n.length === 0)
      return;
    if (n[0] !== "#") {
      this.trigger("data", {
        type: "uri",
        uri: n
      });
      return;
    }
    this.tagMappers.reduce((s, u) => {
      const o = u(n);
      return o === n ? s : s.concat([o]);
    }, [n]).forEach((s) => {
      for (let u = 0; u < this.customParsers.length; u++)
        if (this.customParsers[u].call(this, s))
          return;
      if (s.indexOf("#EXT") !== 0) {
        this.trigger("data", {
          type: "comment",
          text: s.slice(1)
        });
        return;
      }
      if (s = s.replace("\r", ""), e = /^#EXTM3U/.exec(s), e) {
        this.trigger("data", {
          type: "tag",
          tagType: "m3u"
        });
        return;
      }
      if (e = /^#EXTINF:([0-9\.]*)?,?(.*)?$/.exec(s), e) {
        t = {
          type: "tag",
          tagType: "inf"
        }, e[1] && (t.duration = parseFloat(e[1])), e[2] && (t.title = e[2]), this.trigger("data", t);
        return;
      }
      if (e = /^#EXT-X-TARGETDURATION:([0-9.]*)?/.exec(s), e) {
        t = {
          type: "tag",
          tagType: "targetduration"
        }, e[1] && (t.duration = parseInt(e[1], 10)), this.trigger("data", t);
        return;
      }
      if (e = /^#EXT-X-VERSION:([0-9.]*)?/.exec(s), e) {
        t = {
          type: "tag",
          tagType: "version"
        }, e[1] && (t.version = parseInt(e[1], 10)), this.trigger("data", t);
        return;
      }
      if (e = /^#EXT-X-MEDIA-SEQUENCE:(\-?[0-9.]*)?/.exec(s), e) {
        t = {
          type: "tag",
          tagType: "media-sequence"
        }, e[1] && (t.number = parseInt(e[1], 10)), this.trigger("data", t);
        return;
      }
      if (e = /^#EXT-X-DISCONTINUITY-SEQUENCE:(\-?[0-9.]*)?/.exec(s), e) {
        t = {
          type: "tag",
          tagType: "discontinuity-sequence"
        }, e[1] && (t.number = parseInt(e[1], 10)), this.trigger("data", t);
        return;
      }
      if (e = /^#EXT-X-PLAYLIST-TYPE:(.*)?$/.exec(s), e) {
        t = {
          type: "tag",
          tagType: "playlist-type"
        }, e[1] && (t.playlistType = e[1]), this.trigger("data", t);
        return;
      }
      if (e = /^#EXT-X-BYTERANGE:(.*)?$/.exec(s), e) {
        t = I(D(e[1]), {
          type: "tag",
          tagType: "byterange"
        }), this.trigger("data", t);
        return;
      }
      if (e = /^#EXT-X-ALLOW-CACHE:(YES|NO)?/.exec(s), e) {
        t = {
          type: "tag",
          tagType: "allow-cache"
        }, e[1] && (t.allowed = !/NO/.test(e[1])), this.trigger("data", t);
        return;
      }
      if (e = /^#EXT-X-MAP:(.*)$/.exec(s), e) {
        if (t = {
          type: "tag",
          tagType: "map"
        }, e[1]) {
          const u = h(e[1]);
          u.URI && (t.uri = u.URI), u.BYTERANGE && (t.byterange = D(u.BYTERANGE));
        }
        this.trigger("data", t);
        return;
      }
      if (e = /^#EXT-X-STREAM-INF:(.*)$/.exec(s), e) {
        t = {
          type: "tag",
          tagType: "stream-inf"
        }, e[1] && (t.attributes = h(e[1]), t.attributes.RESOLUTION && (t.attributes.RESOLUTION = M(t.attributes.RESOLUTION)), t.attributes.BANDWIDTH && (t.attributes.BANDWIDTH = parseInt(t.attributes.BANDWIDTH, 10)), t.attributes["FRAME-RATE"] && (t.attributes["FRAME-RATE"] = parseFloat(t.attributes["FRAME-RATE"])), t.attributes["PROGRAM-ID"] && (t.attributes["PROGRAM-ID"] = parseInt(t.attributes["PROGRAM-ID"], 10))), this.trigger("data", t);
        return;
      }
      if (e = /^#EXT-X-MEDIA:(.*)$/.exec(s), e) {
        t = {
          type: "tag",
          tagType: "media"
        }, e[1] && (t.attributes = h(e[1])), this.trigger("data", t);
        return;
      }
      if (e = /^#EXT-X-ENDLIST/.exec(s), e) {
        this.trigger("data", {
          type: "tag",
          tagType: "endlist"
        });
        return;
      }
      if (e = /^#EXT-X-DISCONTINUITY/.exec(s), e) {
        this.trigger("data", {
          type: "tag",
          tagType: "discontinuity"
        });
        return;
      }
      if (e = /^#EXT-X-PROGRAM-DATE-TIME:(.*)$/.exec(s), e) {
        t = {
          type: "tag",
          tagType: "program-date-time"
        }, e[1] && (t.dateTimeString = e[1], t.dateTimeObject = new Date(e[1])), this.trigger("data", t);
        return;
      }
      if (e = /^#EXT-X-KEY:(.*)$/.exec(s), e) {
        t = {
          type: "tag",
          tagType: "key"
        }, e[1] && (t.attributes = h(e[1]), t.attributes.IV && (t.attributes.IV.substring(0, 2).toLowerCase() === "0x" && (t.attributes.IV = t.attributes.IV.substring(2)), t.attributes.IV = t.attributes.IV.match(/.{8}/g), t.attributes.IV[0] = parseInt(t.attributes.IV[0], 16), t.attributes.IV[1] = parseInt(t.attributes.IV[1], 16), t.attributes.IV[2] = parseInt(t.attributes.IV[2], 16), t.attributes.IV[3] = parseInt(t.attributes.IV[3], 16), t.attributes.IV = new Uint32Array(t.attributes.IV))), this.trigger("data", t);
        return;
      }
      if (e = /^#EXT-X-START:(.*)$/.exec(s), e) {
        t = {
          type: "tag",
          tagType: "start"
        }, e[1] && (t.attributes = h(e[1]), t.attributes["TIME-OFFSET"] = parseFloat(t.attributes["TIME-OFFSET"]), t.attributes.PRECISE = /YES/.test(t.attributes.PRECISE)), this.trigger("data", t);
        return;
      }
      if (e = /^#EXT-X-CUE-OUT-CONT:(.*)?$/.exec(s), e) {
        t = {
          type: "tag",
          tagType: "cue-out-cont"
        }, e[1] ? t.data = e[1] : t.data = "", this.trigger("data", t);
        return;
      }
      if (e = /^#EXT-X-CUE-OUT:(.*)?$/.exec(s), e) {
        t = {
          type: "tag",
          tagType: "cue-out"
        }, e[1] ? t.data = e[1] : t.data = "", this.trigger("data", t);
        return;
      }
      if (e = /^#EXT-X-CUE-IN:?(.*)?$/.exec(s), e) {
        t = {
          type: "tag",
          tagType: "cue-in"
        }, e[1] ? t.data = e[1] : t.data = "", this.trigger("data", t);
        return;
      }
      if (e = /^#EXT-X-SKIP:(.*)$/.exec(s), e && e[1]) {
        t = {
          type: "tag",
          tagType: "skip"
        }, t.attributes = h(e[1]), t.attributes.hasOwnProperty("SKIPPED-SEGMENTS") && (t.attributes["SKIPPED-SEGMENTS"] = parseInt(t.attributes["SKIPPED-SEGMENTS"], 10)), t.attributes.hasOwnProperty("RECENTLY-REMOVED-DATERANGES") && (t.attributes["RECENTLY-REMOVED-DATERANGES"] = t.attributes["RECENTLY-REMOVED-DATERANGES"].split(x)), this.trigger("data", t);
        return;
      }
      if (e = /^#EXT-X-PART:(.*)$/.exec(s), e && e[1]) {
        t = {
          type: "tag",
          tagType: "part"
        }, t.attributes = h(e[1]), ["DURATION"].forEach(function(u) {
          t.attributes.hasOwnProperty(u) && (t.attributes[u] = parseFloat(t.attributes[u]));
        }), ["INDEPENDENT", "GAP"].forEach(function(u) {
          t.attributes.hasOwnProperty(u) && (t.attributes[u] = /YES/.test(t.attributes[u]));
        }), t.attributes.hasOwnProperty("BYTERANGE") && (t.attributes.byterange = D(t.attributes.BYTERANGE)), this.trigger("data", t);
        return;
      }
      if (e = /^#EXT-X-SERVER-CONTROL:(.*)$/.exec(s), e && e[1]) {
        t = {
          type: "tag",
          tagType: "server-control"
        }, t.attributes = h(e[1]), ["CAN-SKIP-UNTIL", "PART-HOLD-BACK", "HOLD-BACK"].forEach(function(u) {
          t.attributes.hasOwnProperty(u) && (t.attributes[u] = parseFloat(t.attributes[u]));
        }), ["CAN-SKIP-DATERANGES", "CAN-BLOCK-RELOAD"].forEach(function(u) {
          t.attributes.hasOwnProperty(u) && (t.attributes[u] = /YES/.test(t.attributes[u]));
        }), this.trigger("data", t);
        return;
      }
      if (e = /^#EXT-X-PART-INF:(.*)$/.exec(s), e && e[1]) {
        t = {
          type: "tag",
          tagType: "part-inf"
        }, t.attributes = h(e[1]), ["PART-TARGET"].forEach(function(u) {
          t.attributes.hasOwnProperty(u) && (t.attributes[u] = parseFloat(t.attributes[u]));
        }), this.trigger("data", t);
        return;
      }
      if (e = /^#EXT-X-PRELOAD-HINT:(.*)$/.exec(s), e && e[1]) {
        t = {
          type: "tag",
          tagType: "preload-hint"
        }, t.attributes = h(e[1]), ["BYTERANGE-START", "BYTERANGE-LENGTH"].forEach(function(u) {
          if (t.attributes.hasOwnProperty(u)) {
            t.attributes[u] = parseInt(t.attributes[u], 10);
            const o = u === "BYTERANGE-LENGTH" ? "length" : "offset";
            t.attributes.byterange = t.attributes.byterange || {}, t.attributes.byterange[o] = t.attributes[u], delete t.attributes[u];
          }
        }), this.trigger("data", t);
        return;
      }
      if (e = /^#EXT-X-RENDITION-REPORT:(.*)$/.exec(s), e && e[1]) {
        t = {
          type: "tag",
          tagType: "rendition-report"
        }, t.attributes = h(e[1]), ["LAST-MSN", "LAST-PART"].forEach(function(u) {
          t.attributes.hasOwnProperty(u) && (t.attributes[u] = parseInt(t.attributes[u], 10));
        }), this.trigger("data", t);
        return;
      }
      if (e = /^#EXT-X-DATERANGE:(.*)$/.exec(s), e && e[1]) {
        t = {
          type: "tag",
          tagType: "daterange"
        }, t.attributes = h(e[1]), ["ID", "CLASS"].forEach(function(o) {
          t.attributes.hasOwnProperty(o) && (t.attributes[o] = String(t.attributes[o]));
        }), ["START-DATE", "END-DATE"].forEach(function(o) {
          t.attributes.hasOwnProperty(o) && (t.attributes[o] = new Date(t.attributes[o]));
        }), ["DURATION", "PLANNED-DURATION"].forEach(function(o) {
          t.attributes.hasOwnProperty(o) && (t.attributes[o] = parseFloat(t.attributes[o]));
        }), ["END-ON-NEXT"].forEach(function(o) {
          t.attributes.hasOwnProperty(o) && (t.attributes[o] = /YES/i.test(t.attributes[o]));
        }), ["SCTE35-CMD", " SCTE35-OUT", "SCTE35-IN"].forEach(function(o) {
          t.attributes.hasOwnProperty(o) && (t.attributes[o] = t.attributes[o].toString(16));
        });
        const u = /^X-([A-Z]+-)+[A-Z]+$/;
        for (const o in t.attributes) {
          if (!u.test(o))
            continue;
          const d = /[0-9A-Fa-f]{6}/g.test(t.attributes[o]), R = /^\d+(\.\d+)?$/.test(t.attributes[o]);
          t.attributes[o] = d ? t.attributes[o].toString(16) : R ? parseFloat(t.attributes[o]) : String(t.attributes[o]);
        }
        this.trigger("data", t);
        return;
      }
      if (e = /^#EXT-X-INDEPENDENT-SEGMENTS/.exec(s), e) {
        this.trigger("data", {
          type: "tag",
          tagType: "independent-segments"
        });
        return;
      }
      if (e = /^#EXT-X-I-FRAMES-ONLY/.exec(s), e) {
        this.trigger("data", {
          type: "tag",
          tagType: "i-frames-only"
        });
        return;
      }
      if (e = /^#EXT-X-CONTENT-STEERING:(.*)$/.exec(s), e) {
        t = {
          type: "tag",
          tagType: "content-steering"
        }, t.attributes = h(e[1]), this.trigger("data", t);
        return;
      }
      if (e = /^#EXT-X-I-FRAME-STREAM-INF:(.*)$/.exec(s), e) {
        t = {
          type: "tag",
          tagType: "i-frame-playlist"
        }, t.attributes = h(e[1]), t.attributes.URI && (t.uri = t.attributes.URI), t.attributes.BANDWIDTH && (t.attributes.BANDWIDTH = parseInt(t.attributes.BANDWIDTH, 10)), t.attributes.RESOLUTION && (t.attributes.RESOLUTION = M(t.attributes.RESOLUTION)), t.attributes["AVERAGE-BANDWIDTH"] && (t.attributes["AVERAGE-BANDWIDTH"] = parseInt(t.attributes["AVERAGE-BANDWIDTH"], 10)), t.attributes["FRAME-RATE"] && (t.attributes["FRAME-RATE"] = parseFloat(t.attributes["FRAME-RATE"])), this.trigger("data", t);
        return;
      }
      if (e = /^#EXT-X-DEFINE:(.*)$/.exec(s), e) {
        t = {
          type: "tag",
          tagType: "define"
        }, t.attributes = h(e[1]), this.trigger("data", t);
        return;
      }
      this.trigger("data", {
        type: "tag",
        data: s.slice(4)
      });
    });
  }
  /**
   * Add a parser for custom headers
   *
   * @param {Object}   options              a map of options for the added parser
   * @param {RegExp}   options.expression   a regular expression to match the custom header
   * @param {string}   options.customType   the custom type to register to the output
   * @param {Function} [options.dataParser] function to parse the line into an object
   * @param {boolean}  [options.segment]    should tag data be attached to the segment object
   */
  addParser({
    expression: n,
    customType: e,
    dataParser: t,
    segment: a
  }) {
    typeof t != "function" && (t = (s) => s), this.customParsers.push((s) => {
      if (n.exec(s))
        return this.trigger("data", {
          type: "custom",
          data: t(s),
          customType: e,
          segment: a
        }), !0;
    });
  }
  /**
   * Add a custom header mapper
   *
   * @param {Object}   options
   * @param {RegExp}   options.expression   a regular expression to match the custom header
   * @param {Function} options.map          function to translate tag into a different tag
   */
  addTagMapper({
    expression: n,
    map: e
  }) {
    const t = (a) => n.test(a) ? e(a) : a;
    this.tagMappers.push(t);
  }
}
const F = (f) => f.toLowerCase().replace(/-(\w)/g, (n) => n[1].toUpperCase()), c = function(f) {
  const n = {};
  return Object.keys(f).forEach(function(e) {
    n[F(e)] = f[e];
  }), n;
}, N = function(f) {
  const {
    serverControl: n,
    targetDuration: e,
    partTargetDuration: t
  } = f;
  if (!n)
    return;
  const a = "#EXT-X-SERVER-CONTROL", s = "holdBack", u = "partHoldBack", o = e && e * 3, d = t && t * 2;
  e && !n.hasOwnProperty(s) && (n[s] = o, this.trigger("info", {
    message: `${a} defaulting HOLD-BACK to targetDuration * 3 (${o}).`
  })), o && n[s] < o && (this.trigger("warn", {
    message: `${a} clamping HOLD-BACK (${n[s]}) to targetDuration * 3 (${o})`
  }), n[s] = o), t && !n.hasOwnProperty(u) && (n[u] = t * 3, this.trigger("info", {
    message: `${a} defaulting PART-HOLD-BACK to partTargetDuration * 3 (${n[u]}).`
  })), t && n[u] < d && (this.trigger("warn", {
    message: `${a} clamping PART-HOLD-BACK (${n[u]}) to partTargetDuration * 2 (${d}).`
  }), n[u] = d);
};
class Y extends O {
  constructor(n = {}) {
    super(), this.lineStream = new w(), this.parseStream = new G(), this.lineStream.pipe(this.parseStream), this.mainDefinitions = n.mainDefinitions || {}, this.params = new URL(n.uri, "https://a.com").searchParams, this.lastProgramDateTime = null;
    const e = this, t = [];
    let a = {}, s, u, o = !1;
    const d = function() {
    }, R = {
      AUDIO: {},
      VIDEO: {},
      "CLOSED-CAPTIONS": {},
      SUBTITLES: {}
    }, C = "urn:uuid:edef8ba9-79d6-4ace-a3c8-27dcd51d21ed";
    let b = 0;
    this.manifest = {
      allowCache: !0,
      discontinuityStarts: [],
      dateRanges: [],
      iFramePlaylists: [],
      segments: []
    };
    let S = 0, A = 0;
    const l = {};
    this.on("end", () => {
      a.uri || !a.parts && !a.preloadHints || (!a.map && s && (a.map = s), !a.key && u && (a.key = u), !a.timeline && typeof b == "number" && (a.timeline = b), this.manifest.preloadSegment = a);
    }), this.parseStream.on("data", function(i) {
      let P, p;
      if (e.manifest.definitions) {
        for (const g in e.manifest.definitions)
          if (i.uri && (i.uri = i.uri.replace(`{$${g}}`, e.manifest.definitions[g])), i.attributes)
            for (const r in i.attributes)
              typeof i.attributes[r] == "string" && (i.attributes[r] = i.attributes[r].replace(`{$${g}}`, e.manifest.definitions[g]));
      }
      ({
        tag() {
          ({
            version() {
              i.version && (this.manifest.version = i.version);
            },
            "allow-cache"() {
              this.manifest.allowCache = i.allowed, "allowed" in i || (this.trigger("info", {
                message: "defaulting allowCache to YES"
              }), this.manifest.allowCache = !0);
            },
            byterange() {
              const g = {};
              "length" in i && (a.byterange = g, g.length = i.length, "offset" in i || (i.offset = S)), "offset" in i && (a.byterange = g, g.offset = i.offset), S = g.offset + g.length;
            },
            endlist() {
              this.manifest.endList = !0;
            },
            inf() {
              "mediaSequence" in this.manifest || (this.manifest.mediaSequence = 0, this.trigger("info", {
                message: "defaulting media sequence to zero"
              })), "discontinuitySequence" in this.manifest || (this.manifest.discontinuitySequence = 0, this.trigger("info", {
                message: "defaulting discontinuity sequence to zero"
              })), i.title && (a.title = i.title), i.duration > 0 && (a.duration = i.duration), i.duration === 0 && (a.duration = 0.01, this.trigger("info", {
                message: "updating zero segment duration to a small value"
              })), this.manifest.segments = t;
            },
            key() {
              if (!i.attributes) {
                this.trigger("warn", {
                  message: "ignoring key declaration without attribute list"
                });
                return;
              }
              if (i.attributes.METHOD === "NONE") {
                u = null;
                return;
              }
              if (!i.attributes.URI) {
                this.trigger("warn", {
                  message: "ignoring key declaration without URI"
                });
                return;
              }
              if (i.attributes.KEYFORMAT === "com.apple.streamingkeydelivery") {
                this.manifest.contentProtection = this.manifest.contentProtection || {}, this.manifest.contentProtection["com.apple.fps.1_0"] = {
                  attributes: i.attributes
                };
                return;
              }
              if (i.attributes.KEYFORMAT === "com.microsoft.playready") {
                this.manifest.contentProtection = this.manifest.contentProtection || {}, this.manifest.contentProtection["com.microsoft.playready"] = {
                  uri: i.attributes.URI
                };
                return;
              }
              if (i.attributes.KEYFORMAT === C) {
                if (["SAMPLE-AES", "SAMPLE-AES-CTR", "SAMPLE-AES-CENC"].indexOf(i.attributes.METHOD) === -1) {
                  this.trigger("warn", {
                    message: "invalid key method provided for Widevine"
                  });
                  return;
                }
                if (i.attributes.METHOD === "SAMPLE-AES-CENC" && this.trigger("warn", {
                  message: "SAMPLE-AES-CENC is deprecated, please use SAMPLE-AES-CTR instead"
                }), i.attributes.URI.substring(0, 23) !== "data:text/plain;base64,") {
                  this.trigger("warn", {
                    message: "invalid key URI provided for Widevine"
                  });
                  return;
                }
                if (!(i.attributes.KEYID && i.attributes.KEYID.substring(0, 2) === "0x")) {
                  this.trigger("warn", {
                    message: "invalid key ID provided for Widevine"
                  });
                  return;
                }
                this.manifest.contentProtection = this.manifest.contentProtection || {}, this.manifest.contentProtection["com.widevine.alpha"] = {
                  attributes: {
                    schemeIdUri: i.attributes.KEYFORMAT,
                    // remove '0x' from the key id string
                    keyId: i.attributes.KEYID.substring(2)
                  },
                  // decode the base64-encoded PSSH box
                  pssh: U(i.attributes.URI.split(",")[1])
                };
                return;
              }
              i.attributes.METHOD || this.trigger("warn", {
                message: "defaulting key method to AES-128"
              }), u = {
                method: i.attributes.METHOD || "AES-128",
                uri: i.attributes.URI
              }, typeof i.attributes.IV < "u" && (u.iv = i.attributes.IV);
            },
            "media-sequence"() {
              if (!isFinite(i.number)) {
                this.trigger("warn", {
                  message: "ignoring invalid media sequence: " + i.number
                });
                return;
              }
              this.manifest.mediaSequence = i.number;
            },
            "discontinuity-sequence"() {
              if (!isFinite(i.number)) {
                this.trigger("warn", {
                  message: "ignoring invalid discontinuity sequence: " + i.number
                });
                return;
              }
              this.manifest.discontinuitySequence = i.number, b = i.number;
            },
            "playlist-type"() {
              if (!/VOD|EVENT/.test(i.playlistType)) {
                this.trigger("warn", {
                  message: "ignoring unknown playlist type: " + i.playlist
                });
                return;
              }
              this.manifest.playlistType = i.playlistType;
            },
            map() {
              s = {}, i.uri && (s.uri = i.uri), i.byterange && (s.byterange = i.byterange), u && (s.key = u);
            },
            "stream-inf"() {
              if (this.manifest.playlists = t, this.manifest.mediaGroups = this.manifest.mediaGroups || R, !i.attributes) {
                this.trigger("warn", {
                  message: "ignoring empty stream-inf attributes"
                });
                return;
              }
              a.attributes || (a.attributes = {}), I(a.attributes, i.attributes);
            },
            media() {
              if (this.manifest.mediaGroups = this.manifest.mediaGroups || R, !(i.attributes && i.attributes.TYPE && i.attributes["GROUP-ID"] && i.attributes.NAME)) {
                this.trigger("warn", {
                  message: "ignoring incomplete or missing media group"
                });
                return;
              }
              const g = this.manifest.mediaGroups[i.attributes.TYPE];
              g[i.attributes["GROUP-ID"]] = g[i.attributes["GROUP-ID"]] || {}, P = g[i.attributes["GROUP-ID"]], p = {
                default: /yes/i.test(i.attributes.DEFAULT)
              }, p.default ? p.autoselect = !0 : p.autoselect = /yes/i.test(i.attributes.AUTOSELECT), i.attributes.LANGUAGE && (p.language = i.attributes.LANGUAGE), i.attributes.URI && (p.uri = i.attributes.URI), i.attributes["INSTREAM-ID"] && (p.instreamId = i.attributes["INSTREAM-ID"]), i.attributes.CHARACTERISTICS && (p.characteristics = i.attributes.CHARACTERISTICS), i.attributes.FORCED && (p.forced = /yes/i.test(i.attributes.FORCED)), P[i.attributes.NAME] = p;
            },
            discontinuity() {
              b += 1, a.discontinuity = !0, this.manifest.discontinuityStarts.push(t.length);
            },
            "program-date-time"() {
              typeof this.manifest.dateTimeString > "u" && (this.manifest.dateTimeString = i.dateTimeString, this.manifest.dateTimeObject = i.dateTimeObject), a.dateTimeString = i.dateTimeString, a.dateTimeObject = i.dateTimeObject;
              const {
                lastProgramDateTime: g
              } = this;
              this.lastProgramDateTime = new Date(i.dateTimeString).getTime(), g === null && this.manifest.segments.reduceRight((r, E) => (E.programDateTime = r - E.duration * 1e3, E.programDateTime), this.lastProgramDateTime);
            },
            targetduration() {
              if (!isFinite(i.duration) || i.duration < 0) {
                this.trigger("warn", {
                  message: "ignoring invalid target duration: " + i.duration
                });
                return;
              }
              this.manifest.targetDuration = i.duration, N.call(this, this.manifest);
            },
            start() {
              if (!i.attributes || isNaN(i.attributes["TIME-OFFSET"])) {
                this.trigger("warn", {
                  message: "ignoring start declaration without appropriate attribute list"
                });
                return;
              }
              this.manifest.start = {
                timeOffset: i.attributes["TIME-OFFSET"],
                precise: i.attributes.PRECISE
              };
            },
            "cue-out"() {
              a.cueOut = i.data;
            },
            "cue-out-cont"() {
              a.cueOutCont = i.data;
            },
            "cue-in"() {
              a.cueIn = i.data;
            },
            skip() {
              this.manifest.skip = c(i.attributes), this.warnOnMissingAttributes_("#EXT-X-SKIP", i.attributes, ["SKIPPED-SEGMENTS"]);
            },
            part() {
              o = !0;
              const g = this.manifest.segments.length, r = c(i.attributes);
              a.parts = a.parts || [], a.parts.push(r), r.byterange && (r.byterange.hasOwnProperty("offset") || (r.byterange.offset = A), A = r.byterange.offset + r.byterange.length);
              const E = a.parts.length - 1;
              this.warnOnMissingAttributes_(`#EXT-X-PART #${E} for segment #${g}`, i.attributes, ["URI", "DURATION"]), this.manifest.renditionReports && this.manifest.renditionReports.forEach((m, T) => {
                m.hasOwnProperty("lastPart") || this.trigger("warn", {
                  message: `#EXT-X-RENDITION-REPORT #${T} lacks required attribute(s): LAST-PART`
                });
              });
            },
            "server-control"() {
              const g = this.manifest.serverControl = c(i.attributes);
              g.hasOwnProperty("canBlockReload") || (g.canBlockReload = !1, this.trigger("info", {
                message: "#EXT-X-SERVER-CONTROL defaulting CAN-BLOCK-RELOAD to false"
              })), N.call(this, this.manifest), g.canSkipDateranges && !g.hasOwnProperty("canSkipUntil") && this.trigger("warn", {
                message: "#EXT-X-SERVER-CONTROL lacks required attribute CAN-SKIP-UNTIL which is required when CAN-SKIP-DATERANGES is set"
              });
            },
            "preload-hint"() {
              const g = this.manifest.segments.length, r = c(i.attributes), E = r.type && r.type === "PART";
              a.preloadHints = a.preloadHints || [], a.preloadHints.push(r), r.byterange && (r.byterange.hasOwnProperty("offset") || (r.byterange.offset = E ? A : 0, E && (A = r.byterange.offset + r.byterange.length)));
              const m = a.preloadHints.length - 1;
              if (this.warnOnMissingAttributes_(`#EXT-X-PRELOAD-HINT #${m} for segment #${g}`, i.attributes, ["TYPE", "URI"]), !!r.type)
                for (let T = 0; T < a.preloadHints.length - 1; T++) {
                  const X = a.preloadHints[T];
                  X.type && X.type === r.type && this.trigger("warn", {
                    message: `#EXT-X-PRELOAD-HINT #${m} for segment #${g} has the same TYPE ${r.type} as preload hint #${T}`
                  });
                }
            },
            "rendition-report"() {
              const g = c(i.attributes);
              this.manifest.renditionReports = this.manifest.renditionReports || [], this.manifest.renditionReports.push(g);
              const r = this.manifest.renditionReports.length - 1, E = ["LAST-MSN", "URI"];
              o && E.push("LAST-PART"), this.warnOnMissingAttributes_(`#EXT-X-RENDITION-REPORT #${r}`, i.attributes, E);
            },
            "part-inf"() {
              this.manifest.partInf = c(i.attributes), this.warnOnMissingAttributes_("#EXT-X-PART-INF", i.attributes, ["PART-TARGET"]), this.manifest.partInf.partTarget && (this.manifest.partTargetDuration = this.manifest.partInf.partTarget), N.call(this, this.manifest);
            },
            daterange() {
              this.manifest.dateRanges.push(c(i.attributes));
              const g = this.manifest.dateRanges.length - 1;
              this.warnOnMissingAttributes_(`#EXT-X-DATERANGE #${g}`, i.attributes, ["ID", "START-DATE"]);
              const r = this.manifest.dateRanges[g];
              r.endDate && r.startDate && new Date(r.endDate) < new Date(r.startDate) && this.trigger("warn", {
                message: "EXT-X-DATERANGE END-DATE must be equal to or later than the value of the START-DATE"
              }), r.duration && r.duration < 0 && this.trigger("warn", {
                message: "EXT-X-DATERANGE DURATION must not be negative"
              }), r.plannedDuration && r.plannedDuration < 0 && this.trigger("warn", {
                message: "EXT-X-DATERANGE PLANNED-DURATION must not be negative"
              });
              const E = !!r.endOnNext;
              if (E && !r.class && this.trigger("warn", {
                message: "EXT-X-DATERANGE with an END-ON-NEXT=YES attribute must have a CLASS attribute"
              }), E && (r.duration || r.endDate) && this.trigger("warn", {
                message: "EXT-X-DATERANGE with an END-ON-NEXT=YES attribute must not contain DURATION or END-DATE attributes"
              }), r.duration && r.endDate) {
                const T = r.startDate.getTime() + r.duration * 1e3;
                this.manifest.dateRanges[g].endDate = new Date(T);
              }
              if (!l[r.id])
                l[r.id] = r;
              else {
                for (const T in l[r.id])
                  if (r[T] && JSON.stringify(l[r.id][T]) !== JSON.stringify(r[T])) {
                    this.trigger("warn", {
                      message: "EXT-X-DATERANGE tags with the same ID in a playlist must have the same attributes values"
                    });
                    break;
                  }
                const m = this.manifest.dateRanges.findIndex((T) => T.id === r.id);
                this.manifest.dateRanges[m] = I(this.manifest.dateRanges[m], r), l[r.id] = I(l[r.id], r), this.manifest.dateRanges.pop();
              }
            },
            "independent-segments"() {
              this.manifest.independentSegments = !0;
            },
            "i-frames-only"() {
              this.manifest.iFramesOnly = !0, this.requiredCompatibilityversion(this.manifest.version, 4);
            },
            "content-steering"() {
              this.manifest.contentSteering = c(i.attributes), this.warnOnMissingAttributes_("#EXT-X-CONTENT-STEERING", i.attributes, ["SERVER-URI"]);
            },
            /** @this {Parser} */
            define() {
              this.manifest.definitions = this.manifest.definitions || {};
              const g = (r, E) => {
                if (r in this.manifest.definitions) {
                  this.trigger("error", {
                    message: `EXT-X-DEFINE: Duplicate name ${r}`
                  });
                  return;
                }
                this.manifest.definitions[r] = E;
              };
              if ("QUERYPARAM" in i.attributes) {
                if ("NAME" in i.attributes || "IMPORT" in i.attributes) {
                  this.trigger("error", {
                    message: "EXT-X-DEFINE: Invalid attributes"
                  });
                  return;
                }
                const r = this.params.get(i.attributes.QUERYPARAM);
                if (!r) {
                  this.trigger("error", {
                    message: `EXT-X-DEFINE: No query param ${i.attributes.QUERYPARAM}`
                  });
                  return;
                }
                g(i.attributes.QUERYPARAM, decodeURIComponent(r));
                return;
              }
              if ("NAME" in i.attributes) {
                if ("IMPORT" in i.attributes) {
                  this.trigger("error", {
                    message: "EXT-X-DEFINE: Invalid attributes"
                  });
                  return;
                }
                if (!("VALUE" in i.attributes) || typeof i.attributes.VALUE != "string") {
                  this.trigger("error", {
                    message: `EXT-X-DEFINE: No value for ${i.attributes.NAME}`
                  });
                  return;
                }
                g(i.attributes.NAME, i.attributes.VALUE);
                return;
              }
              if ("IMPORT" in i.attributes) {
                if (!this.mainDefinitions[i.attributes.IMPORT]) {
                  this.trigger("error", {
                    message: `EXT-X-DEFINE: No value ${i.attributes.IMPORT} to import, or IMPORT used on main playlist`
                  });
                  return;
                }
                g(i.attributes.IMPORT, this.mainDefinitions[i.attributes.IMPORT]);
                return;
              }
              this.trigger("error", {
                message: "EXT-X-DEFINE: No attribute"
              });
            },
            "i-frame-playlist"() {
              this.manifest.iFramePlaylists.push({
                attributes: i.attributes,
                uri: i.uri,
                timeline: b
              }), this.warnOnMissingAttributes_("#EXT-X-I-FRAME-STREAM-INF", i.attributes, ["BANDWIDTH", "URI"]);
            }
          }[i.tagType] || d).call(e);
        },
        uri() {
          a.uri = i.uri, t.push(a), this.manifest.targetDuration && !("duration" in a) && (this.trigger("warn", {
            message: "defaulting segment duration to the target duration"
          }), a.duration = this.manifest.targetDuration), u && (a.key = u), a.timeline = b, s && (a.map = s), A = 0, this.lastProgramDateTime !== null && (a.programDateTime = this.lastProgramDateTime, this.lastProgramDateTime += a.duration * 1e3), a = {};
        },
        comment() {
        },
        custom() {
          i.segment ? (a.custom = a.custom || {}, a.custom[i.customType] = i.data) : (this.manifest.custom = this.manifest.custom || {}, this.manifest.custom[i.customType] = i.data);
        }
      })[i.type].call(e);
    });
  }
  requiredCompatibilityversion(n, e) {
    (n < e || !n) && this.trigger("warn", {
      message: `manifest must be at least version ${e}`
    });
  }
  warnOnMissingAttributes_(n, e, t) {
    const a = [];
    t.forEach(function(s) {
      e.hasOwnProperty(s) || a.push(s);
    }), a.length && this.trigger("warn", {
      message: `${n} lacks required attribute(s): ${a.join(", ")}`
    });
  }
  /**
   * Parse the input string and update the manifest object.
   *
   * @param {string} chunk a potentially incomplete portion of the manifest
   */
  push(n) {
    this.lineStream.push(n);
  }
  /**
   * Flush any remaining input. This can be handy if the last line of an M3U8
   * manifest did not contain a trailing newline but the file has been
   * completely received.
   */
  end() {
    this.lineStream.push(`
`), this.manifest.dateRanges.length && this.lastProgramDateTime === null && this.trigger("warn", {
      message: "A playlist with EXT-X-DATERANGE tag must contain atleast one EXT-X-PROGRAM-DATE-TIME tag"
    }), this.lastProgramDateTime = null, this.trigger("end");
  }
  /**
   * Add an additional parser for non-standard tags
   *
   * @param {Object}   options              a map of options for the added parser
   * @param {RegExp}   options.expression   a regular expression to match the custom header
   * @param {string}   options.customType   the custom type to register to the output
   * @param {Function} [options.dataParser] function to parse the line into an object
   * @param {boolean}  [options.segment]    should tag data be attached to the segment object
   */
  addParser(n) {
    this.parseStream.addParser(n);
  }
  /**
   * Add a custom header mapper
   *
   * @param {Object}   options
   * @param {RegExp}   options.expression   a regular expression to match the custom header
   * @param {Function} options.map          function to translate tag into a different tag
   */
  addTagMapper(n) {
    this.parseStream.addTagMapper(n);
  }
}
export {
  w as LineStream,
  G as ParseStream,
  Y as Parser
};
//# sourceMappingURL=m3u8-parser.es.js.map
