let n = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split(""), o = [];
for (let e = 0; e < n.length; e++)
  o[n[e].charCodeAt(0)] = e;
o[45] = n.indexOf("+");
o[95] = n.indexOf("/");
const h = {
  /**
   * Decodes a base64 string to a byte array.
   *
   * - ignores white-space, including line breaks and tabs
   * - allows inner padding (can decode concatenated base64 strings)
   * - does not require padding
   * - understands base64url encoding:
   *   "-" instead of "+",
   *   "_" instead of "/",
   *   no padding
   */
  dec(e) {
    let t = e.length * 3 / 4;
    e[e.length - 2] == "=" ? t -= 2 : e[e.length - 1] == "=" && (t -= 1);
    let i = new Uint8Array(t), a = 0, r = 0, c, s = 0;
    for (let l = 0; l < e.length; l++) {
      if (c = o[e.charCodeAt(l)], c === void 0)
        switch (e[l]) {
          case "=":
            r = 0;
          case `
`:
          case "\r":
          case "	":
          case " ":
            continue;
          default:
            throw Error("invalid base64 string.");
        }
      switch (r) {
        case 0:
          s = c, r = 1;
          break;
        case 1:
          i[a++] = s << 2 | (c & 48) >> 4, s = c, r = 2;
          break;
        case 2:
          i[a++] = (s & 15) << 4 | (c & 60) >> 2, s = c, r = 3;
          break;
        case 3:
          i[a++] = (s & 3) << 6 | c, r = 0;
          break;
      }
    }
    if (r == 1)
      throw Error("invalid base64 string.");
    return i.subarray(0, a);
  },
  /**
   * Encode a byte array to a base64 string.
   */
  enc(e) {
    let t = "", i = 0, a, r = 0;
    for (let c = 0; c < e.length; c++)
      switch (a = e[c], i) {
        case 0:
          t += n[a >> 2], r = (a & 3) << 4, i = 1;
          break;
        case 1:
          t += n[r | a >> 4], r = (a & 15) << 2, i = 2;
          break;
        case 2:
          t += n[r | a >> 6], t += n[a & 63], i = 0;
          break;
      }
    return i && (t += n[r], t += "=", i == 1 && (t += "=")), t;
  }
};
export {
  h as protoBase64
};
