// components/fontTransforms.jsx

// 1) Unicode-Blöcke
const unicodeBlocks = {
  sansSerif:       { upperStart: 0x1D5A0, lowerStart: 0x1D5BA },
  sansSerifBold:   { upperStart: 0x1D5D4, lowerStart: 0x1D5EE },
  script:          { upperStart: 0x1D49C, lowerStart: 0x1D4B6 },
  scriptBold:      { upperStart: 0x1D4D0, lowerStart: 0x1D4EA },
  fraktur:         { upperStart: 0x1D504, lowerStart: 0x1D51E },
  frakturBold:     { upperStart: 0x1D56C, lowerStart: 0x1D586 },
  monospace:       { upperStart: 0x1D670, lowerStart: 0x1D68A },
  fullwidth:       { upperStart: 0xFF21,  lowerStart: 0xFF41 }
};

// 2) Unicode-Mapping-Funktion
const mapUnicode = (char, block) => {
  const code = char.charCodeAt(0);
  if (code >= 65 && code <= 90) return String.fromCodePoint(block.upperStart + (code - 65));
  if (code >= 97 && code <= 122) return String.fromCodePoint(block.lowerStart + (code - 97));
  return char;
};

const createTransform = (blockKey) => (text) =>
  text.split('').map((c) => mapUnicode(c, unicodeBlocks[blockKey])).join('');

// 3) Font-Definitionen
const fontList = [
  "abril-fatface", "alegreya", "alfa-slab-one", "almendra", "amatic-sc", "andika",
  "architects-daughter", "audiowide", "averia-libre", "bebas-neue", "black-ops-one",
  "caveat", "cinzel-decorative", "courgette", "dancing-script", "exo", "fjalla-one",
  "germania-one", "glass-antiqua", "gloria-hallelujah", "great-vibes", "holtwood-one-sc",
  "indie-flower", "italiana", "jost", "kaushan-script", "lato", "metal-mania", "montserrat",
  "neucha", "noto-sans", "open-sans", "orbitron", "oswald", "pacifico", "permanent-marker",
  "philosopher", "playfair-display", "poppins", "press-start-2p", "questrial", "quicksand",
  "rajdhani", "raleway", "righteous", "roboto", "sacramento", "satisfy", "space-mono",
  "spectral", "staatliches", "stint-ultra-condensed", "syncopate", "ultra", "unica-one",
  "work-sans", "yellowtail"
];

// 4) Kategorie-Regeln (vereinfacht)
const getCategory = (name) => {
  if (["caveat", "dancing-script", "pacifico", "amatic-sc", "kaushan-script", "courgette", "great-vibes", "satisfy", "sacramento", "neucha", "gloria-hallelujah", "almendra", "indie-flower", "architects-daughter"].includes(name)) return "handwriting";
  if (["bebas-neue", "black-ops-one", "holtwood-one-sc", "abril-fatface", "playfair-display", "permanent-marker", "alfa-slab-one", "germania-one", "oswald", "stint-ultra-condensed"].includes(name)) return "statement";
  if (["exo", "orbitron", "audiowide", "rajdhani", "space-mono", "questrial", "syncopate", "unica-one", "italiana", "staatliches"].includes(name)) return "futuristic";
  if (["press-start-2p", "righteous", "metal-mania", "alegreya", "spectral", "fjalla-one", "glass-antiqua", "cinzel-decorative", "andika"].includes(name)) return "aesthetic";
  return "modern";
};

const blockForCategory = {
  modern: "sansSerif",
  handwriting: "scriptBold",
  statement: "fullwidth",
  futuristic: "monospace",
  aesthetic: "frakturBold"
};

export const fontTransforms = Object.fromEntries(
  fontList.map((font) => {
    const name = font.replace(/-/g, " ").replace(/\b\w/g, (l) => l.toUpperCase());
    const category = getCategory(font);
    const block = blockForCategory[category];
    return [name, {
      transform: createTransform(block),
      category,
      description: `${name} – Unicode-Stil automatisch zugewiesen` ,
      className: `font-${font}`
    }];
  })
);

export const transformText = (text, fontName) => {
  const font = fontTransforms[fontName];
  if (!font || !text) return { transformed: text, fontClassName: "" };
  return {
    transformed: font.transform(text),
    fontClassName: font.className
  };
};

export const getPopularFonts = () => Object.keys(fontTransforms).slice(0, 10);

export const getFontsByCategory = (category) =>
  category === "all"
    ? Object.keys(fontTransforms)
    : Object.keys(fontTransforms).filter(
        (f) => fontTransforms[f].category === category
      );