"use client";

import { createContext, useContext } from "react";
import { useLang } from "@/context/LanguageContext";

export interface PortalConfig {
  /** category (CLASS, SECTION…) → custom label ("Course"…) */
  academicLabels: Record<string, string>;
  /** module keys the institution enabled (null = all on) */
  enabledModules: string[] | null;
}

export const PortalConfigContext = createContext<PortalConfig>({
  academicLabels: {},
  enabledModules: null,
});

export function usePortalConfig(): PortalConfig {
  return useContext(PortalConfigContext);
}

/** Label for an academic category with the institution's rename applied —
    locale-aware: the Bangla override (`${cat}_BN`) wins in Bangla, the
    English override everywhere else. */
export function useCategoryLabel() {
  const { academicLabels } = usePortalConfig();
  const { locale } = useLang();
  return (category: string, fallback: string) => {
    const en = academicLabels[category];
    const bn = academicLabels[`${category}_BN`];
    if (locale === "bn") return bn ?? en ?? fallback;
    return en ?? bn ?? fallback;
  };
}
