/**
 * Thin fetch helpers for the IDE tab's API routes.
 *
 * Kept separate from components so the network boundary is obvious and
 * easy to mock in tests and Storybook.
 */
export async function fetchFileTree(applicationId) {
    const res = await fetch(`/api/applications/${applicationId}/files`, { cache: 'no-store' });
    if (!res.ok) {
        const body = (await res.json().catch(() => ({})));
        throw new Error(body.error ?? `Failed to load file tree (${res.status})`);
    }
    const data = (await res.json());
    return data.tree;
}
export async function fetchFileContent(applicationId, path) {
    const url = `/api/applications/${applicationId}/files/content?path=${encodeURIComponent(path)}`;
    const res = await fetch(url, { cache: 'no-store' });
    if (!res.ok) {
        const body = (await res.json().catch(() => ({})));
        throw new Error(body.error ?? `Failed to read file (${res.status})`);
    }
    return (await res.json());
}
export async function saveFileContent(applicationId, path, content) {
    const res = await fetch(`/api/applications/${applicationId}/files/content`, {
        method: 'PUT',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ path, content }),
    });
    if (!res.ok) {
        const body = (await res.json().catch(() => ({})));
        throw new Error(body.error ?? `Failed to save file (${res.status})`);
    }
}
