"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.stringifyJSON = stringifyJSON;
var _jsan = _interopRequireDefault(require("jsan"));
var _dataTypes = require("../constants/dataTypes");
function replacer(key, value) {
  if (typeof value === 'object' && value !== null && _dataTypes.DATA_TYPE_KEY in value) {
    const __serializedType__ = value[_dataTypes.DATA_TYPE_KEY];
    const clone = {
      ...value
    };
    delete clone[_dataTypes.DATA_TYPE_KEY]; // eslint-disable-line no-param-reassign
    const r = {
      data: clone,
      __serializedType__
    };
    if (_dataTypes.DATA_REF_KEY in value) r.__serializedRef__ = clone[_dataTypes.DATA_REF_KEY];
    return r;
  }
  return value;
}
function stringifyJSON(data, serialize) {
  return serialize ? _jsan.default.stringify(data, replacer, null, true) : _jsan.default.stringify(data);
}