import { immutableEqual } from "./stuff";
import { Utils } from "@react-awesome-query-builder/core";
var validateAndFixTree = Utils.Validation.validateAndFixTree;
export var createValidationMemo = function createValidationMemo() {
  var originalTree;
  var validatedTree;
  var configId;
  return function (config, tree) {
    var oldConfig = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : undefined;
    var sanitizeTree = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true;
    if (!tree) {
      return null;
    }
    if (config.__configId === configId && (immutableEqual(tree, originalTree) || immutableEqual(tree, validatedTree))) {
      return validatedTree;
    } else {
      configId = config.__configId;
      originalTree = tree;
      if (sanitizeTree === false) {
        validatedTree = validateAndFixTree(tree, null, config, oldConfig || config, false, false, false);
      } else {
        validatedTree = validateAndFixTree(tree, null, config, oldConfig || config);
      }
      return validatedTree;
    }
  };
};