"""Bind an actual mini selection to a fresh native observation before any input."""


def selection_error(selection, snapshot):
    if str(snapshot.get('character_id')) != str(selection.get('character_id')):
        return 'selected_character_changed'
    read = snapshot.get('quest_state_read') or {}
    if not read.get('ok'):
        return 'selected_quest_current_read_unavailable'
    qid = int(selection['quest_id'])
    rows = [r for name in ('offers', 'current_quests', 'state_proofs')
            for r in read.get(name, []) if r.get('quest_id') == qid]
    instance = selection.get('instance_id')
    accepted = selection.get('accepted_at')
    if instance:
        rows = [r for r in rows if str(r.get('instance_id')) == str(instance)
                or (r.get('state') == 'completed' and accepted and r.get('accept_time') == accepted)]
    if accepted:
        rows = [r for r in rows if r.get('accept_time') == accepted]
    if not rows:
        return 'selected_quest_occurrence_not_current'
    return None
