"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.createValidationMemo = void 0;
var _stuff = require("./stuff");
var _core = require("@react-awesome-query-builder/core");
var validateAndFixTree = _core.Utils.Validation.validateAndFixTree;
var createValidationMemo = exports.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 && ((0, _stuff.immutableEqual)(tree, originalTree) || (0, _stuff.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;
    }
  };
};