import { Playlist, ServerType, ApiResponse, PlexServerConnection, PlexConnectionSettings, RegexReplacement, UiConfig, SyncSettings } from '../types'; const API_PREFIX = '/api'; const parseJson = async (response: Response): Promise> => { try { const data = await response.json(); return data as ApiResponse; } catch (error) { console.error('Failed to parse API response', error); return { data: {} as T, status: 'error', message: 'Invalid response from server' }; } }; const mapServerType = (type: ServerType) => type === ServerType.LOCAL ? 'local' : 'cloud'; export const apiService = { getUiConfig: async (): Promise> => { try { const response = await fetch(`${API_PREFIX}/ui-config`); const result = await parseJson(response); if (!response.ok || result.status !== 'success') { return { data: { statusCheckIntervalSeconds: 60 }, status: 'error', message: result.message || '无法获取前端配置' }; } return result; } catch (error) { return { data: { statusCheckIntervalSeconds: 60 }, status: 'error', message: '无法获取前端配置' }; } }, getSettings: async (): Promise> => { try { const response = await fetch(`${API_PREFIX}/settings`); const result = await parseJson(response); if (!response.ok || result.status !== 'success') { return { data: { syncStrategy: 'LOCAL_OVERWRITE', regexRules: [] }, status: 'error', message: result.message || '无法获取设置' }; } return result; } catch (error) { return { data: { syncStrategy: 'LOCAL_OVERWRITE', regexRules: [] }, status: 'error', message: '无法获取设置' }; } }, saveStrategy: async (strategy: string): Promise> => { try { const response = await fetch(`${API_PREFIX}/settings/strategy`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ strategy }) }); return await parseJson(response); } catch (error) { return { data: { syncStrategy: strategy, regexRules: [] }, status: 'error', message: '无法保存同步策略' }; } }, getRegexRules: async (): Promise> => { try { const response = await fetch(`${API_PREFIX}/regex-rules`); return await parseJson(response); } catch (error) { return { data: [], status: 'error', message: '无法获取正则规则' }; } }, saveRegexRules: async (rules: RegexReplacement[]): Promise> => { try { const response = await fetch(`${API_PREFIX}/regex-rules`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(rules) }); return await parseJson(response); } catch (error) { return { data: [], status: 'error', message: '无法保存正则规则' }; } }, getPlaylists: async (serverType: ServerType): Promise> => { try { const response = await fetch(`${API_PREFIX}/playlists/${mapServerType(serverType)}`); const result = await parseJson(response); if (!response.ok || result.status !== 'success') { return { data: [], status: 'error', message: result.message || 'Failed to fetch playlists' }; } return result; } catch (error) { console.error(`Error fetching ${serverType} playlists:`, error); return { data: [], status: 'error', message: 'Failed to fetch playlists' }; } }, getServerStatus: async (): Promise> => { try { const response = await fetch(`${API_PREFIX}/server/status`); const result = await parseJson(response); if (!response.ok || result.status !== 'success') { return { data: { isConnected: false }, status: 'error', message: result.message || 'Failed to connect to server' }; } return result; } catch (error) { return { data: { isConnected: false }, status: 'error', message: 'Failed to connect to server' }; } }, connectToPlex: async (settings: PlexConnectionSettings): Promise> => { try { const response = await fetch(`${API_PREFIX}/server/connect`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(settings) }); return await parseJson<{ token: string, serverInfo: PlexServerConnection }>(response); } catch (error: any) { return { data: { token: '', serverInfo: { isConnected: false } }, status: 'error', message: error?.message || 'Connection failed' }; } }, selectLibrary: async (library: string): Promise> => { try { const response = await fetch(`${API_PREFIX}/server/library`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ library }) }); return await parseJson<{ libraryName: string }>(response); } catch (error) { return { data: { libraryName: library }, status: 'error', message: '无法切换媒体库' }; } } };