/**
 * Format duration in seconds to human-readable string.
 * 0 → "Single Run", 45 → "0:45", 75 → "1:15", 300 → "5:00"
 */
export function formatDuration(seconds) {
  if (seconds === 0) return "Single Run";
  const min = Math.floor(seconds / 60);
  const sec = seconds % 60;
  return `${min}:${sec.toString().padStart(2, "0")}`;
}

/**
 * Parse a JSON-encoded localized string into a dict.
 * If already a dict, returns it as-is. Returns {} on failure.
 */
export function parseLocalizedString(value) {
  if (!value) return {};
  if (typeof value === "object") return value;
  try {
    const parsed = JSON.parse(value);
    if (typeof parsed === "object" && parsed !== null) return parsed;
  } catch {
    // Not valid JSON — treat as plain string
    return { en: value };
  }
  return {};
}

/**
 * Serialize a localized dict back to a JSON string for storage.
 */
export function serializeLocalizedString(dict) {
  if (!dict || typeof dict !== "object") return "";
  return JSON.stringify(dict);
}

/**
 * Get a localized value from a JSON string or dict, with fallback chain:
 * selected lang → "en" → first available value → fallback
 */
export function getLocalizedValue(value, lang, fallback = "") {
  const dict = parseLocalizedString(value);
  return dict[lang] || dict["en"] || Object.values(dict)[0] || fallback;
}

/**
 * Normalize color string for HTML color input.
 * Converts "0xFFRRGGBB" or "0xRRGGBB" format to "#RRGGBB".
 */
export function normalizeColor(val) {
  if (!val) {
    try {
      if (typeof window !== "undefined" && window.getComputedStyle) {
        const v = getComputedStyle(document.documentElement).getPropertyValue("--color-default").trim();
        if (v) return v;
      }
    } catch (e) {
      // fallthrough
    }
    return "var(--color-default)";
  }
  if (val.startsWith("0x") || val.startsWith("0X")) {
    const hex = val.slice(2);
    // If 8 chars (AARRGGBB), skip alpha
    return `#${hex.length === 8 ? hex.slice(2) : hex}`;
  }
  if (val.startsWith("#")) return val;
  return `#${val}`;
}

/**
 * Master list of common ISO 639-1 language codes.
 */
export const LANGUAGE_MASTER_LIST = [
  { code: "en", name: "English" },
  { code: "el", name: "Greek" },
  { code: "sr", name: "Serbian" },
  { code: "bg", name: "Bulgarian" },
  { code: "ro", name: "Romanian" },
  { code: "ru", name: "Russian" },
  { code: "uk", name: "Ukrainian" },
  { code: "ka", name: "Georgian" },
  { code: "ar", name: "Arabic" },
  { code: "de", name: "German" },
  { code: "fr", name: "French" },
  { code: "es", name: "Spanish" },
  { code: "it", name: "Italian" },
  { code: "pt", name: "Portuguese" },
  { code: "nl", name: "Dutch" },
  { code: "pl", name: "Polish" },
  { code: "cs", name: "Czech" },
  { code: "sk", name: "Slovak" },
  { code: "hu", name: "Hungarian" },
  { code: "hr", name: "Croatian" },
  { code: "sl", name: "Slovenian" },
  { code: "mk", name: "Macedonian" },
  { code: "sq", name: "Albanian" },
  { code: "tr", name: "Turkish" },
  { code: "ja", name: "Japanese" },
  { code: "zh", name: "Chinese" },
  { code: "ko", name: "Korean" },
  { code: "hi", name: "Hindi" },
  { code: "he", name: "Hebrew" },
  { code: "fi", name: "Finnish" },
  { code: "sv", name: "Swedish" },
  { code: "da", name: "Danish" },
  { code: "no", name: "Norwegian" },
];

/**
 * Get display name for a language code.
 */
export function getLanguageName(code) {
  const lang = LANGUAGE_MASTER_LIST.find((l) => l.code === code);
  return lang ? lang.name : code.toUpperCase();
}
