import { ref } from "vue"; import { defineStore } from "pinia"; import * as api from "@/api/platforms"; import type { Platform } from "@/api/platforms"; /** * The global platform catalog (milestone 463). Shared by every project, so * it is fetched once per session — as the canonical-area catalog is. */ export const usePlatformsStore = defineStore("platforms", () => { const catalog = ref([]); const loaded = ref(false); const loading = ref(false); async function fetchCatalog(force = false) { if (loaded.value && !force) return catalog.value; loading.value = true; try { catalog.value = await api.listPlatforms(); loaded.value = true; } catch { // The catalog is a vocabulary for a form; an empty one degrades the // form rather than failing the screen it sits on. catalog.value = []; } finally { loading.value = false; } return catalog.value; } async function createEntry(data: { name: string; description?: string; markers?: string[] }) { const entry = await api.createPlatform(data); catalog.value.push(entry); return entry; } async function updateEntry( id: number, data: Partial<{ name: string; description: string; order_index: number; markers: string[] }>, ) { const entry = await api.updatePlatform(id, data); const idx = catalog.value.findIndex((p) => p.id === id); if (idx >= 0) catalog.value[idx] = entry; return entry; } return { catalog, loaded, loading, fetchCatalog, createEntry, updateEntry }; });