export const STYLE_GROUPS = {
  size: ['s', 'm', 'l', 'xl', 'xxl'],
  color: ['accent', 'accent-dark', 'dark', 'heading', 'text', 'text-light', 'white'],
  weight: ['normal', 'bold'],
  style: ['normal', 'italic'],
  align: ['left', 'center', 'right', 'justify'],
} as const;

export type StyleGroup = keyof typeof STYLE_GROUPS;

export function validateStyles(input: unknown):
  | { ok: true; styles: Record<string, string> }
  | { ok: false; error: string } {
  if (typeof input !== 'object' || input === null || Array.isArray(input)) {
    return { ok: false, error: 'Stili non validi.' };
  }
  const out: Record<string, string> = {};
  for (const [k, v] of Object.entries(input)) {
    const allowed = (STYLE_GROUPS as Record<string, readonly string[]>)[k];
    if (!allowed) return { ok: false, error: `Gruppo stile sconosciuto: ${k}` };
    if (typeof v !== 'string' || !allowed.includes(v)) {
      return { ok: false, error: `Valore non ammesso per ${k}: ${String(v)}` };
    }
    out[k] = v;
  }
  return { ok: true, styles: out };
}

export function stylesToClasses(styles: Record<string, string>): string {
  return Object.entries(styles).map(([k, v]) => `cs-${k}-${v}`).join(' ');
}
