from typing import Iterable


def structured_location_parts(*, county="", sub_county="", division="", location="", sub_location="", village=""):
    return {
        "county": str(county or "").strip(),
        "sub_county": str(sub_county or "").strip(),
        "division": str(division or "").strip(),
        "location": str(location or "").strip(),
        "sub_location": str(sub_location or "").strip(),
        "village": str(village or "").strip(),
    }


def structured_location_label(*, county="", sub_county="", division="", location="", sub_location="", village="", fallback=""):
    parts = structured_location_parts(
        county=county,
        sub_county=sub_county,
        division=division,
        location=location,
        sub_location=sub_location,
        village=village,
    )
    ordered_values = [
        parts["village"],
        parts["sub_location"],
        parts["location"],
        parts["division"],
        parts["sub_county"],
        parts["county"],
    ]
    cleaned = [value for value in ordered_values if value]
    if cleaned:
        return ", ".join(cleaned)
    legacy_values = [value for value in parts.values() if value]
    if legacy_values:
        return ", ".join(legacy_values)
    return str(fallback or "").strip()


def compact_location_lines(parts: Iterable[str]):
    cleaned = [str(part or "").strip() for part in parts if str(part or "").strip()]
    return cleaned
