import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { useApi } from '../composables/useApi.js'

export const usePlatformsStore = defineStore('platforms', () => {
  const api = useApi()

  const byKey = ref(new Map())
  const loaded = ref(false)

  const list = computed(() => Array.from(byKey.value.values()))

  async function loadAll() {
    if (loaded.value) return
    const body = await api.get('/api/platforms')
    const m = new Map()
    for (const [k, v] of Object.entries(body.platforms || {})) {
      m.set(k, v)
    }
    byKey.value = m
    loaded.value = true
  }

  return { byKey, loaded, list, loadAll }
})
