"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _metadataParser = require("../metadata-parser");
var _token = require("./token");
var _helpers = require("./helpers");
function readTableName(buf, offset, metadata, options) {
  if (!metadata.type.hasTableName) {
    return new _helpers.Result(undefined, offset);
  }
  if (options.tdsVersion < '7_2') {
    return (0, _helpers.readUsVarChar)(buf, offset);
  }
  let numberOfTableNameParts;
  ({
    offset,
    value: numberOfTableNameParts
  } = (0, _helpers.readUInt8)(buf, offset));
  const tableName = [];
  for (let i = 0; i < numberOfTableNameParts; i++) {
    let tableNamePart;
    ({
      offset,
      value: tableNamePart
    } = (0, _helpers.readUsVarChar)(buf, offset));
    tableName.push(tableNamePart);
  }
  return new _helpers.Result(tableName, offset);
}
function readColumnName(buf, offset, index, metadata, options) {
  let colName;
  ({
    offset,
    value: colName
  } = (0, _helpers.readBVarChar)(buf, offset));
  if (options.columnNameReplacer) {
    return new _helpers.Result(options.columnNameReplacer(colName, index, metadata), offset);
  } else if (options.camelCaseColumns) {
    return new _helpers.Result(colName.replace(/^[A-Z]/, function (s) {
      return s.toLowerCase();
    }), offset);
  } else {
    return new _helpers.Result(colName, offset);
  }
}
function readColumn(buf, offset, options, index) {
  let metadata;
  ({
    offset,
    value: metadata
  } = (0, _metadataParser.readMetadata)(buf, offset, options));
  let tableName;
  ({
    offset,
    value: tableName
  } = readTableName(buf, offset, metadata, options));
  let colName;
  ({
    offset,
    value: colName
  } = readColumnName(buf, offset, index, metadata, options));
  return new _helpers.Result({
    userType: metadata.userType,
    flags: metadata.flags,
    type: metadata.type,
    collation: metadata.collation,
    precision: metadata.precision,
    scale: metadata.scale,
    udtInfo: metadata.udtInfo,
    dataLength: metadata.dataLength,
    schema: metadata.schema,
    colName: colName,
    tableName: tableName
  }, offset);
}
async function colMetadataParser(parser) {
  let columnCount;
  while (true) {
    let offset;
    try {
      ({
        offset,
        value: columnCount
      } = (0, _helpers.readUInt16LE)(parser.buffer, parser.position));
    } catch (err) {
      if (err instanceof _helpers.NotEnoughDataError) {
        await parser.waitForChunk();
        continue;
      }
      throw err;
    }
    parser.position = offset;
    break;
  }
  const columns = [];
  for (let i = 0; i < columnCount; i++) {
    while (true) {
      let column;
      let offset;
      try {
        ({
          offset,
          value: column
        } = readColumn(parser.buffer, parser.position, parser.options, i));
      } catch (err) {
        if (err instanceof _helpers.NotEnoughDataError) {
          await parser.waitForChunk();
          continue;
        }
        throw err;
      }
      parser.position = offset;
      columns.push(column);
      break;
    }
  }
  return new _token.ColMetadataToken(columns);
}
var _default = exports.default = colMetadataParser;
module.exports = colMetadataParser;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 10328 characters]