import { computed, ref } from 'vue'; import type { ActivityPriority, ActivityPrioritySetting } from '@mento/shared'; import { DEFAULT_ACTIVITY_PRIORITY_SETTINGS, parseActivityPrioritySettings, } from '@mento/shared'; import { api } from '@/lib/api'; const items = ref([...DEFAULT_ACTIVITY_PRIORITY_SETTINGS]); const loaded = ref(false); let pending: Promise | null = null; /** تنظیمات اولویت فعالیت/روتین (کش مشترک) */ export function useActivityPriorities() { async function load(force = false): Promise { if (loaded.value && !force) return; if (pending && !force) return pending; pending = api .get('/activities/priorities') .then(({ data }) => { items.value = parseActivityPrioritySettings(data); loaded.value = true; }) .catch(() => { if (!loaded.value) items.value = [...DEFAULT_ACTIVITY_PRIORITY_SETTINGS]; }) .finally(() => { pending = null; }); return pending; } const byKey = computed(() => { const map = new Map(); for (const row of items.value) map.set(row.key, row); return map; }); const labels = computed(() => { const out = {} as Record; for (const row of items.value) out[row.key] = row.label; return out; }); const colors = computed(() => { const out = {} as Record; for (const row of items.value) out[row.key] = row.color; return out; }); function labelOf(key: ActivityPriority | string | null | undefined): string { if (!key) return '—'; return byKey.value.get(key as ActivityPriority)?.label ?? String(key); } function colorOf(key: ActivityPriority | string | null | undefined): string { if (!key) return '#94a3b8'; return byKey.value.get(key as ActivityPriority)?.color ?? '#94a3b8'; } function setLocal(next: ActivityPrioritySetting[]) { items.value = parseActivityPrioritySettings(next); loaded.value = true; } return { items, byKey, labels, colors, loaded, load, labelOf, colorOf, setLocal }; }