export class ViewerApi {
    static API_BASE = 'https://api.ifuntanhub.dev';

    static assetUrl(uuid) {
        return `${this.API_BASE}/assets/${uuid}`;
    }

    static async fetchBlob(url, onProgress) {
        const response = await fetch(url);
        if (!response.ok) throw new Error(`Gagal mengambil berkas (HTTP ${response.status}).`);

        const contentLength = response.headers.get('Content-Length');
        if (onProgress && contentLength && response.body) {
            const total = Number.parseInt(contentLength, 10);
            const reader = response.body.getReader();
            const chunks = [];
            let received = 0;

            while (true) {
                const { done, value } = await reader.read();
                if (done) break;
                chunks.push(value);
                received += value.length;
                onProgress(received / total);
            }
            return new Blob(chunks);
        }

        return response.blob();
    }
}
