/**
 * Detects Android Telegram Mini App performance class from User-Agent and sets
 * data-perf on document.documentElement so CSS can reduce animations on low-end devices.
 * @see https://core.telegram.org/bots/webapps#additional-data-in-user-agent
 */

import { retrieveAndroidDeviceData } from "@telegram-apps/sdk-react";

const DATA_ATTR = "data-perf";

/**
 * Runs once: if running in Telegram on Android with LOW performance class,
 * sets data-perf="low" on the document root for CSS to minimize animations.
 */
export function applyAndroidPerformanceClass(): void {
  if (typeof document === "undefined") return;
  try {
    const data = retrieveAndroidDeviceData();
    const perf = data?.performanceClass;
    if (perf === "LOW") {
      document.documentElement.setAttribute(DATA_ATTR, "low");
    }
  } catch {
    // Not in Telegram or not Android; ignore.
  }
}
