"use client";

import { CapacitorHttp } from "@capacitor/core";

const TIMEOUT_MS = 8000;

/**
 * GET JSON natively, for static files on app.rangook.ir: the server sends no CORS
 * headers for them, and native requests need none.
 */
export async function getJson(url: string): Promise<unknown> {
    const res = await CapacitorHttp.get({
        url,
        // Cache-buster, as for version.json.
        params: { t: String(Date.now()) },
        headers: { Accept: "application/json" },
        connectTimeout: TIMEOUT_MS,
        readTimeout: TIMEOUT_MS,
        responseType: "json",
    });

    if (res.status < 200 || res.status >= 300) {
        throw new Error(`GET ${url} -> HTTP ${res.status}`);
    }

    return typeof res.data === "string" ? JSON.parse(res.data) : res.data;
}
