"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _token = require("./token");
var _metadataParser = require("../metadata-parser");
var _valueParser = require("../value-parser");
var _helpers = require("./helpers");
var iconv = _interopRequireWildcard(require("iconv-lite"));
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
// s2.2.7.16

async function returnParser(parser) {
  let paramName;
  let paramOrdinal;
  let metadata;
  while (true) {
    const buf = parser.buffer;
    let offset = parser.position;
    try {
      ({
        offset,
        value: paramOrdinal
      } = (0, _helpers.readUInt16LE)(buf, offset));
      ({
        offset,
        value: paramName
      } = (0, _helpers.readBVarChar)(buf, offset));
      // status
      ({
        offset
      } = (0, _helpers.readUInt8)(buf, offset));
      ({
        offset,
        value: metadata
      } = (0, _metadataParser.readMetadata)(buf, offset, parser.options));
      if (paramName.charAt(0) === '@') {
        paramName = paramName.slice(1);
      }
    } catch (err) {
      if (err instanceof _helpers.NotEnoughDataError) {
        await parser.waitForChunk();
        continue;
      }
      throw err;
    }
    parser.position = offset;
    break;
  }
  let value;
  while (true) {
    const buf = parser.buffer;
    let offset = parser.position;
    if ((0, _valueParser.isPLPStream)(metadata)) {
      const chunks = await (0, _valueParser.readPLPStream)(parser);
      if (chunks === null) {
        value = chunks;
      } else if (metadata.type.name === 'NVarChar' || metadata.type.name === 'Xml') {
        value = Buffer.concat(chunks).toString('ucs2');
      } else if (metadata.type.name === 'VarChar') {
        value = iconv.decode(Buffer.concat(chunks), metadata.collation?.codepage ?? 'utf8');
      } else if (metadata.type.name === 'VarBinary' || metadata.type.name === 'UDT') {
        value = Buffer.concat(chunks);
      }
    } else {
      try {
        ({
          value,
          offset
        } = (0, _valueParser.readValue)(buf, offset, metadata, parser.options));
      } catch (err) {
        if (err instanceof _helpers.NotEnoughDataError) {
          await parser.waitForChunk();
          continue;
        }
        throw err;
      }
      parser.position = offset;
    }
    break;
  }
  return new _token.ReturnValueToken({
    paramOrdinal: paramOrdinal,
    paramName: paramName,
    metadata: metadata,
    value: value
  });
}
var _default = exports.default = returnParser;
module.exports = returnParser;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 8524 characters]