// This file is auto generated by the protocol-buffers compiler

/* eslint-disable quotes */
/* eslint-disable indent */
/* eslint-disable no-redeclare */
/* eslint-disable camelcase */

// Remember to `npm install --save protocol-buffers-encodings`
var encodings = require('protocol-buffers-encodings')
var varint = encodings.varint
var skip = encodings.skip

var YoloIndex = exports.YoloIndex = {
  buffer: true,
  encodingLength: null,
  encode: null,
  decode: null
}

var Header = exports.Header = {
  buffer: true,
  encodingLength: null,
  encode: null,
  decode: null
}

var Node = exports.Node = {
  buffer: true,
  encodingLength: null,
  encode: null,
  decode: null
}

var Extension = exports.Extension = {
  buffer: true,
  encodingLength: null,
  encode: null,
  decode: null
}

defineYoloIndex()
defineHeader()
defineNode()
defineExtension()

function defineYoloIndex () {
  var Level = YoloIndex.Level = {
    buffer: true,
    encodingLength: null,
    encode: null,
    decode: null
  }

  defineLevel()

  function defineLevel () {
    Level.encodingLength = encodingLength
    Level.encode = encode
    Level.decode = decode

    function encodingLength (obj) {
      var length = 0
      if (defined(obj.keys)) {
        var packedLen = 0
        for (var i = 0; i < obj.keys.length; i++) {
          if (!defined(obj.keys[i])) continue
          var len = encodings.varint.encodingLength(obj.keys[i])
          packedLen += len
        }
        if (packedLen) {
          length += 1 + packedLen + varint.encodingLength(packedLen)
        }
      }
      if (defined(obj.children)) {
        var packedLen = 0
        for (var i = 0; i < obj.children.length; i++) {
          if (!defined(obj.children[i])) continue
          var len = encodings.varint.encodingLength(obj.children[i])
          packedLen += len
        }
        if (packedLen) {
          length += 1 + packedLen + varint.encodingLength(packedLen)
        }
      }
      return length
    }

    function encode (obj, buf, offset) {
      if (!offset) offset = 0
      if (!buf) buf = Buffer.allocUnsafe(encodingLength(obj))
      var oldOffset = offset
      if (defined(obj.keys)) {
        var packedLen = 0
        for (var i = 0; i < obj.keys.length; i++) {
          if (!defined(obj.keys[i])) continue
          packedLen += encodings.varint.encodingLength(obj.keys[i])
        }
        if (packedLen) {
          buf[offset++] = 10
          varint.encode(packedLen, buf, offset)
          offset += varint.encode.bytes
        }
        for (var i = 0; i < obj.keys.length; i++) {
          if (!defined(obj.keys[i])) continue
          encodings.varint.encode(obj.keys[i], buf, offset)
          offset += encodings.varint.encode.bytes
        }
      }
      if (defined(obj.children)) {
        var packedLen = 0
        for (var i = 0; i < obj.children.length; i++) {
          if (!defined(obj.children[i])) continue
          packedLen += encodings.varint.encodingLength(obj.children[i])
        }
        if (packedLen) {
          buf[offset++] = 18
          varint.encode(packedLen, buf, offset)
          offset += varint.encode.bytes
        }
        for (var i = 0; i < obj.children.length; i++) {
          if (!defined(obj.children[i])) continue
          encodings.varint.encode(obj.children[i], buf, offset)
          offset += encodings.varint.encode.bytes
        }
      }
      encode.bytes = offset - oldOffset
      return buf
    }

    function decode (buf, offset, end) {
      if (!offset) offset = 0
      if (!end) end = buf.length
      if (!(end <= buf.length && offset <= buf.length)) throw new Error("Decoded message is not valid")
      var oldOffset = offset
      var obj = {
        keys: [],
        children: []
      }
      while (true) {
        if (end <= offset) {
          decode.bytes = offset - oldOffset
          return obj
        }
        var prefix = varint.decode(buf, offset)
        offset += varint.decode.bytes
        var tag = prefix >> 3
        switch (tag) {
          case 1:
          var packedEnd = varint.decode(buf, offset)
          offset += varint.decode.bytes
          packedEnd += offset
          while (offset < packedEnd) {
            obj.keys.push(encodings.varint.decode(buf, offset))
            offset += encodings.varint.decode.bytes
          }
          break
          case 2:
          var packedEnd = varint.decode(buf, offset)
          offset += varint.decode.bytes
          packedEnd += offset
          while (offset < packedEnd) {
            obj.children.push(encodings.varint.decode(buf, offset))
            offset += encodings.varint.decode.bytes
          }
          break
          default:
          offset = skip(prefix & 7, buf, offset)
        }
      }
    }
  }

  YoloIndex.encodingLength = encodingLength
  YoloIndex.encode = encode
  YoloIndex.decode = decode

  function encodingLength (obj) {
    var length = 0
    if (defined(obj.levels)) {
      for (var i = 0; i < obj.levels.length; i++) {
        if (!defined(obj.levels[i])) continue
        var len = Level.encodingLength(obj.levels[i])
        length += varint.encodingLength(len)
        length += 1 + len
      }
    }
    return length
  }

  function encode (obj, buf, offset) {
    if (!offset) offset = 0
    if (!buf) buf = Buffer.allocUnsafe(encodingLength(obj))
    var oldOffset = offset
    if (defined(obj.levels)) {
      for (var i = 0; i < obj.levels.length; i++) {
        if (!defined(obj.levels[i])) continue
        buf[offset++] = 10
        varint.encode(Level.encodingLength(obj.levels[i]), buf, offset)
        offset += varint.encode.bytes
        Level.encode(obj.levels[i], buf, offset)
        offset += Level.encode.bytes
      }
    }
    encode.bytes = offset - oldOffset
    return buf
  }

  function decode (buf, offset, end) {
    if (!offset) offset = 0
    if (!end) end = buf.length
    if (!(end <= buf.length && offset <= buf.length)) throw new Error("Decoded message is not valid")
    var oldOffset = offset
    var obj = {
      levels: []
    }
    while (true) {
      if (end <= offset) {
        decode.bytes = offset - oldOffset
        return obj
      }
      var prefix = varint.decode(buf, offset)
      offset += varint.decode.bytes
      var tag = prefix >> 3
      switch (tag) {
        case 1:
        var len = varint.decode(buf, offset)
        offset += varint.decode.bytes
        obj.levels.push(Level.decode(buf, offset, offset + len))
        offset += Level.decode.bytes
        break
        default:
        offset = skip(prefix & 7, buf, offset)
      }
    }
  }
}

function defineHeader () {
  var Metadata = Header.Metadata = {
    buffer: true,
    encodingLength: null,
    encode: null,
    decode: null
  }

  defineMetadata()

  function defineMetadata () {
    Metadata.encodingLength = encodingLength
    Metadata.encode = encode
    Metadata.decode = decode

    function encodingLength (obj) {
      var length = 0
      if (defined(obj.contentFeed)) {
        var len = encodings.bytes.encodingLength(obj.contentFeed)
        length += 1 + len
      }
      return length
    }

    function encode (obj, buf, offset) {
      if (!offset) offset = 0
      if (!buf) buf = Buffer.allocUnsafe(encodingLength(obj))
      var oldOffset = offset
      if (defined(obj.contentFeed)) {
        buf[offset++] = 10
        encodings.bytes.encode(obj.contentFeed, buf, offset)
        offset += encodings.bytes.encode.bytes
      }
      encode.bytes = offset - oldOffset
      return buf
    }

    function decode (buf, offset, end) {
      if (!offset) offset = 0
      if (!end) end = buf.length
      if (!(end <= buf.length && offset <= buf.length)) throw new Error("Decoded message is not valid")
      var oldOffset = offset
      var obj = {
        contentFeed: null
      }
      while (true) {
        if (end <= offset) {
          decode.bytes = offset - oldOffset
          return obj
        }
        var prefix = varint.decode(buf, offset)
        offset += varint.decode.bytes
        var tag = prefix >> 3
        switch (tag) {
          case 1:
          obj.contentFeed = encodings.bytes.decode(buf, offset)
          offset += encodings.bytes.decode.bytes
          break
          default:
          offset = skip(prefix & 7, buf, offset)
        }
      }
    }
  }

  Header.encodingLength = encodingLength
  Header.encode = encode
  Header.decode = decode

  function encodingLength (obj) {
    var length = 0
    if (!defined(obj.protocol)) throw new Error("protocol is required")
    var len = encodings.string.encodingLength(obj.protocol)
    length += 1 + len
    if (defined(obj.metadata)) {
      var len = Metadata.encodingLength(obj.metadata)
      length += varint.encodingLength(len)
      length += 1 + len
    }
    return length
  }

  function encode (obj, buf, offset) {
    if (!offset) offset = 0
    if (!buf) buf = Buffer.allocUnsafe(encodingLength(obj))
    var oldOffset = offset
    if (!defined(obj.protocol)) throw new Error("protocol is required")
    buf[offset++] = 10
    encodings.string.encode(obj.protocol, buf, offset)
    offset += encodings.string.encode.bytes
    if (defined(obj.metadata)) {
      buf[offset++] = 18
      varint.encode(Metadata.encodingLength(obj.metadata), buf, offset)
      offset += varint.encode.bytes
      Metadata.encode(obj.metadata, buf, offset)
      offset += Metadata.encode.bytes
    }
    encode.bytes = offset - oldOffset
    return buf
  }

  function decode (buf, offset, end) {
    if (!offset) offset = 0
    if (!end) end = buf.length
    if (!(end <= buf.length && offset <= buf.length)) throw new Error("Decoded message is not valid")
    var oldOffset = offset
    var obj = {
      protocol: "",
      metadata: null
    }
    var found0 = false
    while (true) {
      if (end <= offset) {
        if (!found0) throw new Error("Decoded message is not valid")
        decode.bytes = offset - oldOffset
        return obj
      }
      var prefix = varint.decode(buf, offset)
      offset += varint.decode.bytes
      var tag = prefix >> 3
      switch (tag) {
        case 1:
        obj.protocol = encodings.string.decode(buf, offset)
        offset += encodings.string.decode.bytes
        found0 = true
        break
        case 2:
        var len = varint.decode(buf, offset)
        offset += varint.decode.bytes
        obj.metadata = Metadata.decode(buf, offset, offset + len)
        offset += Metadata.decode.bytes
        break
        default:
        offset = skip(prefix & 7, buf, offset)
      }
    }
  }
}

function defineNode () {
  Node.encodingLength = encodingLength
  Node.encode = encode
  Node.decode = decode

  function encodingLength (obj) {
    var length = 0
    if (!defined(obj.index)) throw new Error("index is required")
    var len = encodings.bytes.encodingLength(obj.index)
    length += 1 + len
    if (!defined(obj.key)) throw new Error("key is required")
    var len = encodings.bytes.encodingLength(obj.key)
    length += 1 + len
    if (defined(obj.value)) {
      var len = encodings.bytes.encodingLength(obj.value)
      length += 1 + len
    }
    return length
  }

  function encode (obj, buf, offset) {
    if (!offset) offset = 0
    if (!buf) buf = Buffer.allocUnsafe(encodingLength(obj))
    var oldOffset = offset
    if (!defined(obj.index)) throw new Error("index is required")
    buf[offset++] = 10
    encodings.bytes.encode(obj.index, buf, offset)
    offset += encodings.bytes.encode.bytes
    if (!defined(obj.key)) throw new Error("key is required")
    buf[offset++] = 18
    encodings.bytes.encode(obj.key, buf, offset)
    offset += encodings.bytes.encode.bytes
    if (defined(obj.value)) {
      buf[offset++] = 26
      encodings.bytes.encode(obj.value, buf, offset)
      offset += encodings.bytes.encode.bytes
    }
    encode.bytes = offset - oldOffset
    return buf
  }

  function decode (buf, offset, end) {
    if (!offset) offset = 0
    if (!end) end = buf.length
    if (!(end <= buf.length && offset <= buf.length)) throw new Error("Decoded message is not valid")
    var oldOffset = offset
    var obj = {
      index: null,
      key: null,
      value: null
    }
    var found0 = false
    var found1 = false
    while (true) {
      if (end <= offset) {
        if (!found0 || !found1) throw new Error("Decoded message is not valid")
        decode.bytes = offset - oldOffset
        return obj
      }
      var prefix = varint.decode(buf, offset)
      offset += varint.decode.bytes
      var tag = prefix >> 3
      switch (tag) {
        case 1:
        obj.index = encodings.bytes.decode(buf, offset)
        offset += encodings.bytes.decode.bytes
        found0 = true
        break
        case 2:
        obj.key = encodings.bytes.decode(buf, offset)
        offset += encodings.bytes.decode.bytes
        found1 = true
        break
        case 3:
        obj.value = encodings.bytes.decode(buf, offset)
        offset += encodings.bytes.decode.bytes
        break
        default:
        offset = skip(prefix & 7, buf, offset)
      }
    }
  }
}

function defineExtension () {
  var Get = Extension.Get = {
    buffer: true,
    encodingLength: null,
    encode: null,
    decode: null
  }

  var Cache = Extension.Cache = {
    buffer: true,
    encodingLength: null,
    encode: null,
    decode: null
  }

  defineGet()
  defineCache()

  function defineGet () {
    Get.encodingLength = encodingLength
    Get.encode = encode
    Get.decode = decode

    function encodingLength (obj) {
      var length = 0
      if (defined(obj.head)) {
        var len = encodings.varint.encodingLength(obj.head)
        length += 1 + len
      }
      if (defined(obj.key)) {
        var len = encodings.bytes.encodingLength(obj.key)
        length += 1 + len
      }
      return length
    }

    function encode (obj, buf, offset) {
      if (!offset) offset = 0
      if (!buf) buf = Buffer.allocUnsafe(encodingLength(obj))
      var oldOffset = offset
      if (defined(obj.head)) {
        buf[offset++] = 8
        encodings.varint.encode(obj.head, buf, offset)
        offset += encodings.varint.encode.bytes
      }
      if (defined(obj.key)) {
        buf[offset++] = 18
        encodings.bytes.encode(obj.key, buf, offset)
        offset += encodings.bytes.encode.bytes
      }
      encode.bytes = offset - oldOffset
      return buf
    }

    function decode (buf, offset, end) {
      if (!offset) offset = 0
      if (!end) end = buf.length
      if (!(end <= buf.length && offset <= buf.length)) throw new Error("Decoded message is not valid")
      var oldOffset = offset
      var obj = {
        head: 0,
        key: null
      }
      while (true) {
        if (end <= offset) {
          decode.bytes = offset - oldOffset
          return obj
        }
        var prefix = varint.decode(buf, offset)
        offset += varint.decode.bytes
        var tag = prefix >> 3
        switch (tag) {
          case 1:
          obj.head = encodings.varint.decode(buf, offset)
          offset += encodings.varint.decode.bytes
          break
          case 2:
          obj.key = encodings.bytes.decode(buf, offset)
          offset += encodings.bytes.decode.bytes
          break
          default:
          offset = skip(prefix & 7, buf, offset)
        }
      }
    }
  }

  function defineCache () {
    Cache.encodingLength = encodingLength
    Cache.encode = encode
    Cache.decode = decode

    function encodingLength (obj) {
      var length = 0
      if (!defined(obj.start)) throw new Error("start is required")
      var len = encodings.varint.encodingLength(obj.start)
      length += 1 + len
      if (!defined(obj.end)) throw new Error("end is required")
      var len = encodings.varint.encodingLength(obj.end)
      length += 1 + len
      if (defined(obj.blocks)) {
        var packedLen = 0
        for (var i = 0; i < obj.blocks.length; i++) {
          if (!defined(obj.blocks[i])) continue
          var len = encodings.varint.encodingLength(obj.blocks[i])
          packedLen += len
        }
        if (packedLen) {
          length += 1 + packedLen + varint.encodingLength(packedLen)
        }
      }
      return length
    }

    function encode (obj, buf, offset) {
      if (!offset) offset = 0
      if (!buf) buf = Buffer.allocUnsafe(encodingLength(obj))
      var oldOffset = offset
      if (!defined(obj.start)) throw new Error("start is required")
      buf[offset++] = 8
      encodings.varint.encode(obj.start, buf, offset)
      offset += encodings.varint.encode.bytes
      if (!defined(obj.end)) throw new Error("end is required")
      buf[offset++] = 16
      encodings.varint.encode(obj.end, buf, offset)
      offset += encodings.varint.encode.bytes
      if (defined(obj.blocks)) {
        var packedLen = 0
        for (var i = 0; i < obj.blocks.length; i++) {
          if (!defined(obj.blocks[i])) continue
          packedLen += encodings.varint.encodingLength(obj.blocks[i])
        }
        if (packedLen) {
          buf[offset++] = 26
          varint.encode(packedLen, buf, offset)
          offset += varint.encode.bytes
        }
        for (var i = 0; i < obj.blocks.length; i++) {
          if (!defined(obj.blocks[i])) continue
          encodings.varint.encode(obj.blocks[i], buf, offset)
          offset += encodings.varint.encode.bytes
        }
      }
      encode.bytes = offset - oldOffset
      return buf
    }

    function decode (buf, offset, end) {
      if (!offset) offset = 0
      if (!end) end = buf.length
      if (!(end <= buf.length && offset <= buf.length)) throw new Error("Decoded message is not valid")
      var oldOffset = offset
      var obj = {
        start: 0,
        end: 0,
        blocks: []
      }
      var found0 = false
      var found1 = false
      while (true) {
        if (end <= offset) {
          if (!found0 || !found1) throw new Error("Decoded message is not valid")
          decode.bytes = offset - oldOffset
          return obj
        }
        var prefix = varint.decode(buf, offset)
        offset += varint.decode.bytes
        var tag = prefix >> 3
        switch (tag) {
          case 1:
          obj.start = encodings.varint.decode(buf, offset)
          offset += encodings.varint.decode.bytes
          found0 = true
          break
          case 2:
          obj.end = encodings.varint.decode(buf, offset)
          offset += encodings.varint.decode.bytes
          found1 = true
          break
          case 3:
          var packedEnd = varint.decode(buf, offset)
          offset += varint.decode.bytes
          packedEnd += offset
          while (offset < packedEnd) {
            obj.blocks.push(encodings.varint.decode(buf, offset))
            offset += encodings.varint.decode.bytes
          }
          break
          default:
          offset = skip(prefix & 7, buf, offset)
        }
      }
    }
  }

  Extension.encodingLength = encodingLength
  Extension.encode = encode
  Extension.decode = decode

  function encodingLength (obj) {
    var length = 0
    if (defined(obj.cache)) {
      var len = Cache.encodingLength(obj.cache)
      length += varint.encodingLength(len)
      length += 1 + len
    }
    if (defined(obj.get)) {
      var len = Get.encodingLength(obj.get)
      length += varint.encodingLength(len)
      length += 1 + len
    }
    return length
  }

  function encode (obj, buf, offset) {
    if (!offset) offset = 0
    if (!buf) buf = Buffer.allocUnsafe(encodingLength(obj))
    var oldOffset = offset
    if (defined(obj.cache)) {
      buf[offset++] = 10
      varint.encode(Cache.encodingLength(obj.cache), buf, offset)
      offset += varint.encode.bytes
      Cache.encode(obj.cache, buf, offset)
      offset += Cache.encode.bytes
    }
    if (defined(obj.get)) {
      buf[offset++] = 18
      varint.encode(Get.encodingLength(obj.get), buf, offset)
      offset += varint.encode.bytes
      Get.encode(obj.get, buf, offset)
      offset += Get.encode.bytes
    }
    encode.bytes = offset - oldOffset
    return buf
  }

  function decode (buf, offset, end) {
    if (!offset) offset = 0
    if (!end) end = buf.length
    if (!(end <= buf.length && offset <= buf.length)) throw new Error("Decoded message is not valid")
    var oldOffset = offset
    var obj = {
      cache: null,
      get: null
    }
    while (true) {
      if (end <= offset) {
        decode.bytes = offset - oldOffset
        return obj
      }
      var prefix = varint.decode(buf, offset)
      offset += varint.decode.bytes
      var tag = prefix >> 3
      switch (tag) {
        case 1:
        var len = varint.decode(buf, offset)
        offset += varint.decode.bytes
        obj.cache = Cache.decode(buf, offset, offset + len)
        offset += Cache.decode.bytes
        break
        case 2:
        var len = varint.decode(buf, offset)
        offset += varint.decode.bytes
        obj.get = Get.decode(buf, offset, offset + len)
        offset += Get.decode.bytes
        break
        default:
        offset = skip(prefix & 7, buf, offset)
      }
    }
  }
}

function defined (val) {
  return val !== null && val !== undefined && (typeof val !== 'number' || !isNaN(val))
}
