import { get } from "./serviceHelper";
// In-flight de-duplication and short TTL cache to avoid duplicate network requests
const customCommandsInFlight = new Map();
const customCommandsCache = new Map();
const CUSTOM_COMMANDS_TTL_MS = 60_000; // 1 minute
/**
 * Loads custom commands from the backend API for a specific menu location.
 * Includes caching and request deduplication.
 *
 * @param menu - The menu location to load commands for
 * @returns Array of custom command configurations
 */
export async function loadCustomCommands(menu) {
    const url = `/parhelia/customCommands?menu=${menu}`;
    const cacheKey = `custom-commands-${menu}`;
    // Serve from cache if fresh
    const cached = customCommandsCache.get(cacheKey);
    if (cached && Date.now() - cached.timestamp < CUSTOM_COMMANDS_TTL_MS) {
        return cached.data;
    }
    // Reuse in-flight request if one exists
    const inFlight = customCommandsInFlight.get(cacheKey);
    if (inFlight)
        return inFlight;
    const requestPromise = (async () => {
        const response = await get(url);
        if (response.type !== "success") {
            console.error(`Error loading custom commands for ${menu}:`, response.details);
            return [];
        }
        const data = response.data || [];
        customCommandsCache.set(cacheKey, { timestamp: Date.now(), data });
        return data;
    })().finally(() => {
        customCommandsInFlight.delete(cacheKey);
    });
    customCommandsInFlight.set(cacheKey, requestPromise);
    return requestPromise;
}
//# sourceMappingURL=customCommandsService.js.map