import { defineStore } from 'pinia'; import { useZiyaConfig } from '../composables/useZiyaConfig'; import { useThemeStore } from './theme'; interface AppState { isInitialized: boolean; isLoading: boolean; error: string | null; currentUser: { name: string; email: string } | null; appVersion: string; toastMessage: string; toastType: 'success' | 'error' | 'info'; showToast: boolean; } export const useAppStore = defineStore('app', { state: (): AppState => { // Get config from composable if available (client-side) const { config } = import.meta.client ? useZiyaConfig() : { config: { app: { version: '1.0.0' } } }; return { isInitialized: false, isLoading: false, error: null, currentUser: null, appVersion: config.app.version, toastMessage: '', toastType: 'info', showToast: false, }; }, getters: { isAuthenticated: state => state.currentUser !== null, userInitials: (state) => { if (!state.currentUser) return '??'; return state.currentUser.name .split(' ') .map(n => n[0]) .join('') .toUpperCase(); }, appInfo: (state) => { // Get config for additional app info const { config } = import.meta.client ? useZiyaConfig() : { config: { app: { name: 'Ziya', version: '1.0.0', description: 'Trading Platform', author: 'bismillahDAO' } } }; return { name: config.app.name, version: state.appVersion, description: config.app.description, author: config.app.author, }; }, }, actions: { async initialize() { if (this.isInitialized) return; this.isLoading = true; this.error = null; try { // Initialize theme system const themeStore = useThemeStore(); await themeStore.initializeTheme(); // Mark as initialized this.isInitialized = true; } catch (error) { this.error = error instanceof Error ? error.message : 'Failed to initialize app'; console.error('App initialization failed:', error); throw error; } finally { this.isLoading = false; } }, setLoading(loading: boolean) { this.isLoading = loading; }, showToastMessage(message: string, type: 'success' | 'error' | 'info' = 'info') { this.toastMessage = message; this.toastType = type; this.showToast = true; setTimeout(() => { this.showToast = false; }, 3000); }, async login(email: string, _password: string) { this.setLoading(true); try { // Simulate API call await new Promise(resolve => setTimeout(resolve, 1000)); // Mock user data this.currentUser = { name: 'John Trader', email: email, }; this.showToastMessage('Welcome back!', 'success'); return true; } catch { this.showToastMessage('Login failed. Please try again.', 'error'); return false; } finally { this.setLoading(false); } }, async logout() { this.setLoading(true); try { // Simulate API call await new Promise(resolve => setTimeout(resolve, 500)); this.currentUser = null; this.showToastMessage('You have been logged out', 'info'); } finally { this.setLoading(false); } }, // Persist user data to localStorage async $afterStateRestored() { if (this.currentUser) { localStorage.setItem('ziya-user', JSON.stringify(this.currentUser)); } else { localStorage.removeItem('ziya-user'); } }, // Initialize from localStorage async initializeFromStorage() { if (import.meta.client) { const storedUser = localStorage.getItem('ziya-user'); if (storedUser) { try { this.currentUser = JSON.parse(storedUser); } catch (error) { console.error('Failed to parse stored user data:', error); localStorage.removeItem('ziya-user'); } } await this.initialize(); } }, }, });