How do I dynamically add content from a dictionary defined in a template?

Hello. You are stepping into the same trap as How to keep exercise and solution together in source, but render them separately? - #3 by Andrew and Why is my random shuffle always producing the same result when using Suiji? - #9 by Andrew.

#let ref-type = (definition: "Definition", theorem: "Theorem", lemma: "Lemma")
#let lecture-refs = (
  def1-1: (
    lecture: 1,
    index: 1,
    type: ref-type.definition,
    content: [This is my first definition.],
  ),
  the1-2: (
    lecture: 1,
    index: 2,
    type: ref-type.theorem,
    content: [This is the second entry but its a theorem.],
  ),
  the2-1: (
    lecture: 2,
    index: 1,
    type: ref-type.theorem,
    content: [This theorem is relevant but I won't need it for the notes.],
  ),
  def2-2: (
    lecture: 2,
    index: 2,
    type: ref-type.definition,
    content: [Let's define some more items.],
  ),
  lem3-1: (
    lecture: 3,
    index: 1,
    type: ref-type.lemma,
    content: [Now we add a lemma for a change.],
  ),
)

#let lecture-ref(def) = context {
  let state = state("used-literature-refs", ())
  let array = state.get()
  if not array.contains(def) {
    state.update(array => array + (def,))
  }
  ref(label(def))
}

#let used-lecture-refs() = context {
  let used = state("used-literature-refs").final()
  if used.len() == 0 { return }
  [= Lecture References]
  set heading(numbering: "1.1")
  for (key, value) in used.map(key => (key, lecture-refs.at(key))) {
    counter(heading).update((value.lecture, value.index - 1))
    [#heading(depth: 2, supplement: value.type)[#value.type]#label(key)]
    value.content
  }
}

#let template(doc) = {
  doc
  used-lecture-refs()
}

#show: template

= Homework
In my homework I need to reference some content from the lectures like
#lecture-ref("def1-1"), #lecture-ref("the1-2"), #lecture-ref("def2-2"),
#lecture-ref("lem3-1")