import { toUint8 as h, bytesMatch as t } from "./index-byte-helpers.js";
var c = h([0, 0, 0, 1]), l = h([0, 0, 1]), e = h([0, 0, 3]), E = function(r) {
  for (var n = [], i = 1; i < r.length - 2; )
    t(r.subarray(i, i + 3), e) && (n.push(i + 2), i++), i++;
  if (n.length === 0)
    return r;
  var v = r.length - n.length, a = new Uint8Array(v), o = 0;
  for (i = 0; i < v; o++, i++)
    o === n[0] && (o++, n.shift()), a[i] = r[o];
  return a;
}, N = function(r, n, i, v) {
  r = h(r), i = [].concat(i);
  for (var a = 0, o, d = 0; a < r.length && (d < v || o); ) {
    var u = void 0;
    if (t(r.subarray(a), c) ? u = 4 : t(r.subarray(a), l) && (u = 3), !u) {
      a++;
      continue;
    }
    if (d++, o)
      return E(r.subarray(o, a));
    var f = void 0;
    n === "h264" ? f = r[a + u] & 31 : n === "h265" && (f = r[a + u] >> 1 & 63), i.indexOf(f) !== -1 && (o = a + u), a += u + (n === "h264" ? 1 : 2);
  }
  return r.subarray(0, 0);
}, w = function(r, n, i) {
  return N(r, "h264", n, i);
}, P = function(r, n, i) {
  return N(r, "h265", n, i);
};
export {
  e as EMULATION_PREVENTION,
  c as NAL_TYPE_ONE,
  l as NAL_TYPE_TWO,
  E as discardEmulationPreventionBytes,
  w as findH264Nal,
  P as findH265Nal,
  N as findNal
};
