'use strict';

const zlib = require('zlib');

class MCStream {
  constructor() {
    this.threshold = -1;
  }

  setThreshold(t) {
    this.threshold = t | 0;
  }

  inflate(data) {
    if (this.threshold < 0) return data;
    if (!data) return data;
    try {
      return zlib.inflateRawSync(data);
    } catch (_) {
      return data;
    }
  }

  deflate(data) {
    if (this.threshold < 0) return data;
    if (!data || data.length < this.threshold) return data;
    try {
      return zlib.deflateRawSync(data);
    } catch (_) {
      return data;
    }
  }

  isEnabled() {
    return this.threshold >= 0;
  }
}

module.exports = { MCStream };
