import { jsxs, jsx } from 'react/jsx-runtime';
import React__default from 'react';
import { cn } from '../../../../utils/cn.js';
import { ChartText } from '../ChartText/ChartText.js';
import { generateNiceTicks } from '../../utils/chart-validation.js';

/**
 * YAxis component for charts
 * Renders vertical axis with labels, optional tick marks, and grid lines
 */
const YAxis = React__default.memo(({ min, max, tickCount = 5, x, y, height, show = true, className, color = "currentColor", showTicks = true, tickLength = 6, tickPadding = 8, textAnchor = "end", tickFormatter, showGrid = false, gridWidth = 0, ticks: customTicks, }) => {
    if (!show)
        return null;
    // Generate tick values using Heckbert's nice numbers algorithm
    const ticks = (() => {
        if (customTicks)
            return customTicks;
        if (tickCount <= 1)
            return [min];
        const { ticks: niceTicks } = generateNiceTicks(min, max, tickCount);
        return niceTicks;
    })();
    const formatTick = (value) => {
        if (tickFormatter) {
            return tickFormatter(value);
        }
        // Default formatting - prioritize whole numbers
        if (Math.abs(value) >= 1000000) {
            return `${Math.round(value / 1000000)}M`;
        }
        else if (Math.abs(value) >= 1000) {
            return `${Math.round(value / 1000)}K`;
        }
        else if (Number.isInteger(value) ||
            Math.abs(value - Math.round(value)) < 0.01) {
            return Math.round(value).toString();
        }
        else {
            return value.toFixed(1);
        }
    };
    const getTickY = (value) => {
        if (max === min) {
            return y + height / 2; // Center position when no range
        }
        const ratio = (value - min) / (max - min);
        return y + height - ratio * height;
    };
    return (jsxs("g", { className: cn("chart-y-axis", className), children: [jsx("line", { x1: x, y1: y, x2: x, y2: y + height, className: "stroke-[var(--color-border)]", strokeWidth: 1 }), ticks.map((tick, index) => {
                const tickY = getTickY(tick);
                return (jsxs("g", { children: [showGrid && gridWidth > 0 && (jsx("line", { x1: x, y1: tickY, x2: x + gridWidth, y2: tickY, className: "opacity-80 stroke-[var(--color-border)]", strokeWidth: 1 })), showTicks && (jsx("line", { x1: x - tickLength, y1: tickY, x2: x, y2: tickY, className: "stroke-[var(--color-border)]", strokeWidth: 1 })), jsx(ChartText, { x: x - tickLength - tickPadding, y: tickY, textAnchor: textAnchor, dominantBaseline: "middle", children: formatTick(tick) })] }, index));
            })] }));
});

export { YAxis };
