"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = useDeepJSONEffect;
exports.stringify = void 0;
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
var _react = require("react");
const getCircularReplacer = () => {
  const seen = new WeakSet();
  return (_, value) => {
    if (typeof value === 'object' && value !== null) {
      if (seen.has(value)) {
        return;
      }
      seen.add(value);
    }
    return value;
  };
};

/**
 * 一个更加安全的 stringify，可以解决循环依赖的问题
 * @param value
 */
const stringify = value => JSON.stringify(value, getCircularReplacer());
exports.stringify = stringify;
const jsonCompareEquals = (value, nextValue) => {
  try {
    return stringify(value) === stringify(nextValue);
  } catch (error) {
    // do something
  }
  return false;
};
function useJsonCompareMemoize(value) {
  const ref = (0, _react.useRef)('');
  // it can be done by using useMemo as well
  // but useRef is rather cleaner and easier
  if (!jsonCompareEquals(value, ref.current)) {
    ref.current = JSON.stringify(value, getCircularReplacer());
  }
  return ref.current;
}
function useDeepJSONEffect(effect, dependencies) {
  (0, _react.useEffect)(effect, [useJsonCompareMemoize(dependencies)]);
}