/**
 * Buat slug aman dari nama. Hapus diakritik, karakter spesial, lowercase.
 */
export function makeSlug(input) {
  return String(input || '')
    .normalize('NFD')
    .replace(/[\u0300-\u036f]/g, '') // strip diakritik
    .toLowerCase()
    .replace(/['"`’]/g, '')
    .replace(/[^a-z0-9]+/g, '-')
    .replace(/^-+|-+$/g, '')
    .slice(0, 200);
}

/**
 * Pastikan slug unik dalam scope angkatan. Tambah suffix -2, -3 jika tabrakan.
 */
export async function uniqueSlug(base, isTaken) {
  let candidate = base || 'santri';
  let n = 2;
  while (await isTaken(candidate)) {
    candidate = `${base}-${n}`;
    n++;
    if (n > 999) throw new Error('Tidak bisa generate slug unik.');
  }
  return candidate;
}
