"""
Organisation Number Normalizer

Normalizes Swedish organisation numbers and VAT numbers.
"""

from .base import BaseNormalizer
from shared.utils.format_variants import FormatVariants
from shared.utils.text_cleaner import TextCleaner


class OrganisationNumberNormalizer(BaseNormalizer):
    """
    Normalizes Swedish organisation numbers and VAT numbers.

    Organisation number format: NNNNNN-NNNN (6 digits + hyphen + 4 digits)
    Swedish VAT format: SE + org_number (10 digits) + 01

    Examples:
        '556123-4567' -> ['556123-4567', '5561234567', 'SE556123456701', ...]
        '5561234567' -> ['5561234567', '556123-4567', 'SE556123456701', ...]
        'SE556123456701' -> ['SE556123456701', '5561234567', '556123-4567', ...]
    """

    def normalize(self, value: str) -> list[str]:
        """Generate variants of organisation number."""
        # Use shared module for base variants
        variants = set(FormatVariants.organisation_number_variants(value))

        # Add OCR error variants for digit sequences
        digits = TextCleaner.extract_digits(value, apply_ocr_correction=False)
        if digits and len(digits) >= 10:
            # Generate variants where OCR might have misread characters
            for ocr_var in TextCleaner.generate_ocr_variants(digits[:10]):
                variants.add(ocr_var)
                if len(ocr_var) == 10:
                    variants.add(f"{ocr_var[:6]}-{ocr_var[6:]}")

        return list(v for v in variants if v)
