import { ComponentType, useEffect } from 'react';

const warn = (name: string, alternative?: string) => {
  if (process.env.NODE_ENV === 'production') {
    return;
  }
  if (typeof console?.warn === 'function') {
    if (alternative) {
      console.warn(`DEPRECATED: ${name} is deprecated, use ${alternative} instead.`);
    } else {
      console.warn(`DEPRECATED: ${name} is deprecated.`);
    }
  }
};

const withDeprecated = <P extends object>(
  Component: ComponentType<P>,
  { name, alternative }: { name: string; alternative?: string }
) => {
  return function Deprecated(props: P) {
    useEffect(() => {
      warn(name, alternative);
    }, []);

    return <Component {...props} />;
  };
};

export { warn, withDeprecated };
