export const DESKTOP_APP_DISPLAY_NAME = 'Toju';
export const DESKTOP_EXECUTABLE_NAME = 'toju';

export const LEGACY_APP_REGISTRY_NAMES = [
  'MetoYou',
  'MeToYou',
  'metoyou'
] as const;

function normalizeAutostartBaseName(fileName: string): string {
  return fileName.replace(/\.desktop$/iu, '').replace(/\.exe$/iu, '');
}

export function isLegacyLinuxAutostartEntry(
  fileName: string,
  currentLaunchBaseName: string
): boolean {
  const entryBaseName = normalizeAutostartBaseName(fileName);
  const currentBaseName = normalizeAutostartBaseName(currentLaunchBaseName);

  if (entryBaseName === currentBaseName) {
    return false;
  }

  const normalizedEntry = entryBaseName.toLowerCase();

  if (LEGACY_APP_REGISTRY_NAMES.some((legacyName) => normalizedEntry === legacyName.toLowerCase())) {
    return true;
  }

  return /^metoyou[-.]/iu.test(entryBaseName);
}

export function patchLinuxAutostartDesktopEntryNameField(
  desktopEntry: string,
  displayName: string = DESKTOP_APP_DISPLAY_NAME
): string {
  if (/^Name=.*$/m.test(desktopEntry)) {
    return desktopEntry.replace(/^Name=.*$/m, `Name=${displayName}`);
  }

  return desktopEntry;
}
