'use strict';

var warning = require('../internal/warning.js');

/**
 * Copyright IBM Corp. 2016, 2023
 *
 * This source code is licensed under the Apache-2.0 license found in the
 * LICENSE file in the root directory of this source tree.
 */


const warningCache = new Map();

/**
 * Wraps a prop-type validator with a deprecation warning.
 *
 * @param propType - The original prop validator function.
 * @param message - Deprecation message.
 * @returns A new validator function that emits a warning the first time a
 *          deprecated prop is used.
 */
const deprecate = (propType, message) => {
  const checker = (props, propName, componentName, ...rest) => {
    if (typeof props[propName] === 'undefined') {
      return;
    }
    if (!warningCache.has(componentName)) {
      warningCache.set(componentName, new Set());
    }
    const warnedProps = warningCache.get(componentName);
    if (warnedProps && !warnedProps.has(propName)) {
      warnedProps.add(propName);
      warning.warning(false, message || `The prop \`${propName}\` has been deprecated for the ` + `${componentName} component. It will be removed in the next major ` + `release`) ;
    }
    return propType(props, propName, componentName, ...rest);
  };
  return checker;
};

exports.deprecate = deprecate;
