'use strict'

function find (arr, test) {
  for (var i = 0; i < arr.length; ++i) {
    if (test(arr[i], i, arr)) return arr[i]
  }

  return undefined
}

module.exports = function (tagType, items) {
  items = items.map(function (codec) { return codec })

  function _length (item, value) {
    return tagType.encodingLength(item.match) + item.type.encodingLength(value)
  }

  return {
    encode: function encode (value, buffer, offset) {
      var item = find(items, function (item) { return item.test(value) })
      if (!item) throw new TypeError('no encoding for: ' + JSON.stringify(value))
      if (!buffer) buffer = new Buffer(_length(item, value))
      if (!offset) offset = 0
      tagType.encode(item.match, buffer, offset)
      var _offset = offset + tagType.encode.bytes
      item.type.encode(value, buffer, _offset)
      encode.bytes = item.type.encode.bytes + _offset - offset
      return buffer
    },
    decode: function decode (buffer, offset) {
      if (!offset) offset = 0
      var match = tagType.decode(buffer, offset)
      var item = find(items, function (item) { return item.match === match })
      if (!item) throw new TypeError('no encoding for: ' + JSON.stringify(match))
      var _offset = offset + tagType.decode.bytes
      var value = item.type.decode(buffer, _offset)
      decode.bytes = item.type.decode.bytes + _offset - offset
      return value
    },
    encodingLength: function (value) {
      var item = find(items, function (item) { return item.test(value) })
      if (item === undefined) throw new TypeError('no encoding for: ' + JSON.stringify(value))
      return _length(item, value)
    }
  }
}

@@ -951,32 +951,37 @@
  (buffer, offset
+, end
 ) %7B%0A      if (!o
@@ -1037,32 +1037,37 @@
 e(buffer, offset
+, end
 )%0A      var item
@@ -1304,32 +1304,37 @@
 (buffer, _offset
+, end
 )%0A      decode.b

add end as 3rd argument to decode