// ============================================
// WCAG & Color Utilities
// ============================================

/**
 * Convert Hex color to RGB array
 */
export function hexToRgb(hex: string): [number, number, number] {
    const bigint = parseInt(hex.replace('#', ''), 16);
    return [(bigint >> 16) & 255, (bigint >> 8) & 255, bigint & 255];
}

/**
 * Calculate Relative Luminance (WCAG 2.0 formula)
 */
export function getLuminance(r: number, g: number, b: number): number {
    const a = [r, g, b].map((v) => {
        v /= 255;
        return v <= 0.03928 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4);
    });
    return a[0] * 0.2126 + a[1] * 0.7152 + a[2] * 0.0722;
}

/**
 * Calculate Contrast Ratio between two colors
 */
export function getContrast(hex1: string, hex2: string): number {
    const rgb1 = hexToRgb(hex1);
    const rgb2 = hexToRgb(hex2);
    const l1 = getLuminance(rgb1[0], rgb1[1], rgb1[2]);
    const l2 = getLuminance(rgb2[0], rgb2[1], rgb2[2]);
    return (Math.max(l1, l2) + 0.05) / (Math.min(l1, l2) + 0.05);
}

/**
 * Convert Hex to HSL
 * Returns [H in degrees, S as 0-1, L as 0-1]
 */
export function hexToHsl(hex: string): [number, number, number] {
    const rgb = hexToRgb(hex);
    const r = rgb[0] / 255;
    const g = rgb[1] / 255;
    const b = rgb[2] / 255;
    const max = Math.max(r, g, b);
    const min = Math.min(r, g, b);
    let h = 0;
    let s = 0;
    const l = (max + min) / 2;

    if (max !== min) {
        const d = max - min;
        s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
        switch (max) {
            case r: h = (g - b) / d + (g < b ? 6 : 0); break;
            case g: h = (b - r) / d + 2; break;
            case b: h = (r - g) / d + 4; break;
        }
        h /= 6;
    }
    return [h * 360, s, l];
}

/**
 * Convert HSL to Hex
 * @param h Hue in degrees (0-360)
 * @param s Saturation as percentage (0-100)
 * @param l Lightness as percentage (0-100)
 */
export function hslToHex(h: number, s: number, l: number): string {
    l /= 100;
    const a = s * Math.min(l, 1 - l) / 100;
    const f = (n: number) => {
        const k = (n + h / 30) % 12;
        const color = l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
        return Math.round(255 * color).toString(16).padStart(2, '0');
    };
    return `#${f(0)}${f(8)}${f(4)}`;
}

/**
 * Generate a random color with controlled HSL values
 */
export function generateHslColor(
    hueRange: [number, number] | null,
    satRange: [number, number],
    lumRange: [number, number]
): string {
    const h = hueRange
        ? Math.floor(Math.random() * (hueRange[1] - hueRange[0])) + hueRange[0]
        : Math.floor(Math.random() * 360);

    const s = Math.floor(Math.random() * (satRange[1] - satRange[0])) + satRange[0];
    const l = Math.floor(Math.random() * (lumRange[1] - lumRange[0])) + lumRange[0];

    return hslToHex(h, s, l);
}

// Default theme colors
export const DEFAULT_THEME = {
    primary: "#003B9B",
    secondary: "#FFAB00",
    text: "#FFFFFF",
    background: "#000000",
};
