import { defineStore } from 'pinia' import type { ActiveStream, Participant, RoomState, WatchMap } from '~/types/room' export const useRoomStore = defineStore('room', () => { const roomId = ref(null) const roomName = ref('') const joined = ref(false) const joinedAt = ref(null) const participants = ref([]) const activeStreams = ref([]) const watchMap = ref({}) const watchingStreamIds = ref([]) const layoutMode = ref<'grid' | 'focus'>('grid') const focusStreamId = ref(null) const volumeMap = ref>({}) const mutedMap = ref>({}) const error = ref(null) function applyState(state: RoomState) { roomId.value = state.roomId participants.value = state.participants activeStreams.value = state.activeStreams watchMap.value = state.watchMap } function setWatching(streamId: string, on: boolean) { const set = new Set(watchingStreamIds.value) if (on) set.add(streamId) else set.delete(streamId) watchingStreamIds.value = [...set] if (!on && focusStreamId.value === streamId) { focusStreamId.value = watchingStreamIds.value[0] || null } } function ensureAudioDefaults(streamId: string) { if (volumeMap.value[streamId] === undefined) { volumeMap.value = { ...volumeMap.value, [streamId]: 1 } } if (mutedMap.value[streamId] === undefined) { mutedMap.value = { ...mutedMap.value, [streamId]: false } } } function reset() { joined.value = false joinedAt.value = null participants.value = [] activeStreams.value = [] watchMap.value = {} watchingStreamIds.value = [] focusStreamId.value = null error.value = null } const viewersOfStream = computed(() => { const map: Record = {} for (const [viewerId, streamIds] of Object.entries(watchMap.value)) { for (const sid of streamIds) { if (!map[sid]) map[sid] = [] map[sid].push(viewerId) } } return map }) return { roomId, roomName, joined, joinedAt, participants, activeStreams, watchMap, watchingStreamIds, layoutMode, focusStreamId, volumeMap, mutedMap, error, viewersOfStream, applyState, setWatching, ensureAudioDefaults, reset, } })