"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = parseJSON;
exports.reviver = reviver;
var _jsan = _interopRequireDefault(require("jsan"));
var _dataTypes = require("../constants/dataTypes");
function reviver(key, value) {
  if (typeof value === 'object' && value !== null && '__serializedType__' in value && typeof value.data === 'object') {
    const data = value.data;
    data[_dataTypes.DATA_TYPE_KEY] = value.__serializedType__;
    if ('__serializedRef__' in value) data[_dataTypes.DATA_REF_KEY] = value.__serializedRef__;
    /*
    if (Array.isArray(data)) {
      data.__serializedType__ = value.__serializedType__;
    } else {
      Object.defineProperty(data, '__serializedType__', {
        value: value.__serializedType__
      });
    }
    */
    return data;
  }
  return value;
}
function parseJSON(data, serialize) {
  if (typeof data !== 'string') return data;
  try {
    return serialize ? _jsan.default.parse(data, reviver) : _jsan.default.parse(data);
  } catch (e) {
    if (process.env.NODE_ENV !== 'production') /* eslint-disable-next-line no-console */
      console.error(data + 'is not a valid JSON', e);
    return undefined;
  }
}