import { appPreferences } from '@axium/core';
import { fetchAPI } from './requests.js';
import { useCache } from './cache.js';
export async function getAppPreferences(userId, appId) {
    const schema = appPreferences.get(appId);
    if (!schema)
        throw new Error(`Missing schema for "${appId}"`);
    const pref = await useCache('app_preferences', userId + ':' + appId, async () => {
        const result = await fetchAPI('GET', 'users/:id/preferences/:appId', {}, userId, appId);
        return schema.parse(result);
    });
    return pref;
}
export async function setAppPreferences(userId, appId, preferences) {
    const schema = appPreferences.get(appId);
    if (!schema)
        throw new Error(`Missing schema for "${appId}"`);
    const result = await fetchAPI('POST', 'users/:id/preferences/:appId', preferences, userId, appId);
    return schema.parse(result);
}
export async function clearAppPreferences(userId, appId) {
    const schema = appPreferences.get(appId);
    if (!schema)
        throw new Error(`Missing schema for "${appId}"`);
    const result = await fetchAPI('DELETE', 'users/:id/preferences/:appId', {}, userId, appId);
    return schema.parse(result);
}
export async function appPref(userId, appId, key) {
    const pref = await getAppPreferences(userId, appId);
    return pref[key];
}
