from typing import Optional


def desensitize_value(value: str, field_type: str) -> str:
    if not value or not isinstance(value, str):
        return value or ""
    if field_type == "phone":
        if len(value) < 7:
            return value
        return value[:3] + "****" + value[-4:]
    if field_type == "id_card":
        if len(value) < 7:
            return value
        return value[:3] + "*" * (len(value) - 7) + value[-4:]
    if field_type == "credit_code":
        if len(value) < 6:
            return value
        return value[:2] + "*" * (len(value) - 6) + value[-4:]
    return value


def desensitize_case(case_dict: dict) -> dict:
    field_mapping: dict[str, str] = {
        "party_id_number": "id_card",
        "phone": "phone",
        "credit_code": "credit_code",
    }
    for field, field_type in field_mapping.items():
        if field in case_dict and case_dict[field]:
            case_dict[field] = desensitize_value(
                str(case_dict[field]), field_type
            )
    return case_dict
