/**
 * Map common colour names to hex so we can auto-generate colour swatches from
 * option values without the merchant typing every hex. Covers English and
 * Danish (the store's language). Returns null when we can't confidently map.
 */
const NAMED_COLORS = {
  // English
  black: "#111111",
  white: "#ffffff",
  red: "#e11d2a",
  green: "#1f9e4b",
  blue: "#1f6fe1",
  navy: "#1b2a4a",
  yellow: "#f4c20d",
  orange: "#f2701f",
  purple: "#8a3ffc",
  pink: "#ff5fa2",
  brown: "#7b4a2b",
  beige: "#e8dcc0",
  grey: "#8a8a8a",
  gray: "#8a8a8a",
  silver: "#c9c9c9",
  gold: "#d4af37",
  turquoise: "#1ab6b0",
  teal: "#1a8f8a",
  cream: "#f4efe2",
  khaki: "#9a8b5a",
  burgundy: "#6e1423",
  // Danish
  sort: "#111111",
  hvid: "#ffffff",
  rød: "#e11d2a",
  roed: "#e11d2a",
  grøn: "#1f9e4b",
  groen: "#1f9e4b",
  blå: "#1f6fe1",
  blaa: "#1f6fe1",
  gul: "#f4c20d",
  orange_da: "#f2701f",
  lilla: "#8a3ffc",
  lyserød: "#ff5fa2",
  lyseroed: "#ff5fa2",
  brun: "#7b4a2b",
  grå: "#8a8a8a",
  graa: "#8a8a8a",
  sølv: "#c9c9c9",
  soelv: "#c9c9c9",
  guld: "#d4af37",
};

/**
 * Best-effort colour for an option value. Handles exact matches and simple
 * compound names ("light blue", "mørkegrøn"). Returns hex or null.
 */
export function autoColor(value) {
  if (!value) return null;
  const key = value.trim().toLowerCase();

  if (NAMED_COLORS[key]) return NAMED_COLORS[key];

  // Direct hex already? ("#fff", "#ffffff")
  if (/^#([0-9a-f]{3}|[0-9a-f]{6})$/i.test(key)) return key;

  // Compound: take the last word as the base colour (e.g. "light blue").
  const words = key.split(/[\s-]+/);
  const base = words[words.length - 1];
  if (NAMED_COLORS[base]) return NAMED_COLORS[base];

  return null;
}