"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.doneInProcParser = doneInProcParser;
exports.doneParser = doneParser;
exports.doneProcParser = doneProcParser;
var _token = require("./token");
var _helpers = require("./helpers");
// s2.2.7.5/6/7

const STATUS = {
  MORE: 0x0001,
  ERROR: 0x0002,
  // This bit is not yet in use by SQL Server, so is not exposed in the returned token
  INXACT: 0x0004,
  COUNT: 0x0010,
  ATTN: 0x0020,
  SRVERROR: 0x0100
};
function readToken(buf, offset, options) {
  let status;
  ({
    offset,
    value: status
  } = (0, _helpers.readUInt16LE)(buf, offset));
  const more = !!(status & STATUS.MORE);
  const sqlError = !!(status & STATUS.ERROR);
  const rowCountValid = !!(status & STATUS.COUNT);
  const attention = !!(status & STATUS.ATTN);
  const serverError = !!(status & STATUS.SRVERROR);
  let curCmd;
  ({
    offset,
    value: curCmd
  } = (0, _helpers.readUInt16LE)(buf, offset));
  let rowCount;
  ({
    offset,
    value: rowCount
  } = (options.tdsVersion < '7_2' ? _helpers.readUInt32LE : _helpers.readBigUInt64LE)(buf, offset));
  return new _helpers.Result({
    more: more,
    sqlError: sqlError,
    attention: attention,
    serverError: serverError,
    rowCount: rowCountValid ? Number(rowCount) : undefined,
    curCmd: curCmd
  }, offset);
}
function doneParser(buf, offset, options) {
  let value;
  ({
    offset,
    value
  } = readToken(buf, offset, options));
  return new _helpers.Result(new _token.DoneToken(value), offset);
}
function doneInProcParser(buf, offset, options) {
  let value;
  ({
    offset,
    value
  } = readToken(buf, offset, options));
  return new _helpers.Result(new _token.DoneInProcToken(value), offset);
}
function doneProcParser(buf, offset, options) {
  let value;
  ({
    offset,
    value
  } = readToken(buf, offset, options));
  return new _helpers.Result(new _token.DoneProcToken(value), offset);
}
//# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 6196 characters]