// #region Imports
import chroma from 'chroma-js';
import { ColorFormat } from '../../../Types/ColorFormat';
// #endregion
/**
 * Color validation patterns for different color formats.
 *
 * @internal
 */
const COLOR_PATTERNS = {
    [ColorFormat.Hex]: '#([A-Fa-f0-9]{3,4}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})',
    [ColorFormat.Hex8]: '#[A-Fa-f0-9]{8}',
    [ColorFormat.Rgb]: 'rgb\\s*\\(\\s*\\d{1,3}\\s*,\\s*\\d{1,3}\\s*,\\s*\\d{1,3}\\s*\\)',
    [ColorFormat.Rgba]: 'rgba\\s*\\(\\s*\\d{1,3}\\s*,\\s*\\d{1,3}\\s*,\\s*\\d{1,3}\\s*,\\s*(0|1|0?\\.\\d+)\\s*\\)',
    [ColorFormat.Hsl]: 'hsl\\s*\\(\\s*\\d{1,3}\\s*,\\s*\\d{1,3}%?\\s*,\\s*\\d{1,3}%?\\s*\\)',
    [ColorFormat.Hsla]: 'hsla\\s*\\(\\s*\\d{1,3}\\s*,\\s*\\d{1,3}%?\\s*,\\s*\\d{1,3}%?\\s*,\\s*(0|1|0?\\.\\d+)\\s*\\)',
    [ColorFormat.Hsv]: 'hsv\\s*\\(\\s*\\d{1,3}\\s*,\\s*\\d{1,3}%?\\s*,\\s*\\d{1,3}%?\\s*\\)',
    [ColorFormat.Hsva]: 'hsva\\s*\\(\\s*\\d{1,3}\\s*,\\s*\\d{1,3}%?\\s*,\\s*\\d{1,3}%?\\s*,\\s*(0|1|0?\\.\\d+)\\s*\\)',
    [ColorFormat.Oklch]: 'oklch\\s*\\(\\s*[\\d.]+%?\\s+[\\d.]+\\s+[\\d.]+\\s*(\\s*/\\s*(0|1|0?\\.\\d+))?\\s*\\)',
    [ColorFormat.Oklab]: 'oklab\\s*\\(\\s*[\\d.]+%?\\s+[\\d.-]+\\s+[\\d.-]+\\s*(\\s*/\\s*(0|1|0?\\.\\d+))?\\s*\\)',
    [ColorFormat.Lch]: 'lch\\s*\\(\\s*[\\d.]+%?\\s+[\\d.]+\\s+[\\d.]+\\s*(\\s*/\\s*(0|1|0?\\.\\d+))?\\s*\\)',
    [ColorFormat.Lab]: 'lab\\s*\\(\\s*[\\d.]+%?\\s+[\\d.-]+\\s+[\\d.-]+\\s*(\\s*/\\s*(0|1|0?\\.\\d+))?\\s*\\)'
};
/**
 * A pattern that matches any supported color format.
 *
 * @internal
 */
const ANY_COLOR_PATTERN = Object.values(COLOR_PATTERNS).join('|');
/**
 * Utility class for color operations in the ColorBox component.
 *
 * @public
 */
export class ColorUtils {
    // #region Methods
    /**
     * Gets the validation pattern for a specific color format.
     *
     * @param format - The color format to get the pattern for.
     * @returns The regex pattern string for the format.
     * @public
     * @static
     */
    static getPatternForFormat(format) {
        return COLOR_PATTERNS[format];
    }
    /**
     * Gets a pattern that matches any supported color format.
     *
     * @returns The regex pattern string that matches any color format.
     * @public
     * @static
     */
    static getAnyColorPattern() {
        return ANY_COLOR_PATTERN;
    }
    /**
     * Validates if a color string matches the expected format.
     *
     * @param value - The color string to validate.
     * @param format - The expected color format (optional, validates against any format if not provided).
     * @returns True if the color string is valid.
     * @public
     * @static
     */
    static isValidColor(value, format) {
        if (!value) {
            return false;
        }
        // First try chroma-js validation (most reliable)
        if (!chroma.valid(value)) {
            return false;
        }
        // If a specific format is requested, check the pattern
        if (format) {
            const pattern = new RegExp(`^${COLOR_PATTERNS[format]}$`, 'i');
            return pattern.test(value.trim());
        }
        return true;
    }
    /**
     * Converts a color string from one format to another.
     *
     * @param value - The color string to convert.
     * @param targetFormat - The target color format.
     * @returns The converted color string, or the original value if conversion fails.
     * @public
     * @static
     */
    static convertColor(value, targetFormat) {
        if (!value || !chroma.valid(value)) {
            return value;
        }
        try {
            const color = chroma(value);
            const alpha = color.alpha();
            switch (targetFormat) {
                case ColorFormat.Hex:
                    return alpha < 1 ? color.hex('rgba') : color.hex();
                case ColorFormat.Hex8:
                    return color.hex('rgba');
                case ColorFormat.Rgb:
                    return color.css();
                case ColorFormat.Rgba: {
                    const [r, g, b] = color.rgb();
                    return `rgba(${r}, ${g}, ${b}, ${alpha})`;
                }
                case ColorFormat.Hsl:
                    return color.css('hsl');
                case ColorFormat.Hsla: {
                    const [h, s, l] = color.hsl();
                    return `hsla(${Math.round(h || 0)}, ${(s * 100).toFixed(1)}%, ${(l * 100).toFixed(1)}%, ${alpha})`;
                }
                case ColorFormat.Hsv: {
                    const [h, s, v] = color.hsv();
                    return `hsv(${Math.round(h || 0)}, ${(s * 100).toFixed(1)}%, ${(v * 100).toFixed(1)}%)`;
                }
                case ColorFormat.Hsva: {
                    const [h, s, v] = color.hsv();
                    return `hsva(${Math.round(h || 0)}, ${(s * 100).toFixed(1)}%, ${(v * 100).toFixed(1)}%, ${alpha})`;
                }
                case ColorFormat.Oklch: {
                    const [l, c, h] = color.oklch();
                    return alpha < 1
                        ? `oklch(${(l * 100).toFixed(2)}% ${c.toFixed(4)} ${(h || 0).toFixed(2)} / ${alpha})`
                        : `oklch(${(l * 100).toFixed(2)}% ${c.toFixed(4)} ${(h || 0).toFixed(2)})`;
                }
                case ColorFormat.Oklab: {
                    const [l, a, b] = color.oklab();
                    return alpha < 1
                        ? `oklab(${(l * 100).toFixed(2)}% ${a.toFixed(4)} ${b.toFixed(4)} / ${alpha})`
                        : `oklab(${(l * 100).toFixed(2)}% ${a.toFixed(4)} ${b.toFixed(4)})`;
                }
                case ColorFormat.Lch: {
                    const [l, c, h] = color.lch();
                    return alpha < 1
                        ? `lch(${l.toFixed(2)}% ${c.toFixed(2)} ${(h || 0).toFixed(2)} / ${alpha})`
                        : `lch(${l.toFixed(2)}% ${c.toFixed(2)} ${(h || 0).toFixed(2)})`;
                }
                case ColorFormat.Lab: {
                    const [l, a, b] = color.lab();
                    return alpha < 1
                        ? `lab(${l.toFixed(2)}% ${a.toFixed(2)} ${b.toFixed(2)} / ${alpha})`
                        : `lab(${l.toFixed(2)}% ${a.toFixed(2)} ${b.toFixed(2)})`;
                }
                default:
                    return color.hex();
            }
        }
        catch {
            return value;
        }
    }
    /**
     * Detects the color format from a color string.
     *
     * @param value - The color string to analyze.
     * @returns The detected color format, or hex as default.
     * @public
     * @static
     */
    static detectFormat(value) {
        if (!value) {
            return ColorFormat.Hex;
        }
        const trimmed = value.trim().toLowerCase();
        if (trimmed.startsWith('#')) {
            return trimmed.length === 9 ? ColorFormat.Hex8 : ColorFormat.Hex;
        }
        if (trimmed.startsWith('rgba')) {
            return ColorFormat.Rgba;
        }
        if (trimmed.startsWith('rgb')) {
            return ColorFormat.Rgb;
        }
        if (trimmed.startsWith('hsla')) {
            return ColorFormat.Hsla;
        }
        if (trimmed.startsWith('hsl')) {
            return ColorFormat.Hsl;
        }
        if (trimmed.startsWith('hsva')) {
            return ColorFormat.Hsva;
        }
        if (trimmed.startsWith('hsv')) {
            return ColorFormat.Hsv;
        }
        if (trimmed.startsWith('oklch')) {
            return ColorFormat.Oklch;
        }
        if (trimmed.startsWith('oklab')) {
            return ColorFormat.Oklab;
        }
        if (trimmed.startsWith('lch')) {
            return ColorFormat.Lch;
        }
        if (trimmed.startsWith('lab')) {
            return ColorFormat.Lab;
        }
        return ColorFormat.Hex;
    }
    /**
     * Parses any valid color string and returns a normalized hex value.
     * Useful for internal color processing.
     *
     * @param value - The color string to parse.
     * @returns The hex color string, or null if invalid.
     * @public
     * @static
     */
    static toHex(value) {
        if (!value || !chroma.valid(value)) {
            return null;
        }
        try {
            const color = chroma(value);
            return color.alpha() < 1 ? color.hex('rgba') : color.hex();
        }
        catch {
            return null;
        }
    }
}
//# sourceMappingURL=ColorUtils.js.map