/**
 * Slug helpers for storefront URLs.
 * Mirrors the SQL function `_storefront_slugify` in init.ts.
 */
export function slugify(input: string): string {
  const cleaned = (input || '')
    .toLowerCase()
    .normalize('NFKD')
    .replace(/[\u0300-\u036f]/g, '')
    .replace(/[^a-z0-9]+/g, '-')
    .replace(/^-+|-+$/g, '');
  return cleaned || 'r';
}

export function validateSlug(slug: string): boolean {
  if (!slug || slug.length > 64) return false;
  // Linear character check — no nested quantifiers, no ReDoS risk.
  // Equivalent to /^[a-z0-9]+(?:-[a-z0-9]+)*$/ but without backtracking ambiguity.
  // Rules: only lowercase alphanumeric and hyphens; no leading, trailing,
  // or consecutive hyphens.
  if (!/^[a-z0-9-]+$/.test(slug)) return false;
  return !slug.startsWith('-') && !slug.endsWith('-') && !slug.includes('--');
}
