import { useCallback, useEffect, useState } from "react";
const DEFAULT_USAGE = {
    totalTokens: 0,
    totalImages: 0,
    dailyTokens: 0,
    dailyImages: 0,
    totalCost: 0,
    dailyCost: 0,
};
const DEFAULT_LIMITS = {
    totalTokens: -1,
    dailyTokens: -1,
    monthlyTokens: -1,
    totalImages: -1,
    dailyImages: -1,
    monthlyImages: -1,
};
function toNumber(value, fallback) {
    return typeof value === "number" && Number.isFinite(value) ? value : fallback;
}
function normalizeQuotaInfo(value) {
    if (!value || typeof value !== "object")
        return null;
    const payload = value;
    // Older websocket payloads may send usage fields at the top level.
    const rawUsage = payload.usage ?? payload;
    const rawLimits = payload.limits ?? {};
    return {
        usage: {
            totalTokens: toNumber(rawUsage.totalTokens, DEFAULT_USAGE.totalTokens),
            totalImages: toNumber(rawUsage.totalImages, DEFAULT_USAGE.totalImages),
            dailyTokens: toNumber(rawUsage.dailyTokens, DEFAULT_USAGE.dailyTokens),
            dailyImages: toNumber(rawUsage.dailyImages, DEFAULT_USAGE.dailyImages),
            totalCost: toNumber(rawUsage.totalCost, DEFAULT_USAGE.totalCost),
            dailyCost: toNumber(rawUsage.dailyCost, DEFAULT_USAGE.dailyCost),
            dailyHistory: Array.isArray(rawUsage.dailyHistory)
                ? rawUsage.dailyHistory
                : undefined,
        },
        limits: {
            totalTokens: toNumber(rawLimits.totalTokens, DEFAULT_LIMITS.totalTokens),
            dailyTokens: toNumber(rawLimits.dailyTokens, DEFAULT_LIMITS.dailyTokens),
            monthlyTokens: toNumber(rawLimits.monthlyTokens, DEFAULT_LIMITS.monthlyTokens),
            totalImages: toNumber(rawLimits.totalImages, DEFAULT_LIMITS.totalImages),
            dailyImages: toNumber(rawLimits.dailyImages, DEFAULT_LIMITS.dailyImages),
            monthlyImages: toNumber(rawLimits.monthlyImages, DEFAULT_LIMITS.monthlyImages),
        },
    };
}
export function useQuota(params) {
    const { showError } = params;
    const [quotaInfo, setQuotaInfo] = useState(null);
    const updateQuotaInfo = useCallback((value) => {
        setQuotaInfo(normalizeQuotaInfo(value));
    }, []);
    const isQuotaExceeded = useCallback(() => {
        if (!quotaInfo)
            return false;
        const { usage, limits } = quotaInfo;
        if (limits.totalTokens > 0 && usage.totalTokens >= limits.totalTokens)
            return true;
        if (limits.totalImages > 0 && usage.totalImages >= limits.totalImages)
            return true;
        return false;
    }, [quotaInfo]);
    const getQuotaWarningMessage = useCallback(() => {
        if (!quotaInfo)
            return null;
        const { usage, limits } = quotaInfo;
        const warnings = [];
        if (limits.totalTokens > 0) {
            const tokenPercentage = (usage.totalTokens / limits.totalTokens) * 100;
            if (tokenPercentage >= 100)
                warnings.push(`Token limit exceeded (${usage.totalTokens}/${limits.totalTokens})`);
            else if (tokenPercentage >= 90)
                warnings.push(`Token usage high: ${Math.round(tokenPercentage)}% (${usage.totalTokens}/${limits.totalTokens})`);
        }
        if (limits.totalImages > 0) {
            const imagePercentage = (usage.totalImages / limits.totalImages) * 100;
            if (imagePercentage >= 100)
                warnings.push(`Image limit exceeded (${usage.totalImages}/${limits.totalImages})`);
            else if (imagePercentage >= 90)
                warnings.push(`Image usage high: ${Math.round(imagePercentage)}% (${usage.totalImages}/${limits.totalImages})`);
        }
        return warnings.length > 0 ? warnings.join(", ") : null;
    }, [quotaInfo]);
    useEffect(() => {
        const warningMessage = getQuotaWarningMessage();
        if (warningMessage) {
            const exceeded = isQuotaExceeded();
            showError({
                summary: exceeded ? "AI Quota Exceeded" : "AI Quota Warning",
                details: warningMessage,
            });
        }
    }, [quotaInfo, getQuotaWarningMessage, isQuotaExceeded, showError]);
    return {
        quotaInfo,
        setQuotaInfo: updateQuotaInfo,
        isQuotaExceeded,
        getQuotaWarningMessage,
    };
}
//# sourceMappingURL=useQuota.js.map