// @flow
import { useEffect } from 'react';

const formatDeprecationWarning = (
  componentName: string,
  removalAtVersion?: string,
  replacementComponent?: string,
): string =>
  `
DEPRECATION WARNING:
================================

The ${componentName} component is deprecated and will be removed from Patchwork in ${
    removalAtVersion ?? 'a future release'
  }.

${
  replacementComponent?.length
    ? `Please use ${replacementComponent} instead.`
    : 'Please visit the Patchwork storybook for advice on alternatives.'
}
`;

export const useDeprecationWarning = (
  componentName: string,
  removalAtVersion?: string,
  replacementComponent?: string,
): void => {
  useEffect(() => {
    if (process.env.NODE_ENV !== 'production') {
      // eslint-disable-next-line no-console
      console.warn(
        formatDeprecationWarning(
          componentName,
          removalAtVersion,
          replacementComponent,
        ),
      );
    }
  }, [componentName, removalAtVersion, replacementComponent]);
};
