import { get, post } from "./serviceHelper";
let systemStatusInFlight = null;
let systemStatusCache = null;
const SYSTEM_STATUS_CACHE_TTL_MS = 15_000;
export function getSystemStatus(options) {
    const force = options?.force === true;
    const url = force ? "/parhelia/status?refresh=1" : "/parhelia/status";
    if (!force &&
        systemStatusCache &&
        Date.now() - systemStatusCache.fetchedAt < SYSTEM_STATUS_CACHE_TTL_MS) {
        return Promise.resolve(systemStatusCache.result);
    }
    if (!force && systemStatusInFlight) {
        return systemStatusInFlight;
    }
    const requestPromise = get(url)
        .then((result) => {
        if (result.type === "success" && result.data) {
            systemStatusCache = {
                fetchedAt: Date.now(),
                result,
            };
        }
        return result;
    })
        .finally(() => {
        if (systemStatusInFlight === requestPromise) {
            systemStatusInFlight = null;
        }
    });
    systemStatusInFlight = requestPromise;
    return requestPromise;
}
export function getEditorSettings() {
    return get("/parhelia/settings");
}
/**
 * Persists global system settings (administrator only). Only the provided keys
 * are updated. Returns the full, freshly-read settings snapshot on success.
 */
export function updateSystemSettings(update) {
    return post("/parhelia/system-settings", update);
}
export function getClusterInstances() {
    return get("/parhelia/cluster/instances");
}
export function getFieldTypeSerializers() {
    return get("/parhelia/fieldtypeserializers");
}
export function getAgentTools() {
    return get("/parhelia/agent-tools");
}
export function deleteStaleClusterInstance(instanceId) {
    return post("/parhelia/cluster/instances/delete", { instanceId });
}
export function getSamplePrompts() {
    return get("/parhelia/sampleprompts");
}
export function getHomeQuickActions() {
    return get("/parhelia/homequickactions");
}
export function getAiTextEditPrompts() {
    return get("/parhelia/aitexteditprompts");
}
export function saveUserPreferences(preferences) {
    return post("/parhelia/savepreferences", preferences);
}
//# sourceMappingURL=systemService.js.map