text: "use strict";
  exports.__esModule = true;
  var errors_1 = require("@ledgerhq/errors");
  var Tag = 0x05;
  function asUInt16BE(value) {
      var b = Buffer.alloc(2);
      b.writeUInt16BE(value, 0);
      return b;
  }
  var initialAcc = {
      data: Buffer.alloc(0),
      dataLength: 0,
      sequence: 0
  };
  /**
   *
   */
  var createHIDframing = function (channel, packetSize) {
      return {
          makeBlocks: function (apdu) {
              var data = Buffer.concat([asUInt16BE(apdu.length), apdu]);
              var blockSize = packetSize - 5;
              var nbBlocks = Math.ceil(data.length / blockSize);
              data = Buffer.concat([
                  data,
                  Buffer.alloc(nbBlocks * blockSize - data.length + 1).fill(0),
              ]);
              var blocks = [];
              for (var i = 0; i < nbBlocks; i++) {
                  var head = Buffer.alloc(5);
                  head.writeUInt16BE(channel, 0);
                  head.writeUInt8(Tag, 2);
                  head.writeUInt16BE(i, 3);
                  var chunk = data.slice(i * blockSize, (i + 1) * blockSize);
                  blocks.push(Buffer.concat([head, chunk]));
              }
              return blocks;
          },
          reduceResponse: function (acc, chunk) {
              var _a = acc || initialAcc, data = _a.data, dataLength = _a.dataLength, sequence = _a.sequence;
              if (chunk.readUInt16BE(0) !== channel) {
                  throw new errors_1.TransportError("Invalid channel", "InvalidChannel");
              }
              if (chunk.readUInt8(2) !== Tag) {
                  throw new errors_1.TransportError("Invalid tag", "InvalidTag");
              }
              if (chunk.readUInt16BE(3) !== sequence) {
                  throw new errors_1.TransportError("Invalid sequence", "InvalidSequence");
              }
              if (!acc) {
                  dataLength = chunk.readUInt16BE(5);
              }
              sequence++;
              var chunkData = chunk.slice(acc ? 5 : 7);
              data = Buffer.concat([data, chunkData]);
              if (data.length > dataLength) {
                  data = data.slice(0, dataLength);
              }
              return {
                  data: data,
                  dataLength: dataLength,
                  sequence: sequence
              };
          },
          getReducedResult: function (acc) {
              if (acc && acc.dataLength === acc.data.length) {
                  return acc.data;
              }
          }
      };
  };
  exports["default"] = createHIDframing;
  //# sourceMappingURL=hid-framing.js.map