/**
 * Shared utility functions for DonutChart components
 */
/**
 * Resolves a CSS variable to its computed value
 * Falls back to the provided value if not a CSS variable or resolution fails
 */
const resolveCssVariable = (value, fallback) => {
    var _a;
    // If not a CSS variable, return as-is
    if (!value.startsWith("var(")) {
        return value;
    }
    // Check if we're in a browser environment
    if (typeof window === "undefined" || typeof document === "undefined") {
        return fallback || value;
    }
    // Extract variable name from var(--name) or var(--name, fallback)
    const match = value.match(/var\(([^,)]+)(?:,\s*([^)]+))?\)/);
    if (!match) {
        return fallback || value;
    }
    const varName = match[1].trim();
    const cssDefault = (_a = match[2]) === null || _a === void 0 ? void 0 : _a.trim();
    // Get computed value from document root
    const computed = getComputedStyle(document.documentElement).getPropertyValue(varName).trim();
    return computed || cssDefault || fallback || value;
};
/**
 * Clamps a value between a minimum and maximum
 */
const clamp = (value, min, max) => Math.min(Math.max(value, min), max);
/**
 * Converts polar coordinates to cartesian coordinates
 */
const polarToCartesian = (centerX, centerY, radius, angleInDegrees) => {
    const angleInRadians = ((angleInDegrees - 90) * Math.PI) / 180;
    return {
        x: centerX + radius * Math.cos(angleInRadians),
        y: centerY + radius * Math.sin(angleInRadians),
    };
};
/**
 * Creates an SVG path for a ring segment (donut segment)
 */
const describeRingSegmentPath = (centerX, centerY, innerRadius, outerRadius, startAngle, endAngle) => {
    const startOuter = polarToCartesian(centerX, centerY, outerRadius, endAngle);
    const endOuter = polarToCartesian(centerX, centerY, outerRadius, startAngle);
    const startInner = polarToCartesian(centerX, centerY, innerRadius, startAngle);
    const endInner = polarToCartesian(centerX, centerY, innerRadius, endAngle);
    const largeArcFlag = endAngle - startAngle <= 180 ? "0" : "1";
    return [
        "M",
        startOuter.x,
        startOuter.y,
        "A",
        outerRadius,
        outerRadius,
        0,
        largeArcFlag,
        0,
        endOuter.x,
        endOuter.y,
        "L",
        startInner.x,
        startInner.y,
        "A",
        innerRadius,
        innerRadius,
        0,
        largeArcFlag,
        1,
        endInner.x,
        endInner.y,
        "Z",
    ].join(" ");
};
/**
 * Creates an SVG path for an arc that draws clockwise
 * Used for stroke-dashoffset animation that reveals clockwise
 */
const describeArcPathClockwise = (centerX, centerY, radius, startAngle, endAngle) => {
    const start = polarToCartesian(centerX, centerY, radius, startAngle);
    const end = polarToCartesian(centerX, centerY, radius, endAngle);
    const largeArcFlag = endAngle - startAngle <= 180 ? "0" : "1";
    return [
        "M",
        start.x,
        start.y,
        "A",
        radius,
        radius,
        0,
        largeArcFlag,
        1, // clockwise sweep
        end.x,
        end.y,
    ].join(" ");
};
/**
 * Converts a hex color to RGB components
 */
const hexToRgb = (value) => {
    const normalized = value.replace("#", "");
    if (normalized.length !== 3 && normalized.length !== 6) {
        return null;
    }
    const expanded = normalized.length === 3
        ? normalized
            .split("")
            .map((char) => char + char)
            .join("")
        : normalized;
    const numberValue = Number.parseInt(expanded, 16);
    if (Number.isNaN(numberValue)) {
        return null;
    }
    return {
        r: (numberValue >> 16) & 255,
        g: (numberValue >> 8) & 255,
        b: numberValue & 255,
    };
};
/**
 * Interpolates between two hex colors
 */
const interpolateHexColor = (from, to, factor) => {
    const start = hexToRgb(from);
    const end = hexToRgb(to);
    if (!start || !end) {
        return from;
    }
    const safeFactor = clamp(factor, 0, 1);
    const r = Math.round(start.r + (end.r - start.r) * safeFactor);
    const g = Math.round(start.g + (end.g - start.g) * safeFactor);
    const b = Math.round(start.b + (end.b - start.b) * safeFactor);
    return `rgb(${r}, ${g}, ${b})`;
};

export { clamp, describeArcPathClockwise, describeRingSegmentPath, hexToRgb, interpolateHexColor, polarToCartesian, resolveCssVariable };
