const hexRegex = /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/;
const rgbRegex = /^rgb\(\s*\d{1,3}\s*,\s*\d{1,3}\s*,\s*\d{1,3}\s*\)$/;
// GLOBAL HELPER FUNCTIONS
export function clamp(value, min, max) {
    return Math.min(Math.max(value, min), max);
}
export function adjust(channel, amount) {
    if (amount === 0) {
        return channel;
    }
    return Math.round(clamp(channel + amount * 255, 5, 250));
}
export function adjustHexColor(color, amount) {
    if (!hexRegex.test(color)) {
        return color;
    }
    const matches = color.match(/\w\w/g);
    if (!matches) {
        return color;
    }
    const [r, g, b] = matches.map((c) => parseInt(c, 16));
    return `#${[adjust(r, amount), adjust(g, amount), adjust(b, amount)]
        .map((c) => Math.round(c).toString(16).padStart(2, "0"))
        .join("")}`.toUpperCase();
}
export function adjustRGBColor(r, g, b, amount) {
    return `rgb(${adjust(r, amount)}, ${adjust(g, amount)}, ${adjust(b, amount)})`;
}
// COLOR GENERATION
export function generateColorPalette(baseColor, shades) {
    const palette = {};
    if (hexRegex.test(baseColor)) {
        Object.entries(shades).reduce((accumulator, [key, adjustment]) => {
            accumulator[key] = adjustHexColor(baseColor, adjustment);
            return accumulator;
        }, palette);
    }
    else if (rgbRegex.test(baseColor)) {
        const [r, g, b] = baseColor
            .replace(/[^\d,]/g, "")
            .split(",")
            .map(Number);
        Object.entries(shades).reduce((accumulator, [key, adjustment]) => {
            accumulator[key] = adjustRGBColor(r, g, b, adjustment);
            return accumulator;
        }, palette);
    }
    else {
        Object.entries(shades).reduce((accumulator, [key]) => {
            accumulator[key] = baseColor;
            return accumulator;
        }, palette);
    }
    return palette;
}
export function setCSSVariables(variables) {
    if (typeof variables !== "object")
        return;
    if (typeof variables["--ll-font-family"] === "string") {
        document.documentElement.style.setProperty("--ll-font-family", variables["--ll-font-family"]);
    }
    if (typeof variables["--ll-color-primary"] === "string") {
        // Backwards compatibility until we remove the old color variables
        document.documentElement.style.setProperty("--ll-color-primary", variables["--ll-color-primary"]);
        // #008491 is the default color for the primary color. We don't want to override it
        if (variables["--ll-color-primary"].toUpperCase() !== "#008491") {
            const primaryColorPalette = generateColorPalette(variables["--ll-color-primary"], {
                10: 0.9,
                25: 0.85,
                50: 0.22,
                100: 0,
                200: -0.18,
                300: -0.3,
            });
            Object.entries(primaryColorPalette).forEach(([key, value]) => {
                document.documentElement.style.setProperty(`--ll-colors-primary-${key}`, value);
            });
        }
    }
    if (typeof variables["--ll-color-primary-variant1"] === "string") {
        // Backwards compatibility until we remove the old color variables
        document.documentElement.style.setProperty("--ll-color-primary-variant1", variables["--ll-color-primary-variant1"]);
        // #0F766E is the default color for the secondary color. We don't want to override it
        if (variables["--ll-color-primary-variant1"].toUpperCase() !== "#0F766E") {
            const secondaryColorPalette = generateColorPalette(variables["--ll-color-primary-variant1"], {
                10: 0.25,
                25: 0.23,
                50: 0.11,
                100: 0,
                200: -0.1,
            });
            Object.entries(secondaryColorPalette).forEach(([key, value]) => {
                document.documentElement.style.setProperty(`--ll-colors-secondary-${key}`, value);
            });
        }
    }
    // We will ignore the rest of the variables
}
//# sourceMappingURL=colors.js.map