export interface ThemeColors {
  primaryColor?: string;
}

export const getPrimaryColorStyles = (primaryColor?: string): Record<string, string> => {
  if (!primaryColor) return {};

  return {
    '--babylai-primary-color': primaryColor,
    '--babylai-primary-color-100': lightenColor(primaryColor, 0.9),
    '--babylai-primary-color-200': lightenColor(primaryColor, 0.8),
    '--babylai-primary-color-300': lightenColor(primaryColor, 0.7),
    '--babylai-primary-color-400': lightenColor(primaryColor, 0.6),
    '--babylai-primary-color-500': primaryColor,
    '--babylai-primary-color-600': darkenColor(primaryColor, 0.1),
    '--babylai-primary-color-700': darkenColor(primaryColor, 0.2),
    '--babylai-primary-color-800': darkenColor(primaryColor, 0.3),
    '--babylai-primary-color-900': darkenColor(primaryColor, 0.4),
    '--babylai-primary-color-950': darkenColor(primaryColor, 0.5),
  };
};

function lightenColor(color: string, amount: number): string {
  const num = parseInt(color.replace('#', ''), 16);
  const amt = Math.round(2.55 * amount * 100);
  const R = (num >> 16) + amt;
  const G = ((num >> 8) & 0x00ff) + amt;
  const B = (num & 0x0000ff) + amt;
  return (
    '#' +
    (
      0x1000000 +
      (R < 255 ? (R < 1 ? 0 : R) : 255) * 0x10000 +
      (G < 255 ? (G < 1 ? 0 : G) : 255) * 0x100 +
      (B < 255 ? (B < 1 ? 0 : B) : 255)
    )
      .toString(16)
      .slice(1)
  );
}

function darkenColor(color: string, amount: number): string {
  const num = parseInt(color.replace('#', ''), 16);
  const amt = Math.round(2.55 * amount * 100);
  const R = (num >> 16) - amt;
  const G = ((num >> 8) & 0x00ff) - amt;
  const B = (num & 0x0000ff) - amt;
  return (
    '#' +
    (
      0x1000000 +
      (R > 255 ? 255 : R < 0 ? 0 : R) * 0x10000 +
      (G > 255 ? 255 : G < 0 ? 0 : G) * 0x100 +
      (B > 255 ? 255 : B < 0 ? 0 : B)
    )
      .toString(16)
      .slice(1)
  );
}
