import { apiClient } from './client'; import type { Repository, FileNode, FileContent } from '../types/api'; export const reposApi = { listRepositories: async (sortBy: 'updated' | 'created' | 'name' = 'updated'): Promise => { const { data } = await apiClient.get<{ repositories: Repository[] }>('/api/repos', { params: { sort: sortBy }, }); return data.repositories; }, listFiles: async ( owner: string, repo: string, path: string = '', branch: string = '', extensions: string[] = [] ): Promise<{ files: FileNode[]; current_branch: string }> => { const { data } = await apiClient.get<{ files: FileNode[]; current_branch: string }>( `/api/repos/${owner}/${repo}/files`, { params: { path, branch, extensions: extensions.length > 0 ? extensions.join(',') : undefined, }, } ); return data; }, getFileContent: async ( owner: string, repo: string, path: string, branch: string = '' ): Promise => { const { data } = await apiClient.get(`/api/repos/${owner}/${repo}/files/${path}`, { params: { branch }, }); return data; }, invalidateCache: async (owner: string, repo: string): Promise<{ invalidated_count: number }> => { const { data } = await apiClient.delete<{ invalidated_count: number }>(`/api/repos/${owner}/${repo}/cache`); return data; }, createFile: async ( owner: string, repo: string, path: string, content: string = '', message?: string ): Promise<{ success: boolean; path: string }> => { const { data } = await apiClient.post<{ success: boolean; path: string }>( `/api/repos/${owner}/${repo}/create-file`, { path, content, message } ); return data; }, createFolder: async ( owner: string, repo: string, path: string, message?: string ): Promise<{ success: boolean; path: string }> => { const { data } = await apiClient.post<{ success: boolean; path: string }>( `/api/repos/${owner}/${repo}/create-folder`, { path, message } ); return data; }, renameItem: async ( owner: string, repo: string, oldPath: string, newPath: string, message?: string ): Promise<{ success: boolean; old_path: string; new_path: string }> => { const { data } = await apiClient.post<{ success: boolean; old_path: string; new_path: string }>( `/api/repos/${owner}/${repo}/rename`, { old_path: oldPath, new_path: newPath, message } ); return data; }, getPendingChanges: async ( owner: string, repo: string ): Promise<{ pending_changes: any[]; count: number }> => { const { data } = await apiClient.get<{ pending_changes: any[]; count: number }>( `/api/repos/${owner}/${repo}/pending-changes` ); return data; }, discardChanges: async ( owner: string, repo: string ): Promise<{ success: boolean; message: string }> => { const { data } = await apiClient.delete<{ success: boolean; message: string }>( `/api/repos/${owner}/${repo}/pending-changes` ); return data; }, };