import { defineStore } from 'pinia' import { ref } from 'vue' import type {JoinResult, Whiteboard} from '@/types' import { whiteboardService } from '@/services/whiteboardService' import type {WhiteboardJoinPolicy} from "@/enums"; export const useWhiteboardsStore = defineStore('whiteboards', () => { const ownedWhiteboards = ref([]) const recentWhiteboards = ref([]) const currentWhiteboard = ref(null) const isLoading = ref(false) const isWaitingToJoin = ref(false) const error = ref(null) async function getWhiteboardHistory() { isLoading.value = true error.value = null try { ownedWhiteboards.value = await whiteboardService.getWhiteboardHistory() } catch (err: any) { error.value = err.message ?? 'Failed to load whiteboards' } finally { isLoading.value = false } } async function getRecentWhiteboards() { isLoading.value = true error.value = null try { recentWhiteboards.value = await whiteboardService.getRecentWhiteboards() } catch (err: any) { error.value = err.message ?? 'Failed to load whiteboards' } finally { isLoading.value = false } } async function createNewWhiteboard(title: string, joinPolicy: WhiteboardJoinPolicy, maxParticipants: number): Promise { let newWhiteboard: Whiteboard; isLoading.value = true error.value = null try { const newId = await whiteboardService.createNewWhiteboard(title, joinPolicy, maxParticipants) newWhiteboard = await whiteboardService.getWhiteboardById(newId) } catch (err: any) { error.value = err.message ?? 'Failed to create whiteboard' throw err } finally { isLoading.value = false } ownedWhiteboards.value.push(newWhiteboard) return newWhiteboard.id; } async function joinWhiteboardWithCode(code: string): Promise { isLoading.value = true; try { return await whiteboardService.joinWhiteboard(code); } catch (err: any) { error.value = err.message ?? 'Failed to join whiteboard'; throw err; } finally { isLoading.value = false; } } function startWaitingToJoin() { isWaitingToJoin.value = true; } function stopWaitingToJoin() { isWaitingToJoin.value = false; } async function deleteWhiteboard(id: string): Promise { isLoading.value = true error.value = null try { await whiteboardService.deleteWhiteboard(id) ownedWhiteboards.value = ownedWhiteboards.value.filter(wb => wb.id !== id) } catch (err: any) { error.value = err.message ?? 'Failed to delete whiteboard' throw err } finally { isLoading.value = false } } function getCurrentWhiteboard(): Whiteboard | null { return currentWhiteboard.value } function selectWhiteboard(whiteboardId: string) { currentWhiteboard.value = ownedWhiteboards.value.find(wb => wb.id === whiteboardId) || recentWhiteboards.value.find(wb => wb.id === whiteboardId) || null } function deselectWhiteboard() { currentWhiteboard.value = null } function clearWhiteboards() { ownedWhiteboards.value = [] recentWhiteboards.value = [] } return { ownedWhiteboards: ownedWhiteboards, recentWhiteboards: recentWhiteboards, isLoading, isWaitingToJoin, error, getWhiteboardHistory: getWhiteboardHistory, getRecentWhiteboards: getRecentWhiteboards, createNewWhiteboard: createNewWhiteboard, joinWhiteboardWithCode: joinWhiteboardWithCode, startWaitingToJoin: startWaitingToJoin, stopWaitingToJoin: stopWaitingToJoin, deleteWhiteboard: deleteWhiteboard, getCurrentWhiteboard: getCurrentWhiteboard, selectWhiteboard: selectWhiteboard, deselectWhiteboard: deselectWhiteboard, clearWhiteboards: clearWhiteboards, } })