import { type UserTask } from 'src/pages/dashboard/serviceManagement/types';
import { type MeUser } from 'src/types/user';
import { type LokaliseTFunction } from 'src/utils/i18n';
import { getFullName } from 'src/utils/userUtils';
import { validateEmail } from 'src/utils/validation';

export const getUserDisplayName = (
  user: UserTask | null | undefined,
  loggedUser: MeUser | null | undefined,
  t: LokaliseTFunction,
) => {
  // Deleted users can be null or undefined, so we need to check for that
  if (!user) return '';
  if (user?.id === loggedUser?.id) return `${getFullName(user)} ${t('you')}`;
  return getFullName(user);
};

export const getDefaultUserEmail = (user: MeUser | null) => {
  if (!user) {
    return '';
  }

  if (user.email) {
    return user.email;
  } else if (validateEmail().pattern.value.test(user?.employeeInternalId)) {
    return user.employeeInternalId;
  }
  return '';
};
