"""
GLAMWalk -- Volume 3: Data at Scale
05_cooper_hewitt_qualify_and_safe_str.py

A deliberately conservative public-domain proxy filter for a source with no rights field at all, plus a defensive helper for JSON fields that are sometimes a string and sometimes a nested dict.

Source: Volume Three, Sections 3.1-3.2
Targets Python 3.11.4 -- see Volume One, Chapter 1.5/1.6.
"""

def cooper_hewitt_qualifies(record):
    """
    No explicit rights field exists for this institution, so this
    is a deliberately conservative proxy: pre-1930 date AND
    institution-owned (never a loan), even though this certainly
    excludes some objects that might, on individual research, turn
    out to be fine to use. Conservative-and-correct beats
    permissive-and-uncertain here.
    """
    date_field = record.get("date_start")
    if not date_field or int(date_field) >= 1930:
        return False

    if record.get("is_loan"):
        return False

    return True


def safe_str(value):
    """
    Some fields in real-world JSON records are sometimes a plain
    string and sometimes a nested dict (e.g. {"value": "...", "lang": "en"}).
    Rather than crashing or writing separate handling for every field
    that might do this, normalize defensively, once, in one place.
    """
    if isinstance(value, dict):
        return value.get("value", "")
    if value is None:
        return ""
    return str(value)
