import { Configuration, PathDescription } from "common/serializableType"; import { ApiService } from "./api"; export interface ThemeConfig { name: string; displayName: string; icon?: string; version: string; description?: string; author?: string; templates: Map; globalSettings?: { layout?: string; css?: string; }; configuration: Configuration; routes: { index: string; post: string; tag: string; category: string; error: string; loading: string; page: Map; }; } export interface Template { name: string; description?: string; config: { layout?: string; styles?: string[]; scripts?: string[]; }; loader: () => Promise; element: () => React.ReactNode; } export class ThemeService { private static instance: ThemeService; private currentTheme?: ThemeConfig; private api: ApiService; private constructor(api: ApiService) { this.api = api; } public static getInstance(api?: ApiService): ThemeService { if (!ThemeService.instance && api) { ThemeService.instance = new ThemeService(api); } return ThemeService.instance; } public async getCurrentTheme(): Promise { try { const themeConfig = await this.api.request( "/theme/current", { method: "GET" }, ); this.currentTheme = themeConfig; } catch (error) { console.error("Failed to initialize theme:", error); throw error; } } public getThemeConfig(): ThemeConfig | undefined { return this.currentTheme; } public async updateThemeConfig(config: Partial): Promise { try { const updatedConfig = await this.api.request( "/theme/config", { method: "PUT", headers: { "Content-Type": "application/json", }, body: JSON.stringify(config), }, ); await this.loadTheme(updatedConfig); } catch (error) { console.error("Failed to update theme configuration:", error); throw error; } } }