import { useFormikContext } from 'formik';
import { Data } from '../../../types';

export const useTitle = <TValues extends Data>(
  titleProperty?: string,
  defaultTitle?: string,
): string | undefined => {
  const { values, dirty } = useFormikContext<TValues>();

  const title: string | undefined =
    titleProperty && values[titleProperty] !== '' && values[titleProperty]
      ? values[titleProperty]
      : defaultTitle ?? '';

  if (dirty) {
    return `*${title}`;
  }

  return title;
};
