function formatUser(user) {
  const u = user || {};
  const username = u.username || (u.profile && u.profile.username_telegram) || 'no_username';
  return `${u.name || ''} ${u.surname || ''} (@${username})`.trim();
}

function safeExec(fn) {
  return async (...args) => {
    try {
      return await fn(...args);
    } catch (e) {
      console.error('Telegram helper error:', e);
      return null;
    }
  };
}

function ensureArray(x) {
  if (!x) return [];
  return Array.isArray(x) ? x : [x];
}

// utility semplice per evitare flood (se ti serve rate-limit: usa bottleneck)
async function eachSeries(arr, fn) {
  for (const item of arr) {
    // eslint-disable-next-line no-await-in-loop
    await fn(item);
  }
}

module.exports = { formatUser, safeExec, ensureArray, eachSeries };
