// slugify.ts
export function slugify(input: string, maxLen = 60) {
  return input
    .normalize('NFD')
    .replace(/[\u0300-\u036f]/g, '')
    .toLowerCase()
    .replace(/[^a-z0-9]+/g, '-')
    .replace(/^-+|-+$/g, '')
    .slice(0, maxLen);
}

// Combina slug + id (id al final facilita parsear con "último -")
export function toParam(name: string, id: string | number) {
  return `${slugify(name)}-${id}`;
}

// Extrae { id, slug } de un param con formato "slug-123"
export function fromParam(param: string) {
  const lastDash = param.lastIndexOf('-');
  if (lastDash === -1) return { slug: param, id: null };
  const slug = param.slice(0, lastDash);
  const idStr = param.slice(lastDash + 1);
  const id = /^\d+$/.test(idStr) ? Number(idStr) : null;
  return { slug, id };
}
