"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

function _toBase() {
  const data = _interopRequireDefault(require("./string/to-base64"));

  _toBase = function () {
    return data;
  };

  return data;
}

function _zlib() {
  const data = _interopRequireDefault(require("zlib"));

  _zlib = function () {
    return data;
  };

  return data;
}

module.exports = function packCmd(obj, base64 = true, compress = true) {
  if (compress) {
    if (obj.payload) {
      obj.payload = _zlib().default.deflateSync(JSON.stringify(obj.payload));
    }

    if (obj.headers && obj.headers.context) {
      obj.headers.context = _zlib().default.deflateSync(JSON.stringify(obj.headers.context));
    }
  }

  const compressed = JSON.stringify(obj);

  if (base64) {
    const res = (0, _toBase().default)(JSON.stringify(obj));
    return res;
  }

  return JSON.stringify(obj);
};