import { toUint8 as t, bytesMatch as o } from "./index-byte-helpers.js";
import { findBox as v } from "./index-mp4-helpers.js";
import { findEbml as c, EBML_TAGS as i } from "./index-ebml-helpers.js";
import { getId3Offset as a } from "./index-id3-helpers.js";
import { findH265Nal as l, findH264Nal as p } from "./index-nal-helpers.js";
var x = {
  // "webm" string literal in hex
  webm: t([119, 101, 98, 109]),
  // "matroska" string literal in hex
  matroska: t([109, 97, 116, 114, 111, 115, 107, 97]),
  // "fLaC" string literal in hex
  flac: t([102, 76, 97, 67]),
  // "OggS" string literal in hex
  ogg: t([79, 103, 103, 83]),
  // ac-3 sync byte, also works for ec-3 as that is simply a codec
  // of ac-3
  ac3: t([11, 119]),
  // "RIFF" string literal in hex used for wav and avi
  riff: t([82, 73, 70, 70]),
  // "AVI" string literal in hex
  avi: t([65, 86, 73]),
  // "WAVE" string literal in hex
  wav: t([87, 65, 86, 69]),
  // "ftyp3g" string literal in hex
  "3gp": t([102, 116, 121, 112, 51, 103]),
  // "ftyp" string literal in hex
  mp4: t([102, 116, 121, 112]),
  // "styp" string literal in hex
  fmp4: t([115, 116, 121, 112]),
  // "ftypqt" string literal in hex
  mov: t([102, 116, 121, 112, 113, 116]),
  // moov string literal in hex
  moov: t([109, 111, 111, 118]),
  // moof string literal in hex
  moof: t([109, 111, 111, 102])
}, e = {
  aac: function(f) {
    var n = a(f);
    return o(f, [255, 16], {
      offset: n,
      mask: [255, 22]
    });
  },
  mp3: function(f) {
    var n = a(f);
    return o(f, [255, 2], {
      offset: n,
      mask: [255, 6]
    });
  },
  webm: function(f) {
    var n = c(f, [i.EBML, i.DocType])[0];
    return o(n, x.webm);
  },
  mkv: function(f) {
    var n = c(f, [i.EBML, i.DocType])[0];
    return o(n, x.matroska);
  },
  mp4: function(f) {
    if (e["3gp"](f) || e.mov(f))
      return !1;
    if (o(f, x.mp4, {
      offset: 4
    }) || o(f, x.fmp4, {
      offset: 4
    }) || o(f, x.moof, {
      offset: 4
    }) || o(f, x.moov, {
      offset: 4
    }))
      return !0;
  },
  mov: function(f) {
    return o(f, x.mov, {
      offset: 4
    });
  },
  "3gp": function(f) {
    return o(f, x["3gp"], {
      offset: 4
    });
  },
  ac3: function(f) {
    var n = a(f);
    return o(f, x.ac3, {
      offset: n
    });
  },
  ts: function(f) {
    if (f.length < 189 && f.length >= 1)
      return f[0] === 71;
    for (var n = 0; n + 188 < f.length && n < 188; ) {
      if (f[n] === 71 && f[n + 188] === 71)
        return !0;
      n += 1;
    }
    return !1;
  },
  flac: function(f) {
    var n = a(f);
    return o(f, x.flac, {
      offset: n
    });
  },
  ogg: function(f) {
    return o(f, x.ogg);
  },
  avi: function(f) {
    return o(f, x.riff) && o(f, x.avi, {
      offset: 8
    });
  },
  wav: function(f) {
    return o(f, x.riff) && o(f, x.wav, {
      offset: 8
    });
  },
  h264: function(f) {
    return p(f, 7, 3).length;
  },
  h265: function(f) {
    return l(f, [32, 33], 3).length;
  }
}, u = Object.keys(e).filter(function(r) {
  return r !== "ts" && r !== "h264" && r !== "h265";
}).concat(["ts", "h264", "h265"]);
u.forEach(function(r) {
  var f = e[r];
  e[r] = function(n) {
    return f(t(n));
  };
});
var g = e, w = function(f) {
  f = t(f);
  for (var n = 0; n < u.length; n++) {
    var m = u[n];
    if (g[m](f))
      return m;
  }
  return "";
}, L = function(f) {
  return v(f, ["moof"]).length > 0;
};
export {
  w as detectContainerForBytes,
  g as isLikely,
  L as isLikelyFmp4MediaSegment
};
