function addToRecentProjects(config, projectName, projectDir) {
  let recent = config.get('recentProjects') || [];

  recent = recent.filter(p => p.name !== projectName || p.dir !== projectDir);

  recent.unshift({ name: projectName, dir: projectDir, timestamp: Date.now() });

  recent = recent.slice(0, 10);

  config.set('recentProjects', recent);
  return recent;
}

function toggleFavorite(config, projectName, projectDir) {
  let favorites = config.get('favoriteProjects') || [];
  const index = favorites.findIndex(p => p.name === projectName && p.dir === projectDir);

  if (index >= 0) {
    favorites.splice(index, 1);
  } else {
    favorites.push({ name: projectName, dir: projectDir });
  }

  config.set('favoriteProjects', favorites);
  return index < 0; // true if added, false if removed
}

function isFavorite(config, projectName, projectDir) {
  const favorites = config.get('favoriteProjects') || [];
  return favorites.some(p => p.name === projectName && p.dir === projectDir);
}

module.exports = { addToRecentProjects, toggleFavorite, isFavorite };
