"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.onlyUpdateForProps = exports.compareChildren = void 0;
var _equals = _interopRequireDefault(require("ramda/src/equals"));
var _react = require("react");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
/**
 * Compares two children and returns true if they are equivalent, false otherwise.
 *
 * @function
 * @param   {children}    a    children props
*  @param   {children}    b    children props
 *
 * @returns {Boolean}          `true` if same
 * @memberof agate/internal/util
 * @private
 */
var compareChildren = exports.compareChildren = function compareChildren(a, b) {
  if (!a || !b || a.length !== b.length) return false;
  var type = null;
  for (var i = 0; i < a.length; i++) {
    type = type || typeof a[i];
    if (type === 'string') {
      if (a[i] !== b[i]) {
        return false;
      }
    } else if (!(0, _equals["default"])(a[i], b[i])) {
      return false;
    }
  }
  return true;
};

/**
 * Updates component only when given props are not shallowly equivalent, not updating otherwise.
 *
 * @function
 * @param   {any}    wrapped    A component
*  @param   {Array}  propKeys   Prop keys to compare
 *
 * @returns {any}               Conditionally memoized component
 * @memberof agate/internal/util
 * @private
 */
var onlyUpdateForProps = exports.onlyUpdateForProps = function onlyUpdateForProps(wrapped, propKeys) {
  return /*#__PURE__*/(0, _react.memo)(wrapped, function (prevProps, nextProps) {
    var hasOwn = Object.prototype.hasOwnProperty;
    if (Array.isArray(propKeys)) {
      return propKeys.every(function (key) {
        return hasOwn.call(prevProps, key) && hasOwn.call(nextProps, key) && Object.is(prevProps[key], nextProps[key]);
      });
    }
    return false;
  });
};