use crate::profile::Profile;
use crate::profile_cache::{USAGE_CACHE_FILE, load_profile_cache};
use crate::usage::{PlanTier, UsageInfo};
pub(crate) fn provider_label(profile: &Profile) -> String {
profile
.provider
.map(|p| p.display_name().to_string())
.unwrap_or_else(|| "anthropic".to_string())
}
pub(crate) fn tier_label(profile: &Profile) -> Option<String> {
if profile.is_third_party() {
return None;
}
let fetched = load_profile_cache::<UsageInfo>(profile.name.as_str(), USAGE_CACHE_FILE)
.and_then(|u| u.plan)
.map(|p| p.tier)
.filter(|t| *t != PlanTier::Unknown);
match fetched {
Some(tier) => tier.short_label(),
None => {
let sub = profile
.credentials
.as_ref()?
.claude_ai_oauth
.as_ref()?
.subscription_type
.as_deref()?;
PlanTier::from_subscription_type(Some(sub)).short_label()
}
}
}
pub(crate) fn windows_json(name: &str) -> serde_json::Value {
let Some(usage) = load_profile_cache::<UsageInfo>(name, USAGE_CACHE_FILE) else {
return serde_json::Value::Array(Vec::new());
};
let windows: Vec<serde_json::Value> = usage
.windows()
.into_iter()
.map(|(label, w)| {
serde_json::json!({
"label": label,
"utilization_pct": w.utilization,
"resets_at": w.resets_at,
})
})
.collect();
serde_json::Value::Array(windows)
}