// Generated by CoffeeScript 1.6.3
(function() {
  var EPOCH_DATE, MAX, NULL, ex, tds;

  tds = require('tedious');

  EPOCH_DATE = new Date(1900, 0, 1);

  NULL = (1 << 16) - 1;

  MAX = (1 << 16) - 1;

  try {
    if (require('tedious/package.json').version !== '0.1.5') {
      return;
    }
    /*
    	Add support for Binary
    */

    tds.TYPES.Binary.maximumLength = 8000;
    tds.TYPES.Binary.writeParameterData = function(buffer, parameter) {
      var length;
      if (parameter.length) {
        length = parameter.length;
      } else if (parameter.value != null) {
        length = parameter.value.length;
      } else {
        length = this.maximumLength;
      }
      buffer.writeUInt8(this.id);
      buffer.writeUInt16LE(length);
      if (parameter.value != null) {
        buffer.writeUInt16LE(length);
        return buffer.writeBuffer(parameter.value.slice(0, Math.min(length, this.maximumLength)));
      } else {
        return buffer.writeUInt16LE(NULL);
      }
    };
    /*
    	Add support for VarBinary
    */

    tds.TYPES.VarBinary.maximumLength = 8000;
    tds.TYPES.VarBinary.writeParameterData = function(buffer, parameter) {
      var length;
      if (parameter.length) {
        length = parameter.length;
      } else if (parameter.value != null) {
        length = parameter.value.length;
      } else {
        length = this.maximumLength;
      }
      buffer.writeUInt8(this.id);
      if (length <= this.maximumLength) {
        buffer.writeUInt16LE(this.maximumLength);
      } else {
        buffer.writeUInt16LE(MAX);
      }
      if (parameter.value != null) {
        if (length <= this.maximumLength) {
          buffer.writeUInt16LE(length);
          return buffer.writeBuffer(parameter.value);
        } else {
          buffer.writeUInt64LE(length);
          buffer.writeUInt32LE(length);
          buffer.writeBuffer(parameter.value);
          return buffer.writeUInt32LE(0);
        }
      } else {
        return buffer.writeUInt16LE(NULL);
      }
    };
    /*
    	Add support for Image
    */

    tds.TYPES.Image.writeParameterData = function(buffer, parameter) {
      var length;
      if (parameter.length) {
        length = parameter.length;
      } else if (parameter.value != null) {
        length = parameter.value.length;
      } else {
        length = -1;
      }
      buffer.writeUInt8(this.id);
      buffer.writeInt32LE(length);
      if (parameter.value != null) {
        buffer.writeInt32LE(length);
        return buffer.writeBuffer(parameter.value);
      } else {
        return buffer.writeInt32LE(length);
      }
    };
    /*
    	Fixes issue when bigint null value is converted to 0 value
    */

    tds.TYPES.BigInt.writeParameterData = function(buffer, parameter) {
      buffer.writeUInt8(tds.TYPES.IntN.id);
      buffer.writeUInt8(8);
      if (parameter.value != null) {
        buffer.writeUInt8(8);
        if (parseInt(parameter.value) > 0x100000000) {
          buffer.writeUInt32LE(parseInt(parameter.value) % 0x100000000);
        } else {
          buffer.writeInt32LE(parseInt(parameter.value) % 0x100000000);
        }
        return buffer.writeInt32LE(Math.floor(parseInt(parameter.value) / 0x100000000));
      } else {
        return buffer.writeUInt8(0);
      }
    };
    /*
    	Fixes dates before 1.1.1900
    */

    tds.TYPES.DateTime.writeParameterData = function(buffer, parameter) {
      var days, milliseconds, seconds, threeHundredthsOfSecond;
      buffer.writeUInt8(tds.TYPES.DateTimeN.id);
      buffer.writeUInt8(8);
      if (parameter.value != null) {
        days = (parameter.value.getTime() - EPOCH_DATE.getTime()) / (1000 * 60 * 60 * 24);
        days = Math.floor(days);
        seconds = parameter.value.getHours() * 60 * 60;
        seconds += parameter.value.getMinutes() * 60;
        seconds += parameter.value.getSeconds();
        milliseconds = (seconds * 1000) + parameter.value.getMilliseconds();
        threeHundredthsOfSecond = milliseconds / (3 + (1 / 3));
        threeHundredthsOfSecond = Math.floor(threeHundredthsOfSecond);
        buffer.writeUInt8(8);
        buffer.writeInt32LE(days);
        return buffer.writeUInt32LE(threeHundredthsOfSecond);
      } else {
        return buffer.writeUInt8(0);
      }
    };
  } catch (_error) {
    ex = _error;
  }

}).call(this);
