// Shared exam state per block.
// The durable refs live here as a module map: all BlockPanel instances of the same
// block use the SAME refs. So the history survives remounting (block switch, focus
// open/close) AND a late-arriving question is immediately reactive and visible.
// Lost on page reload (module re-init) — deliberately no DB overhead.

import { ref } from 'vue'

const store = new Map() // key "topic::block" -> refs (history + questions pool)

export function usePruefSlot(key) {
  if (!store.has(key)) {
    store.set(key, {
      messages: ref([]),
      phase: ref('idle'),
      aktuelleFrage: ref(''),
      letztesFeedback: ref(''),
      pool: ref([]),        // pre-built question objects of the CURRENT form ({form, ...})
      inflight: ref(0),     // running pool generations (coordinated across instances)
      poolForm: ref(''),    // form the pool is filled for — clear on switch
      musterQuelle: ref([]), // immutable full list of question patterns (empty = fallback)
      musterPool: ref([]),  // working copy: drawn seed without replacement; empty → reset
      musterGeladen: ref(false), // pattern sidecar already fetched?
      quizAktuell: ref(null), // running quiz question {question, options, gewaehlt, done, ...}
      lueckAktuell: ref(null), // running gap-text task {sentence, solution, ..., done}
    })
  }
  return store.get(key)
}

export const clearPruef = (key) => store.delete(key)
