import type { LibraryItem, LibraryStrings } from "@/components/TutorialLibrary";
import { getT } from "@/lib/content";
import type { Locale } from "@/lib/locales";
import { MODULES } from "@/lib/routes";
import type { Tutorial } from "@/lib/schemas";

/**
 * Shaping the tutorial index for the library component. The code column shows
 * the module code for the four modules, and SET / SYS for the setup and
 * system videos, so the library reads with the same codes as the rest of the
 * site.
 */

function codeFor(module: Tutorial["module"]): string {
  if (module === "setup") return "SET";
  if (module === "system") return "SYS";
  return MODULES.find((m) => m.id === module)?.code ?? "—";
}

/**
 * The language of the *title*, which is not always the language of the video.
 *
 * These titles are quoted, not translated: they are what Sulsys called the
 * recording when it published it, and that stays true in all four of this
 * site's languages. But an Albanian title sitting inside an English page has
 * to say so, or a screen reader will read it with English phonemes and a
 * reader will take it for part of our own prose. Cyrillic settles it; the rest
 * is Albanian or a brand name, and Albanian is right for both.
 */
function titleLang(title: string): "sq" | "mk" {
  return /[Ѐ-ӿ]/.test(title) ? "mk" : "sq";
}

function languageLabel(locale: Locale, language: Tutorial["language"]): string {
  const t = getT(locale);
  if (language === "sq") return t("tutorial.langSq");
  if (language === "mk") return t("tutorial.langMk");
  return t("tutorial.langSqMk");
}

export function tutorialItems(locale: Locale, tutorials: Tutorial[]): LibraryItem[] {
  const t = getT(locale);
  return tutorials.map((tutorial) => ({
    id: tutorial.id,
    title: tutorial.title,
    titleLang: titleLang(tutorial.title),
    module: tutorial.module,
    code: codeFor(tutorial.module),
    meta: t("support.videoMeta", {
      duration: tutorial.duration,
      language: languageLabel(locale, tutorial.language),
      year: tutorial.date.slice(0, 4),
    }),
  }));
}

export function tutorialFilters(locale: Locale) {
  const t = getT(locale);
  return [
    ...MODULES.map((module) => ({
      key: module.id,
      label: `${module.code} ${module.brandName}`,
    })),
    { key: "setup", label: t("support.moduleSetup") },
    { key: "system", label: t("support.moduleSystem") },
  ];
}

export function TUTORIAL_LIBRARY_STRINGS(locale: Locale): LibraryStrings {
  const t = getT(locale);
  return {
    filterAria: t("support.filterAria"),
    all: t("support.filterAll"),
    play: t("tutorial.play"),
    playAria: t("tutorial.playAria"),
    watchOn: t("tutorial.watchOn"),
    privacy: t("tutorial.privacy"),
    empty: t("support.empty"),
    close: t("support.close"),
  };
}
