"""Canonical Magento category path naming from master/PLP slugs."""

from __future__ import annotations

from typing import Dict

from channel.url_canonical import normalize_plp_path

# PLP slug segment → Magento category path segment (before Title Case).
# Magento uses "Tall Cabinets"; some master/PLP paths still use short "tall".
MAGENTO_PATH_SEGMENT_ALIASES: Dict[str, str] = {
    "tall": "tall-cabinets",
    "tall-cabinets": "tall-cabinets",
}


def canonical_magento_path_segment(segment: str) -> str:
    normalized = normalize_plp_path(segment)
    return MAGENTO_PATH_SEGMENT_ALIASES.get(normalized, normalized)


def plp_slug_to_magento_path_names(path_slug: str) -> str:
    """kitchen-cabinets/tall/pantry-cabinet → Kitchen Cabinets/Tall Cabinets/Pantry Cabinet."""
    parts = [
        canonical_magento_path_segment(part)
        for part in normalize_plp_path(path_slug).split("/")
        if part
    ]
    return "/".join(part.replace("-", " ").title() for part in parts)


def title_path_to_magento_path_names(title: str) -> str:
    """Kitchen Cabinets / Tall / Pantry Cabinet → Kitchen Cabinets/Tall Cabinets/Pantry Cabinet."""
    slug = normalize_plp_path(str(title or "").replace(" / ", "/"))
    return plp_slug_to_magento_path_names(slug)
