"""Canonical slug for a product detail page (/p/<id>/<slug>).

The slug is cosmetic + SEO only — the route looks the product up by the
numeric `parts.id` (stable, the same id our /jump links use), and any
non-canonical or missing slug 301-redirects to the canonical one. So the
id is the source of truth and the slug just makes the URL readable and
keyword-rich. Mirrors the /b/<code>/<slug> build-share precedent
(ngsite/build_url.py), reusing its slugifier.
"""

from .build_url import _slugify

# Keep URLs sane: a product title can be long, so cap the slug to a handful
# of words. The id carries identity, so truncation never breaks the lookup.
_MAX_WORDS = 8


def product_slug(description) -> str:
    s = _slugify(description or "")
    if not s:
        return "part"
    return "-".join(s.split("-")[:_MAX_WORDS])
