import fontsCatalog from "@/assets/fonts/irancell/fonts-editor/fonts.json";

export type FontWeightOption = {
  value: string;
  label: string;
};

export type EditorFont = {
  id: string;
  label: string;
  cssFamily: string;
  category: "persian" | "english";
  defaultWeight: string;
  weights: FontWeightOption[];
};

const LEGACY_FONT_ID_MAP: Record<string, string> = {
  "1": "irancell",
  "2": "irancell",
  byekan: "yekan",
  bzarbd: "bzar",
  "bkoodak-bold": "bkoodak",
};

const fonts = fontsCatalog.fonts as EditorFont[];
const fontById = new Map(fonts.map((font) => [font.id, font]));

export const EDITOR_FONTS = fonts;

export const getEditorFont = (fontId?: string): EditorFont | undefined => {
  const normalizedId = LEGACY_FONT_ID_MAP[fontId || ""] || fontId;
  return fontById.get(normalizedId || "irancell") ?? fontById.get("irancell");
};

export const getFontFamilyOptions = () =>
  fonts.map((font) => ({
    label: font.label,
    value: font.id,
  }));

export const getFontWeightOptions = (fontId?: string): FontWeightOption[] => {
  const font = getEditorFont(fontId);
  return font?.weights ?? [{ value: "400", label: "Regular" }];
};

export const getDefaultFontWeight = (fontId?: string): string => {
  const font = getEditorFont(fontId);
  return font?.defaultWeight ?? "400";
};

export const hasMultipleFontWeights = (fontId?: string): boolean =>
  getFontWeightOptions(fontId).length > 1;
