import json


def get_set_fields_command(editor, *, field_overrides=None):
    """
    Get data for note fields and create JavaScript command that will set the fields in the UI.

    This is based on editor.loadNote, however it only sets the fields, rather than everything
    else.

    field_overrides can be set to override the current value for a field, thus ignoring whatever
    value is found in the note.  This is needed in some cases because the note data may be stale
    compared to the UI.  The UI has the most up to date field value, which may not yet be persisted
    in the note.
    """

    data = []
    for fld, val in editor.note.items():
        if field_overrides and fld in field_overrides:
            val = field_overrides[fld]
        data.append((fld, editor.mw.col.media.escape_media_filenames(val)))

    return "setFields({});".format(
        json.dumps(data)
    )