export const SHADE_LABELS = [
    50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950,
];
// S and L locked for the 500 step in custom palettes — matches the Blue Humand reference.
export const LOCKED_S = 73;
export const LOCKED_L = 59;
// ─── Color math ───────────────────────────────────────────────────────────────
export function hexToHSL(hex) {
    const clean = hex.replace('#', '');
    if (!/^[0-9a-fA-F]{6}$/.test(clean))
        throw new Error(`hexToHSL: expected 6-digit hex, got "${hex}"`);
    const r = parseInt(clean.substring(0, 2), 16) / 255;
    const g = parseInt(clean.substring(2, 4), 16) / 255;
    const b = parseInt(clean.substring(4, 6), 16) / 255;
    const max = Math.max(r, g, b);
    const min = Math.min(r, g, b);
    const delta = max - min;
    const l = (max + min) / 2;
    let s = 0;
    if (delta !== 0) {
        s = l > 0.5 ? delta / (2 - max - min) : delta / (max + min);
    }
    let h = 0;
    if (delta !== 0) {
        if (max === r)
            h = ((g - b) / delta + (g < b ? 6 : 0)) / 6;
        else if (max === g)
            h = ((b - r) / delta + 2) / 6;
        else
            h = ((r - g) / delta + 4) / 6;
    }
    return {
        h: Math.round(h * 360),
        s: Math.round(s * 100),
        l: Math.round(l * 100),
    };
}
export function hslToHex({ h, s, l }) {
    const hN = h / 360;
    const sN = s / 100;
    const lN = l / 100;
    const hueToRgb = (p, q, t) => {
        let tt = t;
        if (tt < 0)
            tt += 1;
        if (tt > 1)
            tt -= 1;
        if (tt < 1 / 6)
            return p + (q - p) * 6 * tt;
        if (tt < 1 / 2)
            return q;
        if (tt < 2 / 3)
            return p + (q - p) * (2 / 3 - tt) * 6;
        return p;
    };
    let r, g, b;
    if (sN === 0) {
        r = g = b = lN;
    }
    else {
        const q = lN < 0.5 ? lN * (1 + sN) : lN + sN - lN * sN;
        const p = 2 * lN - q;
        r = hueToRgb(p, q, hN + 1 / 3);
        g = hueToRgb(p, q, hN);
        b = hueToRgb(p, q, hN - 1 / 3);
    }
    const toHex = (v) => Math.round(v * 255)
        .toString(16)
        .padStart(2, '0')
        .toUpperCase();
    return `#${toHex(r)}${toHex(g)}${toHex(b)}`;
}
// Fixed lightness offsets per step relative to the 500 anchor.
const LT_FIXED = [96, 92, 86, 79, 71]; // steps 50–400
const LT_DARK_OFFSETS = [8, 16, 24, 30, 38]; // subtracted from L500 for steps 600–950
const LT_DARK_MINS = [20, 15, 12, 9, 6];
// Saturation scale factors per step (index 0 = step 50, index 10 = step 950).
const SF = [0.28, 0.42, 0.58, 0.76, 0.9, 1.0, 0.97, 0.9, 0.82, 0.72, 0.58];
function buildLightnessArray(l500) {
    const darks = LT_DARK_OFFSETS.map((offset, i) => Math.max(l500 - offset, LT_DARK_MINS[i]));
    return [...LT_FIXED, l500, ...darks];
}
/** Generate an 11-step palette (50→950) from a hue angle. S and L for step 500 are locked. */
export function generatePaletteFromHue(hue) {
    const hex500 = hslToHex({ h: hue, s: LOCKED_S, l: LOCKED_L });
    return generatePaletteFromHex500(hex500);
}
/** Generate an 11-step palette (50→950) from an arbitrary hex500 anchor color. */
export function generatePaletteFromHex500(hex500) {
    const { h, s, l } = hexToHSL(hex500);
    const lt = buildLightnessArray(l);
    return lt.map((ll, i) => hslToHex({ h, s: Math.min(s * SF[i], 100), l: ll }));
}
// ─── Preset palettes (Figma order) ───────────────────────────────────────────
export const PRESETS = [
    // Row 1 — warm
    { name: 'Copper', steps: generatePaletteFromHex500('#9A543C') },
    {
        name: 'Salmon',
        steps: [
            '#FDF1F3',
            '#FCE4E7',
            '#F8CDD6',
            '#F2A4B3',
            '#EB708A',
            '#DB335B',
            '#BD3159',
            '#962145',
            '#7D1F3F',
            '#6B1D3A',
            '#3C0B1B',
        ],
    },
    {
        name: 'Error',
        steps: [
            '#FEF2F2',
            '#FDE3E3',
            '#FCCCCC',
            '#F8A9A9',
            '#F27777',
            '#E74444',
            '#D42E2E',
            '#B22323',
            '#942020',
            '#7B2121',
            '#430C0C',
        ],
    },
    {
        name: 'Flamingo',
        steps: [
            '#FEF4EE',
            '#FCE7D8',
            '#F8CBB0',
            '#F3A67E',
            '#ED774A',
            '#E9582A',
            '#DA3D1C',
            '#B52C19',
            '#90251C',
            '#74221A',
            '#3F0D0B',
        ],
    },
    {
        name: 'Warning',
        steps: [
            '#FDFAEC',
            '#FCF7CE',
            '#FBEC9E',
            '#F8DA66',
            '#F4C841',
            '#F0B624',
            '#DF920C',
            '#B2680E',
            '#945314',
            '#7E4317',
            '#492109',
        ],
    },
    {
        name: 'Sunshine',
        steps: [
            '#FFFEF2',
            '#FEFBDB',
            '#FDF5B5',
            '#FDEF97',
            '#FBE274',
            '#F8CE63',
            '#F8B136',
            '#F1860F',
            '#C46714',
            '#A25515',
            '#5D2C05',
        ],
    },
    // Row 2 — cool
    {
        name: 'Lime',
        steps: [
            '#FAFEF6',
            '#F4FCEB',
            '#EAFAD9',
            '#DEF6C7',
            '#CEEFAF',
            '#AFE67F',
            '#82DE38',
            '#62B725',
            '#539126',
            '#477D26',
            '#21430D',
        ],
    },
    { name: 'Teal', steps: generatePaletteFromHex500('#35A48E') },
    {
        name: 'Success',
        steps: [
            '#F5FDF6',
            '#E6FBE9',
            '#CFF6D5',
            '#ABEDB6',
            '#7BDD8B',
            '#4ED364',
            '#28C040',
            '#249637',
            '#227831',
            '#1F622C',
            '#0A3613',
        ],
    },
    {
        name: 'Info',
        steps: [
            '#F8FDFE',
            '#ECFAFC',
            '#DCF5F9',
            '#C1ECF4',
            '#A0DEEB',
            '#6ED1E7',
            '#46B9DD',
            '#30A0C7',
            '#3484A4',
            '#326F8B',
            '#18475E',
        ],
    },
    {
        name: 'Blue Humand',
        steps: [
            '#F1F4FD',
            '#DEE5FB',
            '#C5D4F8',
            '#9DB8F3',
            '#6F93EB',
            '#496BE3',
            '#3851D8',
            '#2F3FC6',
            '#2C35A1',
            '#29317F',
            '#1D204E',
        ],
    },
    {
        name: 'Purple',
        steps: [
            '#F4F2FF',
            '#E9E8FF',
            '#D6D4FF',
            '#B9B1FF',
            '#9785FF',
            '#886BFF',
            '#6330F7',
            '#551EE3',
            '#4718BF',
            '#3B169C',
            '#230B6A',
        ],
    },
    // Row 3 — other
    {
        name: 'Mulberry',
        steps: [
            '#FCF3FC',
            '#F9E7F8',
            '#F2CEEF',
            '#E6A7E0',
            '#D672CB',
            '#A33796',
            '#7B3571',
            '#6D2D63',
            '#53234A',
            '#45203E',
            '#2C0E26',
        ],
    },
    {
        name: 'Pink',
        steps: [
            '#FCF1F6',
            '#F9E6F0',
            '#F7CCE3',
            '#F0A5CB',
            '#E66DA7',
            '#D7367D',
            '#B93267',
            '#99224D',
            '#7F1F41',
            '#6A1E39',
            '#410B1E',
        ],
    },
    {
        name: 'Black ink',
        steps: [
            '#F4F4F4',
            '#E0E0E0',
            '#C4C4C4',
            '#9A9A9A',
            '#6A6A6A',
            '#4F4F4F',
            '#434343',
            '#393939',
            '#323232',
            '#2C2C2C',
            '#1C1C1C',
        ],
    },
];
// ─── Contrast utility ─────────────────────────────────────────────────────────
/**
 * Returns the accessible text color to use over a given background.
 * Uses W3C relative luminance. Returns '#000000' when the background is light
 * enough to cause the standard white text to fail WCAG AA (4.5:1).
 */
export function getContrastTextColor(backgroundHex) {
    const clean = backgroundHex.replace('#', '');
    if (!/^[0-9a-fA-F]{6}$/.test(clean))
        throw new Error(`getContrastTextColor: expected 6-digit hex, got "${backgroundHex}"`);
    const toLinear = (c) => {
        const sRGB = c / 255;
        return sRGB <= 0.04045 ? sRGB / 12.92 : ((sRGB + 0.055) / 1.055) ** 2.4;
    };
    const r = toLinear(parseInt(clean.substring(0, 2), 16));
    const g = toLinear(parseInt(clean.substring(2, 4), 16));
    const b = toLinear(parseInt(clean.substring(4, 6), 16));
    const L = 0.2126 * r + 0.7152 * g + 0.0722 * b;
    // Contrast ratio against white = (1 + 0.05) / (L + 0.05)
    const contrastWithWhite = 1.05 / (L + 0.05);
    return contrastWithWhite >= 4.5 ? '#FFFFFF' : '#000000';
}
