"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _token = require("./token");
var _colmetadataTokenParser = _interopRequireDefault(require("./colmetadata-token-parser"));
var _doneTokenParser = require("./done-token-parser");
var _envChangeTokenParser = _interopRequireDefault(require("./env-change-token-parser"));
var _infoerrorTokenParser = require("./infoerror-token-parser");
var _fedauthInfoParser = _interopRequireDefault(require("./fedauth-info-parser"));
var _featureExtAckParser = _interopRequireDefault(require("./feature-ext-ack-parser"));
var _loginackTokenParser = _interopRequireDefault(require("./loginack-token-parser"));
var _orderTokenParser = _interopRequireDefault(require("./order-token-parser"));
var _returnstatusTokenParser = _interopRequireDefault(require("./returnstatus-token-parser"));
var _returnvalueTokenParser = _interopRequireDefault(require("./returnvalue-token-parser"));
var _rowTokenParser = _interopRequireDefault(require("./row-token-parser"));
var _nbcrowTokenParser = _interopRequireDefault(require("./nbcrow-token-parser"));
var _sspiTokenParser = _interopRequireDefault(require("./sspi-token-parser"));
var _helpers = require("./helpers");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
class Parser {
  debug;
  colMetadata;
  options;
  iterator;
  buffer;
  position;
  static async *parseTokens(iterable, debug, options, colMetadata = []) {
    const parser = new Parser(iterable, debug, options);
    parser.colMetadata = colMetadata;
    while (true) {
      try {
        await parser.waitForChunk();
      } catch (err) {
        if (parser.position === parser.buffer.length) {
          return;
        }
        throw err;
      }
      while (parser.buffer.length >= parser.position + 1) {
        const type = parser.buffer.readUInt8(parser.position);
        parser.position += 1;
        const token = parser.readToken(type);
        if (token !== undefined) {
          yield token;
        }
      }
    }
  }
  readToken(type) {
    switch (type) {
      case _token.TYPE.DONE:
        {
          return this.readDoneToken();
        }
      case _token.TYPE.DONEPROC:
        {
          return this.readDoneProcToken();
        }
      case _token.TYPE.DONEINPROC:
        {
          return this.readDoneInProcToken();
        }
      case _token.TYPE.ERROR:
        {
          return this.readErrorToken();
        }
      case _token.TYPE.INFO:
        {
          return this.readInfoToken();
        }
      case _token.TYPE.ENVCHANGE:
        {
          return this.readEnvChangeToken();
        }
      case _token.TYPE.LOGINACK:
        {
          return this.readLoginAckToken();
        }
      case _token.TYPE.RETURNSTATUS:
        {
          return this.readReturnStatusToken();
        }
      case _token.TYPE.ORDER:
        {
          return this.readOrderToken();
        }
      case _token.TYPE.FEDAUTHINFO:
        {
          return this.readFedAuthInfoToken();
        }
      case _token.TYPE.SSPI:
        {
          return this.readSSPIToken();
        }
      case _token.TYPE.COLMETADATA:
        {
          return this.readColMetadataToken();
        }
      case _token.TYPE.RETURNVALUE:
        {
          return this.readReturnValueToken();
        }
      case _token.TYPE.ROW:
        {
          return this.readRowToken();
        }
      case _token.TYPE.NBCROW:
        {
          return this.readNbcRowToken();
        }
      case _token.TYPE.FEATUREEXTACK:
        {
          return this.readFeatureExtAckToken();
        }
      default:
        {
          throw new Error('Unknown type: ' + type);
        }
    }
  }
  readFeatureExtAckToken() {
    let result;
    try {
      result = (0, _featureExtAckParser.default)(this.buffer, this.position, this.options);
    } catch (err) {
      if (err instanceof _helpers.NotEnoughDataError) {
        return this.waitForChunk().then(() => {
          return this.readFeatureExtAckToken();
        });
      }
      throw err;
    }
    this.position = result.offset;
    return result.value;
  }
  async readNbcRowToken() {
    return await (0, _nbcrowTokenParser.default)(this);
  }
  async readReturnValueToken() {
    return await (0, _returnvalueTokenParser.default)(this);
  }
  async readColMetadataToken() {
    const token = await (0, _colmetadataTokenParser.default)(this);
    this.colMetadata = token.columns;
    return token;
  }
  readSSPIToken() {
    let result;
    try {
      result = (0, _sspiTokenParser.default)(this.buffer, this.position, this.options);
    } catch (err) {
      if (err instanceof _helpers.NotEnoughDataError) {
        return this.waitForChunk().then(() => {
          return this.readSSPIToken();
        });
      }
      throw err;
    }
    this.position = result.offset;
    return result.value;
  }
  readFedAuthInfoToken() {
    let result;
    try {
      result = (0, _fedauthInfoParser.default)(this.buffer, this.position, this.options);
    } catch (err) {
      if (err instanceof _helpers.NotEnoughDataError) {
        return this.waitForChunk().then(() => {
          return this.readFedAuthInfoToken();
        });
      }
      throw err;
    }
    this.position = result.offset;
    return result.value;
  }
  readOrderToken() {
    let result;
    try {
      result = (0, _orderTokenParser.default)(this.buffer, this.position, this.options);
    } catch (err) {
      if (err instanceof _helpers.NotEnoughDataError) {
        return this.waitForChunk().then(() => {
          return this.readOrderToken();
        });
      }
      throw err;
    }
    this.position = result.offset;
    return result.value;
  }
  readReturnStatusToken() {
    let result;
    try {
      result = (0, _returnstatusTokenParser.default)(this.buffer, this.position, this.options);
    } catch (err) {
      if (err instanceof _helpers.NotEnoughDataError) {
        return this.waitForChunk().then(() => {
          return this.readReturnStatusToken();
        });
      }
      throw err;
    }
    this.position = result.offset;
    return result.value;
  }
  readLoginAckToken() {
    let result;
    try {
      result = (0, _loginackTokenParser.default)(this.buffer, this.position, this.options);
    } catch (err) {
      if (err instanceof _helpers.NotEnoughDataError) {
        return this.waitForChunk().then(() => {
          return this.readLoginAckToken();
        });
      }
      throw err;
    }
    this.position = result.offset;
    return result.value;
  }
  readEnvChangeToken() {
    let result;
    try {
      result = (0, _envChangeTokenParser.default)(this.buffer, this.position, this.options);
    } catch (err) {
      if (err instanceof _helpers.NotEnoughDataError) {
        return this.waitForChunk().then(() => {
          return this.readEnvChangeToken();
        });
      }
      throw err;
    }
    this.position = result.offset;
    return result.value;
  }
  readRowToken() {
    return (0, _rowTokenParser.default)(this);
  }
  readInfoToken() {
    let result;
    try {
      result = (0, _infoerrorTokenParser.infoParser)(this.buffer, this.position, this.options);
    } catch (err) {
      if (err instanceof _helpers.NotEnoughDataError) {
        return this.waitForChunk().then(() => {
          return this.readInfoToken();
        });
      }
      throw err;
    }
    this.position = result.offset;
    return result.value;
  }
  readErrorToken() {
    let result;
    try {
      result = (0, _infoerrorTokenParser.errorParser)(this.buffer, this.position, this.options);
    } catch (err) {
      if (err instanceof _helpers.NotEnoughDataError) {
        return this.waitForChunk().then(() => {
          return this.readErrorToken();
        });
      }
      throw err;
    }
    this.position = result.offset;
    return result.value;
  }
  readDoneInProcToken() {
    let result;
    try {
      result = (0, _doneTokenParser.doneInProcParser)(this.buffer, this.position, this.options);
    } catch (err) {
      if (err instanceof _helpers.NotEnoughDataError) {
        return this.waitForChunk().then(() => {
          return this.readDoneInProcToken();
        });
      }
      throw err;
    }
    this.position = result.offset;
    return result.value;
  }
  readDoneProcToken() {
    let result;
    try {
      result = (0, _doneTokenParser.doneProcParser)(this.buffer, this.position, this.options);
    } catch (err) {
      if (err instanceof _helpers.NotEnoughDataError) {
        return this.waitForChunk().then(() => {
          return this.readDoneProcToken();
        });
      }
      throw err;
    }
    this.position = result.offset;
    return result.value;
  }
  readDoneToken() {
    let result;
    try {
      result = (0, _doneTokenParser.doneParser)(this.buffer, this.position, this.options);
    } catch (err) {
      if (err instanceof _helpers.NotEnoughDataError) {
        return this.waitForChunk().then(() => {
          return this.readDoneToken();
        });
      }
      throw err;
    }
    this.position = result.offset;
    return result.value;
  }
  constructor(iterable, debug, options) {
    this.debug = debug;
    this.colMetadata = [];
    this.options = options;
    this.iterator = (iterable[Symbol.asyncIterator] || iterable[Symbol.iterator]).call(iterable);
    this.buffer = Buffer.alloc(0);
    this.position = 0;
  }
  async waitForChunk() {
    const result = await this.iterator.next();
    if (result.done) {
      throw new Error('unexpected end of data');
    }
    if (this.position === this.buffer.length) {
      this.buffer = result.value;
    } else {
      this.buffer = Buffer.concat([this.buffer.slice(this.position), result.value]);
    }
    this.position = 0;
  }
}
var _default = exports.default = Parser;
module.exports = Parser;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 28976 characters]